作为通用类型传递的访问类属性 [英] Access class property passed as generic type

查看:41
本文介绍了作为通用类型传递的访问类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,这些类被传递给Serialization方法,并且我想在Serialization方法中访问这些类的两个属性.问题是序列化方法参数作为通用类型传递,在这种情况下,我不知道如何访问传递的类的属性.下面的例子.

I have two classes, which are passed to Serialization method and I would like to access two properties of these classes in Serialization method. The problem is that Serialization method parameter are passed as generic type and I do not know how to access properties of passed class in this case. The example below.

    public class MyClass1 
    {

            public string MyProperty1 { get; set; }

            //These properties are shared in both classes
            public bool Result { get; set; }
            public string EngineErrorMessage { get; set; }

    }
    public class MyClass2 
    {

            public string MyProperty2 { get; set; }

            //These properties are shared in both classes
            public bool Result { get; set; }
            public string EngineErrorMessage { get; set; }

    }


//The method is used to serialize classes above, classes are passed as generic types
    public void Serialization<T>(ref T engine)
            {
                try
                {
                 //Do some work with passed class
                 }
                catch (Exception e)
                {

                   //If Exception occurs I would like to write values to passed class properties, how to do that?
                   Result = false;
                   EngineErrorMessage = e.Message;
                }
    }

完整的方法代码

     public void Submit<T>(ref T engine)
        {
            try
            {

                var workingDir = Path.Combine(Settings.FileStoragePath, Helpers.GetRandomInt(9).ToString());



                Directory.CreateDirectory(workingDir);
                var inputFile = Path.Combine(workingDir, Settings.InFileName);
                var outputFile = Path.Combine(workingDir, Settings.OutFileName);
                var deleteFile = Path.Combine(workingDir, Settings.DelFileName);

                try
                {



                    using (var stream = new FileStream(inputFile, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        Serializer.Serialize(stream, engine);
                    }


                    CheckStatus(outputFile);


                    using (var stream = new FileStream(outputFile, FileMode.Open, FileAccess.Read, FileShare.None))
                    {
                        engine = Serializer.Deserialize<T>(stream);                        
                    }


                }
                finally
                {
                    File.Create(deleteFile).Dispose();
                }
            }
            catch (Exception e)
            {
                //ToDo: Not implemented yet.
/*               Result = false;
               ErrorMessage = e.Message;*/
            }
        }

推荐答案

声明一个包含属性 Result EngineErrorMessage 的接口.现在,您有两个选择:

Declare an interface containing the properties Result and EngineErrorMessage. Now you have two options:

  1. 在您的序列化类型参数中添加一个约束,以便只能对从上述接口派生的类型进行序列化,或者
  2. 在您的catch块中,尝试将 engine 强制转换为上述接口.如果强制转换成功,则写入属性值,否则不执行任何操作.
  1. Add a constraint to your serialization type parameter so that only types that derive from the interface mentioned above can be serialized, or
  2. In your catch block try to cast engine to the interface mentioned above. If the cast succeeds, write the propertie values, otherwise do nothing.

示例:

public interface ISerializationErrorWriter
{
    bool Result { set; get; }
    string EngineErrorMessage { set; get; }
}

public class MyClass1 : ISerializationErrorWriter
{
    public string MyProperty1 { get; set; }

    public bool Result { get; set; }
    public string EngineErrorMessage { get; set; }
}

public class MyClass2 : ISerializationErrorWriter
{
    public string MyProperty2 { get; set; }

    public bool Result { get; set; }
    public string EngineErrorMessage { get; set; }
}

// Option 1:
public void Serialization_1<T>(ref T engine) where T : ISerializationErrorWriter
{
    try
    {
        //Do some work with passed class
    }
    catch (Exception e)
    {
        engine.Result = false;
        engine.EngineErrorMessage = e.Message;
    }
}

// Option 2:
public void Serialization_2<T>(ref T engine)
{
    try
    {
        //Do some work with passed class
    }
    catch (Exception e)
    {
        var serializationErrorWriter = engine as ISerializationErrorWriter;
        if(serializationErrorWriter != null)
        {
            serializationErrorWriter.Result = false;
            serializationErrorWriter.EngineErrorMessage = e.Message;
        }
    }
}

这篇关于作为通用类型传递的访问类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆