Reflection Interface

G

Guest

Hi,

I'm trying to instantiate a class from a precompiled assembly .dll and cast it into a known Interface typ

// Load the assembl
Assembly externalAssembly = Assembly.LoadFrom("some.dll")

// Create The typ
Type externalType = externalAssembly.GetType("My.Created.UserClass")


//Create the objec
IView exView = (IView) Activator.CreateInstance(externalType)

My.Created.UserClass is the fully qualified name for the class, and it extends UserControl and implements IView interface in which I created. I was able to compile this fine but when I get "Specified Cast Not Valid" error in runtime

I need to do this because I have to run a Configure() method which is a member of the IView interface. I know I can make a MethodInfo and assign Configure to it, but I really dont want to because of design requirements. A weird thing I noticed is that the code runs if I instead cast the Activator.CreateInstance(..) into a UserControl Object

Thank you

Rizka Armadhan
 
M

Mattias Sjögren

Rizka,

Does some.dll and the calling assembly reference the *same* definition
of the IView interface?



Mattias
 
J

Jon Skeet [C# MVP]

m4rt14n said:
I'm trying to instantiate a class from a precompiled assembly .dll and
cast it into a known Interface type

My.Created.UserClass is the fully qualified name for the class, and it
extends UserControl and implements IView interface in which I created.
I was able to compile this fine but when I get "Specified Cast Not
Valid" error in runtime.

See http://www.pobox.com/~skeet/csharp/plugin.html - that explains the
most common cause of the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top