How to set an item in a object's property(of array type) using Reflection
-
Hi All, I have a class named "C1" which has a property name "P1", which is declared as below in class. Class C1 { ... Account1[] P1;// Here Account1 is another class/type ... } Now i have created an object of "C1" using reflection as below. // object Type objTempType = Type.GetType("C1"); object objTempObject = Activator.CreateInstance(objTempType); Now i want to set value of first item in "P1" property(An array property) of objTempObject using reflection, which i can do without reflection as per below. objTempType.P1[0].SomeFieldinAccount1Class = SomeObjectOrValue; Could any one of you please give me some direction on how is that possible. I checked all the possible ways do it using property/reflection related classes in C#, also searched many sites and forums to get answer but without any success...
"You just become, like a flower becomes the fruit. It's all built in within you. Allow it to work out." for more details visit : http://www.freemeditation.ca/ http://www.sahajayoga.org Akhilesh Singh
modified on Wednesday, August 26, 2009 5:41 AM
-
Hi All, I have a class named "C1" which has a property name "P1", which is declared as below in class. Class C1 { ... Account1[] P1;// Here Account1 is another class/type ... } Now i have created an object of "C1" using reflection as below. // object Type objTempType = Type.GetType("C1"); object objTempObject = Activator.CreateInstance(objTempType); Now i want to set value of first item in "P1" property(An array property) of objTempObject using reflection, which i can do without reflection as per below. objTempType.P1[0].SomeFieldinAccount1Class = SomeObjectOrValue; Could any one of you please give me some direction on how is that possible. I checked all the possible ways do it using property/reflection related classes in C#, also searched many sites and forums to get answer but without any success...
"You just become, like a flower becomes the fruit. It's all built in within you. Allow it to work out." for more details visit : http://www.freemeditation.ca/ http://www.sahajayoga.org Akhilesh Singh
modified on Wednesday, August 26, 2009 5:41 AM
Hi Before you can set the SomeFieldinAccount1Class property/field value you need to intialize / create the element at position 0
objTempType.P1[0] = new Account1();
-
Hi Before you can set the SomeFieldinAccount1Class property/field value you need to intialize / create the element at position 0
objTempType.P1[0] = new Account1();
Hello nissims, Thanks for your reply. i have already created the first item in P1[0](before i try setvalue thing), I just didn't want to make my post a more lengthier one, so made the code hide.
"You just become, like a flower becomes the fruit. It's all built in within you. Allow it to work out." for more details visit : http://www.freemeditation.ca/ http://www.sahajayoga.org Akhilesh Singh
modified on Wednesday, October 7, 2009 4:23 AM
-
Hi All, I have a class named "C1" which has a property name "P1", which is declared as below in class. Class C1 { ... Account1[] P1;// Here Account1 is another class/type ... } Now i have created an object of "C1" using reflection as below. // object Type objTempType = Type.GetType("C1"); object objTempObject = Activator.CreateInstance(objTempType); Now i want to set value of first item in "P1" property(An array property) of objTempObject using reflection, which i can do without reflection as per below. objTempType.P1[0].SomeFieldinAccount1Class = SomeObjectOrValue; Could any one of you please give me some direction on how is that possible. I checked all the possible ways do it using property/reflection related classes in C#, also searched many sites and forums to get answer but without any success...
"You just become, like a flower becomes the fruit. It's all built in within you. Allow it to work out." for more details visit : http://www.freemeditation.ca/ http://www.sahajayoga.org Akhilesh Singh
modified on Wednesday, August 26, 2009 5:41 AM
You can use the Array class when operating on array properties with reflection. Using only refection, what you want to achieve is done like this:
Type objTempType = Type.GetType("C1");
object objTempObject = Activator.CreateInstance(objTempType);PropertyInfo p1Property = objTempType.GetProperty("P1");
Array p1ArrayValue = p1Property.GetValue(objTempObject, null) as Array;Type itemType = p1ArrayValue.GetType().GetElementType();
object itemValue = p1ArrayValue.GetValue(0);PropertyInfo someFieldProperty = itemType.GetProperty("SomeFieldinAccount1Class");
someFieldProperty.SetValue(itemValue, SomeObjectOrValue, null); -
You can use the Array class when operating on array properties with reflection. Using only refection, what you want to achieve is done like this:
Type objTempType = Type.GetType("C1");
object objTempObject = Activator.CreateInstance(objTempType);PropertyInfo p1Property = objTempType.GetProperty("P1");
Array p1ArrayValue = p1Property.GetValue(objTempObject, null) as Array;Type itemType = p1ArrayValue.GetType().GetElementType();
object itemValue = p1ArrayValue.GetValue(0);PropertyInfo someFieldProperty = itemType.GetProperty("SomeFieldinAccount1Class");
someFieldProperty.SetValue(itemValue, SomeObjectOrValue, null);Thanks Bro... your suggestion exactly matched what i needed and was more than i expected... thanks again... :)
"You just become, like a flower becomes the fruit. It's all built in within you. Allow it to work out." for more details visit : http://www.freemeditation.ca/ http://www.sahajayoga.org Akhilesh Singh
modified on Wednesday, October 7, 2009 4:23 AM
-
Thanks Bro... your suggestion exactly matched what i needed and was more than i expected... thanks again... :)
"You just become, like a flower becomes the fruit. It's all built in within you. Allow it to work out." for more details visit : http://www.freemeditation.ca/ http://www.sahajayoga.org Akhilesh Singh
modified on Wednesday, October 7, 2009 4:23 AM