.net - Populate Class from List of Objects C# -
first off hope can describe looking if not clear please let me know , try best clarify.
i have 2 classes 1 being small class gets wrapped list. second class uses values list. here examples of classes.
class firstclass { public string value1 {get; set; } public string value2 {get; set; } public string value3 {get; set; } public string value4 {get; set; } public string value5 {get; set; } public string value6 {get; set; } } class secondclass { public string fieldname { get; set; } public string fieldvalue { get; set; } } list<secondclass> sc = new list<secondclass>(); sc[0].fieldname = "value1"; sc[0].fieldvalue = "hello world"; sc[1].fieldname = "value2"; sc[1].fieldvalue = "hello world"; sc[2].fieldname = "value3"; sc[2].fieldvalue = "hello world"; sc[3].fieldname = "value4"; sc[3].fieldvalue = "hello world"; sc[4].fieldname = "value5"; sc[4].fieldvalue = "hello world"; sc[5].fieldname = "value6"; sc[5].fieldvalue = "hello world";
okay see 2 classes there , see how secondclass list has 2 values each object first 1 being name of field matches name of field in firstclass , value want apply corresponding field in firstclass.
so how transfer values 1 other? god hope cleared , did not make confusing.
edit edit edit
so think have better way explain using information above.
the secondclass object in index 2
sc[2].fieldname = "value3"; sc[2].fieldvalue = "hello world";
needs assigned firstclass
so sc[2].fieldname = "value3";
in firstclass
actual field named value3
need assign value of sc[2].fieldvalue = "hello world";
firstclass
field value3
hopefully cleared bit.
i think need use reflection accomplish goal...i.e. typeof(firstclass).getproperties
in static constructor of firstclass use reflection dictionary of propertyinfo instances. once , cache performance reasons. when call method setvaluebyreflection
looks proposed string value of property in dictionary , sets value.
internal class program { private static void main(string[] args) { list<secondclass> secondclasses = new list<secondclass>(); secondclasses.add(new secondclass { fieldname = "value1", fieldvalue = "hello world 1" }); secondclasses.add(new secondclass { fieldname = "value2", fieldvalue = "hello world 2" }); secondclasses.add(new secondclass { fieldname = "value3", fieldvalue = "hello world 3" }); secondclasses.add(new secondclass { fieldname = "value4", fieldvalue = "hello world 4" }); secondclasses.add(new secondclass { fieldname = "value5", fieldvalue = "hello world 5" }); secondclasses.add(new secondclass { fieldname = "value6", fieldvalue = "hello world 6" }); // secondclasses.add(new secondclass { fieldname = "value7", fieldvalue = "hello world 7" }); // line throw exception firstclass fc = new firstclass(); foreach (secondclass secondclass in secondclasses) { fc.setvaluebyreflection(secondclass.fieldname, secondclass.fieldvalue); } } } class firstclass { private static readonly dictionary<string, propertyinfo> _publicproperties; static firstclass() { const bindingflags bindingflags = bindingflags.instance | bindingflags.declaredonly | bindingflags.public | bindingflags.setproperty; _publicproperties = typeof(firstclass).getproperties(bindingflags).todictionary(propertyinfo => propertyinfo.name); } public string value1 { get; set; } public string value2 { get; set; } public string value3 { get; set; } public string value4 { get; set; } public string value5 { get; set; } public string value6 { get; set; } /// <summary> /// sets value of 1 of properties of class specified propertyname parameter. /// </summary> /// <exception cref="argumentexception">thrown when propertyname not valid property of firstclass.</exception> public void setvaluebyreflection(string propertyname, string value) { propertyinfo propertyinfo; _publicproperties.trygetvalue(propertyname, out propertyinfo); if (propertyinfo != null) { propertyinfo.setvalue(this, value, null); } else { throw new argumentexception("firstclass not contain property of name " + propertyname, "propertyname"); } } } class secondclass { public string fieldname { get; set; } public string fieldvalue { get; set; } }
Comments
Post a Comment