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")
GLView1.MakeCurrent()
OpenTK.Graphics.GL.ClearColor(Color.SkyBlue)
SetupViewport()
AddHandler Application.Idle, AddressOf Application_Idle
sw.Start()
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
DLL:
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.