I am writing a C# application in which I need to load an assembly from a file and then create an instance of a specific type from that assembly. I'm using the following code to do this:
System.Reflection.Assembly
a = System.Reflection.Assembly
.LoadFile(
path);
object myObject = a.CreateInstance("MyType")
;
The problem is that I then want to interact wth myObject as an instance of MyType:
MyType myInstance = myObject as MyType;
After this line of code myInstance is null. The VS debugger Autos pane shows the value of myObject as {MyType} and shows its Type as object {MyType}. Nonetheless, I cannot successfully cast myObject as MyType. Am I doing something wrong? Is there a way around this? Thanks.
Start Free Trial