I've run into an issue where I need to load a class into my code using reflection, create an array of that class, and then assign the array to another reflected object's property.
My minimum example would be:
My DLL:
Public Class MyItem
Public Property Id As Int32
End Class
Public Class MyMainItem
Public Property MyItems As MyItem()
End Class
My Test App:
Sub Main()
Dim myAssembly As Assembly = Assembly.LoadFile("c:\temp\MyDll.dll")
Dim myMainObject As Object = Activator.CreateInstance(myAssembly.GetType("MyDll.MyMainItem"))
Dim myItemArray(1) As Object
myItemArray(0) = Activator.CreateInstance(myAssembly.GetType("MyDll.MyItem"))
myItemArray(1) = Activator.CreateInstance(myAssembly.GetType("MyDll.MyItem"))
myMainObject.MyItems = myItemArray
End Sub
The error I receive is InvalidCastException when assigning myItemArray to MyItems:
Conversion from type 'Object()' to type 'MyItem()' is not valid.
Is there a way to make myItemArray be understood as an array of MyItem rather than Object?
Aucun commentaire:
Enregistrer un commentaire