samedi 25 février 2017

vb.net Array instance.SetValue for an array is not working

I'm trying to set nested-class array to work using Array.SetValue

I have a very long code for nested-class processing but i have a problem with array of a class (And i want to use reflection)

here is my code:

Private Function ReadStruct(Of T As {New, Class})()
        Dim structArray As New T
        Dim structArraynn As Object = structArray
        structArray = readclasses(structArraynn, br, offset, reverse, Nothing)
        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

A lot of codes...

Dim obj() 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(structme(MyInstance, br, offset, reverse, offset), i - 1)
Next
MsgBox(MyInstance2.GetValue(0).GetType().GetField("Test").GetValue(MyInstance))

And The classes are defind as the following

class m2
public mm as integer
end class

Class main
public int(1) as integer
public test(10) as m2
end class

but if i call msgbox line directly after MyInstance2.SetValue it returns the right value!

im using vs2013 with .net2 project





Aucun commentaire:

Enregistrer un commentaire