C# Casting following Reflection

P

Pete

Hi,

I'm trying to make an application as extensible as possible, to allow
new modules to be added in further down the line. The application
itself is an exe, the modules are classes within "Class Library" dlls.

On startup, the app reads a list of assemblies/classes to load, and
tries to load them up. I won't just accept any old class into the app -
the class must conform to a certain inferface (let's call it
IAddonModule). (The interface is just defined in a .cs file and is
shared between the main app and any add-on modules at the source code
level. (Obviously) the only methods of the module that will be used by
the application are those hanging off the interface.)

So, I wrote some code using reflection to piggy-back onto the Assembly
class. A combination of LoadFrom, GetType, GetInterface etc. etc. All
is fine until I actually create an instance. I can call to
Assembly.CreateInstance, and this works fine, except it brings me back
an object of type "object".

Now, it would make my main application code better if I could actually
refer to this instance as a type IAddonModule, rather than object.
(strong typing if nothing else).

But I'm having an awful time trying to cast the object popping out of
CreateInstance into a variable of type IAddonModule. If I just try to
call the interface's methods on the object, the compiler complains that
such-and-such a method is not defined (not unreasonable). However if I
just try to cast the object using "(IAddonModule)", I get a run time
Invalid Cast exception.

I'm baffled because I expected this to be really straightforward. Am I
missing something obvious?

TIA, Pete
 
D

David Browne

Pete said:
Hi,

I'm trying to make an application as extensible as possible, to allow
new modules to be added in further down the line. The application
itself is an exe, the modules are classes within "Class Library" dlls.

On startup, the app reads a list of assemblies/classes to load, and
tries to load them up. I won't just accept any old class into the app -
the class must conform to a certain inferface (let's call it
IAddonModule). (The interface is just defined in a .cs file and is
shared between the main app and any add-on modules at the source code
level.

There's the problem. If the interface definied in each assembly, then it's
a different interface. Define the interface in a .dll, and have your main
program and all the plug-ins reference that .dll.

David
 
J

Jon Skeet [C# MVP]

[Removed non-existant group microsoft.public.dotnet.csharp.general]

Pete said:
I'm trying to make an application as extensible as possible, to allow
new modules to be added in further down the line. The application
itself is an exe, the modules are classes within "Class Library" dlls.

On startup, the app reads a list of assemblies/classes to load, and
tries to load them up. I won't just accept any old class into the app -
the class must conform to a certain inferface (let's call it
IAddonModule). (The interface is just defined in a .cs file and is
shared between the main app and any add-on modules at the source code
level. (Obviously) the only methods of the module that will be used by
the application are those hanging off the interface.)

<snip>

As David says, that's your problem. See
http://www.pobox.com/~skeet/csharp/plugin.html for more information.
 
P

peter.hurford

Of course. As soon as I read your response, it made sense. Thanks
David.

Thanks for the article Bruce - no, I hadn't read it, but now I know!
 

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