inheritance - Is there a simple way to generically parse data from strings for child class properties of differing types in C#? -


i have base class

abstract public class containerclass {     protected containerclass () {         // stuff     }      virtual protected void parsedata() {         // stuff i'm mentioning later     } } 

and child classes

public class childclassone : containerclass {     public childclassone () : base () {         var1s = "99";         var2s = "88.3";         var3s = "2015-04-22t15:55:25.2625065-07:00";     }     public int var1 {get; protected set;}     public double var2 {get; protected set;}     public datetime var3 {get; protected set;}      public string var1s {get; protected set;}     public string var2s {get; protected set;}     public string var3s {get; protected set;} } 

and

public class childclasstwo : containerclass {     public childclasstwo () : base () {         var1s = "99.22";         var2s = "88.3";         var3s = "43.44";     }     public double var1 {get; protected set;}     public double var2 {get; protected set;}     public double var3 {get; protected set;}      public string var1s {get; protected set;}     public string var2s {get; protected set;}     public string var3s {get; protected set;} } 

what want define parsedata in parent class iterate through properties when it's called child class , parse strings relevant data.

order needs preserved. , strings might external data, not properties or fields of these classes.

my current thoughts in pseudo-code like:

for (property prop in thisclass) {     typeof(prop) temp;     if (typeof(prop).tryparse(var1s, temp))         prop = temp; } 

all of class i'm looking @ have tryparse method 2 input variables. can i'm trying work? if so, how can iterate on properties in unambiguous order?

simple? no. possible? yes.

what want define parsedata in parent class iterate through properties when it's called child class , parse strings relevant data.

the following first part of question. here fiddle demonstrate.

virtual protected void parsedata() {     // iterate through properties when it's called child class     foreach(var p in this.gettype().getproperties())     {         // , parse strings relevant data         var propname = p.name;         var propvalue = p.getvalue(this);         console.writeline(string.format("{0} = {1}", propname, propvalue));     } } 

order needs preserved.

this it's no longer simple. msdn says that, "the order of returned collection not guaranteed identical between calls."

this question `type.getproperties` property order has answer recommends custom attribute preserve order. this:

[system.attributeusage(system.attributetargets.property)] public class properyorderattribute : system.attribute {     public int order { get; private set; }      public properyorderattribute(int order)     {         this.order = order;     } } 

you use on properties.

[properyorderattribute(0)] public int var1 { get; protected set; } 

and compare this:

protected comparison<propertyinfo> comparer = (x,y) => {     var type = typeof(properyorderattribute);     var xattr = x.getcustomattributes(type, false);     var yattr = y.getcustomattributes(type, false);     var xorder = xattr.count() > 0 ?          (xattr[0] properyorderattribute).order :          int64.maxvalue;     var yorder = yattr.count() > 0 ?          (yattr[0] properyorderattribute).order :          int64.maxvalue;     return xorder.compareto(yorder); }; 

this how final parsedata() method, order preserved, work:

virtual protected void parsedata() {     var properties = this.gettype().getproperties();      // maintain order     array.sort(properties, comparer);      // iterate through properties when it's called child class     foreach (var p in properties)     {         // , parse strings relevant data         var propname = p.name;         var propvalue = p.getvalue(this);         console.writeline(string.format("{0} = {1}", propname, propvalue));     } } 

again, this fiddle demonstrates complete solution.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -