I'm currently writing a program in vb.net 2008, that is split into the main program and several DLLs (of which the user can select 1 to use). In the DLL, I have a function that reads data from a filestream into a structure, then returns the structure. While trying to assign the returned information to a variable in the main program, I get "Unable to cast object of type Vertex[] to type Vertex[]". Here's a snippet of the code from both the DLL and the main program:

Main Program:

Public Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    ModelLibrary = Assembly.LoadFrom("C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\ShardM2\ShardM2\bin\Debug\ShardM2.dll")
    'ModelLibrary = Assembly.LoadFrom("ShardM2.dll")
    AddHandler Application.Idle, AddressOf Application_Idle
End Sub

Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
    Dim shard As Type
    shard = ModelLibrary.GetType("ModelLibrary.Shard")
    method = shard.GetMethod("Open")
    Dim o As Object
    o = Activator.CreateInstance(shard)
    Dim model As New FileStream("C:\Documents and Settings\Administrator\Desktop\WotLK M2\1HTrollSpear01.m2", FileMode.Open)
    Dim oo As Object() = {model}
    Dim VertArray() As Vertex = method.Invoke(o, oo)
End Sub


Public Function Open(ByVal model As FileStream) As Vertex()
        ' Start here
        Dim FileHeader As HEADER        ' Stucture variable
        Dim VertArray() As Vertex = {}
        Dim ByteArray() As Byte = {}
        Array.Resize(ByteArray, 304)
        model.Read(ByteArray, 0, 304)
        ' convert to bytes
        ' Call BuildStr method to create structure and copy byte array data to structure 
        FileHeader = BuildStruct(ByteArray, FileHeader.GetType)

        ' Vertices
        Array.Resize(VertArray, FileHeader.nVertices)       ' Declaring temporary vertices variable and setting its size
        model.Seek(FileHeader.ofsVertices, SeekOrigin.Begin)     ' Set filestream position to start of vertex data
        Dim i As Integer = 0
        Dim vert As M2VERTEX      ' temporary vertex structure variable
        Dim vertbytes() As Byte = {}        ' temporary vertex byte array.
        Array.Resize(vertbytes, 48)

        While i < FileHeader.nVertices
            model.Read(vertbytes, 0, 48)        ' reading a single vertex into the temp byte array
            vert = BuildStruct(vertbytes, vert.GetType)     ' converting the array to a VERTEX structure
            VertArray(i).Position.X = vert.pos.X
            VertArray(i).Position.Y = vert.pos.Y
            VertArray(i).Position.Z = vert.pos.Z
            VertArray(i).Normal.X = vert.normal.X
            VertArray(i).Normal.Y = vert.normal.Y
            VertArray(i).Normal.Z = vert.normal.Z
            VertArray(i).TexCoord.X = vert.texcoords.X
            VertArray(i).TexCoord.Y = vert.texcoords.Y
            VertArray(i).Weight0 = vert.weights.Weight0
            VertArray(i).Weight1 = vert.weights.Weight1
            VertArray(i).Weight2 = vert.weights.Weight2
            VertArray(i).Weight3 = vert.weights.Weight3
            VertArray(i).Bone0 = vert.bones.Bone0
            VertArray(i).Bone1 = vert.bones.Bone1
            VertArray(i).Bone2 = vert.bones.Bone2
            VertArray(i).Bone3 = vert.bones.Bone3
            i += 1
        End While
        Return (VertArray)
    End Function

Line 19 in the Main Program code I posted is the line that errors. Both the main program AND the DLL have the Vertex structure defined exactly the same, so I don't quite get why it's not casting... Anyways, thanks in advance for any help :)

P.S. Incase you couldn't tell from the code, I'm using reflection to load the DLL.

In fact you have two different types. One Vertex type in your DLL namespace and another Vertex type in your main program's namespace. Since those are different types you can't do direct assignment.

One way to get around "different types" problem could be following:

Dim VertObj As Object = DirectCast(method.Invoke(o, oo), Object)
Dim VertArray() As Vertex = CType(VertObject, Vertex())

Hmm, I'm still getting the same error :S The strange thing is, when I look at the exception details, TargetSite -> Return Type = {Name = "Void" FullName = "System.Void"}
>_O I know that VertArray is not null, so is it an issue that occurs when trying to return a structure object using method.invoke?

P.S. Thanks for taking the time to try and help :)

No, that's not the problem. After Dim VertObj As Object = DirectCast(method.Invoke(o, oo), Object) VertObj contains a System.Array. For the System.Array instances, IsArray returns False and GetElementType return Nothing, that's why Return Type = {Name = "Void" FullName = "System.Void"} when you/I tried to do Dim VertArray() As Vertex = CType(VertObj, Vertex()) So, you do have the result array in VertObj at this point. Now the question is, how to convert System.Array (i.e. VertObj) to Vertex array (i.e. VertArray())?

I was a bit slobby, because I didn't notice that System.Array type thing. I did some googling, how to convert System.Array but found nothing that worked.

Since the problem was originally two "different" Vertex types, maybe you should create a "base" Vertex type from which the actual Vertex types in DLLs and your main app are derived. That's all I can say at this point. Sorry :(

Thanks for all your help :) I was thinking of trying it that way (1 structure that is shared between the two). I'll see what I can do. Again, thank you for all your help.

This article has been dead for over six months. Start a new discussion instead.