I have 2 classes. ClassA has some objects of ClassB called Field01, Field02, ... Class A also has a list of ClassB items.
I know the name of the ClassB items and want to fill the ClassB items with the values from the list.
Example code:
public class ClassA
{
public ClassB Field01 {get;set;}
public ClassB Field02 {get;set;}
//...
List<ClassB> myItems;
public void FillItems()
{
foreach(var item in MyItems)
{
// what I want in hardcode:
Field01.ValueA = MyItems[0].ValueA;
Field01.ValueB = MyItems[0].ValueB;
Field02.ValueA = MyItems[1].ValueA;
// ...
}
}
}
public class ClassB
{
public string ValueA {get;set;}
public string ValueB {get;set;}
}
In my case I'll have a counting variable which will create the Field01, Field02, Field03, ... names depending on how many items are in my List (there are always enough ClassB fields in ClassA to fill)
I know i can get the PropertyInfo of my ClassB items via name, but I don't know you I can access the ClassB attributes ValueA and ValueB
// If ClassB was just a string or object this would work
Type myType = typeof(ClassA);
PropertyInfo myPropInfo = myType.GetProperty("Field01");
myPropInfo.SetValue(this, "Hello", null);
Aucun commentaire:
Enregistrer un commentaire