我尝试使用Array.SetValue设置嵌套类数组
我有一个非常长的嵌套类处理代码,但我有一个类的数组有问题(我想使用反射)
这是我的代码:
Private Function ReadStruct(Of T As {New, Class})()
Dim structArray As New T
Dim structArraynn As Object = structArray
structArray = readclasses(structArraynn)
Return structArray
End Function
Private Function readclasses(ByVal cls as object) as object
Dim obj as object = cls
For Each f As System.Reflection.FieldInfo In cls.GetType().GetFields
很多代码......
Dim obj2() As Object = f.GetValue(obj)
Dim MyInstance As Object = Activator.CreateInstance(Type.GetType(f.FieldType.ToString().Replace("[]", "")))
Dim MyInstance2 As Array = Array.CreateInstance(Type.GetType(f.FieldType.ToString().Replace("[]", "")), obj.Length)
For i As Integer = 1 To obj.Length
MyInstance2.SetValue(readclasses(MyInstance), i - 1)
Next
MsgBox(MyInstance2.GetValue(0).GetType().GetField("Test").GetValue(MyInstance))
并且这些类定义为以下
class m2
public mm as integer
end class
Class main2
public int(1) as integer
public test(10) as m2
end class
但是如果我在MyInstance2.SetValue之后直接调用msgbox行,它会返回正确的值!
我正在使用vs2013和.net2项目
编辑:事实证明所有测试数组元素都具有相同的值(?为什么?)