如何设置自定义类

时间:2017-07-12 07:37:28

标签: c# asp.net arrays reflection

我如何设置我的阵列的第一行和第二行通过反射,我有 将我的值设置为我的自定义对象,但我无法将其分配给数组,因为第一行和第二行为空

var reflectedArray= Array.CreateInstance(classNameType, 2);

Object itemOfArrayTemp = Activator.CreateInstance(classNameType);

reflectedArray[0] = myValue1;

reflectedArray[1] = myValue2;

错误是:

  

无法将带有[]的索引应用于“Array”类型的表达式

2 个答案:

答案 0 :(得分:1)

Instead of using [], you can use the SetValue method:

reflectedArray.SetValue(myValue1, 0);
reflectedArray.SetValue(myValue2, 1);

Please note that the value is the first parameter, the index the second.

答案 1 :(得分:1)

You need to use the SetValue method on the reflectedArray

reflectedArray.SetValue(value1,0);
reflectedArray.SetValue(value2, 1);
相关问题