Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneric class dynamic method calls to subclasses
    text
    copied!<p>I have the main class one Generic class called File, and two classes called Record1 and Record2 that represented two different types of data.</p> <p>From the Main class i call Process like this:</p> <pre><code>class Program { static void Main(string[] args) { XmlDocument xml = new XmlDocument(); xFile&lt;Record1&gt; xfile1 = new xFile&lt;Record1&gt;(); xml.Load(@"..\Data\xml1.xml"); string rootElementName = "lana"; xfile1 = xFile&lt;Record1&gt;.Deserialize(xml.InnerXml, rootElementName); string a = xfile1.Process(); } } </code></pre> <p>my file class looks like this</p> <pre><code>namespace OOP { [Serializable] public class xFile&lt;T&gt; { 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&lt;T&gt;(); } public xFile (string abc ) { } private List&lt;T&gt; record; [XmlElement("record")] public List&lt;T&gt; 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&lt;T&gt; Deserialize(string xml, string rootElementName) { XmlRootAttribute xRoot = new XmlRootAttribute(); xRoot.ElementName = rootElementName; //xRoot.Namespace = rootNamespace; xRoot.IsNullable = true; xFile&lt;T&gt; result = null; XmlSerializer serializerTarget = new XmlSerializer(typeof(xFile&lt;T&gt;), xRoot); using (TextReader reader = new StringReader(xml)) { result = (xFile&lt;T&gt;)serializerTarget.Deserialize(reader); } return result; } } } </code></pre> <p>I have 2 classes called Record1 and Record2 they both implement an interface that defines the Validate method to validate the different types of data in each record.</p> <pre><code> [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"; } } </code></pre> <p>how can i call the Validate method of each of the two classes Record1 and record2 from the xFile&lt;> class?</p> <p>Thank you! Bilal</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload