泛型类动态方法调用子类

时间:2013-11-25 20:22:24

标签: c# .net class generics

我有一个名为File的主要类Generic类,以及两个名为Record1和Record2的类,它们代表两种不同类型的数据。

从Main类我调用这样的过程:

class Program
   {
       static void Main(string[] args)
       {
           XmlDocument xml = new XmlDocument();
           xFile<Record1> xfile1 = new xFile<Record1>();
           xml.Load(@"..\Data\xml1.xml");
           string rootElementName = "lana";
           xfile1 = xFile<Record1>.Deserialize(xml.InnerXml, rootElementName);
           string a = xfile1.Process();
       }
   }

我的文件类看起来像这样

namespace OOP
{
    [Serializable]
    public class xFile<T>
    {

        private XmlSerializerNamespaces xmlNamespaces = null;
        [XmlNamespaceDeclarations]
        public XmlSerializerNamespaces Namespaces
        {
            get
            {
                return this.xmlNamespaces;
            }
        }


        public xFile()
        {
            xmlNamespaces = new XmlSerializerNamespaces(new XmlQualifiedName[] { new XmlQualifiedName(string.Empty, "urn:LANA") });
            Record = new List<T>();
        }

        public xFile (string abc )
        {

        }

        private List<T> record;
        [XmlElement("record")]
        public List<T> Record 
        { 
            get
            {
                return record;
            }
            set
            {
                record = value;
            }
        }


        public string Process()
        {
            //I need to call the Validate method of each record and i'm not sure how to call them polymorphically bassed on the type of a generic class
        }


        public static xFile<T> Deserialize(string xml, string rootElementName)
        {

            XmlRootAttribute xRoot = new XmlRootAttribute();
            xRoot.ElementName = rootElementName;
            //xRoot.Namespace = rootNamespace;
            xRoot.IsNullable = true;


            xFile<T> result = null;
            XmlSerializer serializerTarget = new XmlSerializer(typeof(xFile<T>), xRoot);

            using (TextReader reader = new StringReader(xml))
            {
                result = (xFile<T>)serializerTarget.Deserialize(reader);
            }
            return result;
        }

    }
}

我有两个名为Record1和Record2的类,它们都实现了一个定义Validate方法的接口,以验证每条记录中不同类型的数据。

 [Serializable]
    public class Record1 : IServiceRecord
    {
        [XmlElement("ppp1")]
        public string PPP1
        {
            get;
            set;
        }
        [XmlElement("ppp2")]
        public string PPP2
        {
            get;
            set;
        }

        public string Validate()
        {
            return "Test a";
        }
    }

如何从xFile&lt;&gt;中调用两个类Record1和record2中的每一个的Validate方法?类?

谢谢! 比拉尔

2 个答案:

答案 0 :(得分:1)

您的xFile<T>类包含List<T> Record属性,因此您应该能够对列表中的每个成员调用validate:

xfile1 = xFile<Record1>.Deserialize(xml.InnerXml, rootElementName);
foreach(var record in xfile1.Record)
   Console.WriteLine(record.Validate());

要将此添加到您的Process()方法,您需要限制您的泛型以允许它:

public class xFile<T> where T : IServiceRecord

然后你可以打电话给它:

    public string Process()
    {
        foreach(T record in this.Record)
        {
            string validation = record.Validate();
            // Do something with validation
        }
    }

答案 1 :(得分:0)

您面临的问题是,使用普通泛型类型,除了简单的对象语义之外,您无法做很多事情。因为类型参数可以是运行时的任何东西,所以编译必须不假设它,除了它将是一个对象。这就是为什么你做的不仅仅是检查通用性是否相等。

解决方案是为编译器提供有关T的更多前期信息,即约束泛型类型。您可以将此作为类定义的一部分,如下所示:

[Serializable]
public class xFile<T>
    where T : IServiceRecord
{
    ...
}

where T : IServiceRecord行表示仅实现IServiceRecord 的类型可用作xFile中的参数。这让编译器知道任何T上都会有Validate

然后,在您的Process方法中,它就像C#中的其他任何地方一样:

foreach (var record in records)
{
    record.Validate();
}