For Programmers: Free Programming Magazines  


Home > Archive > C# > October 2005 > C# Casting following Reflection









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author C# Casting following Reflection
Pete

2005-10-14, 6:58 pm

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

David Browne

2005-10-14, 6:58 pm


"Pete" <peter.hurford@microcrest.com> wrote in message
news:1129309875.172169.220410@o13g2000cwo.googlegroups.com...
> 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



Bruce Wood

2005-10-14, 9:56 pm

Have you read this article?

http://www.yoda.arachsys.com/csharp/plugin.html

peter.hurford@microcrest.com

2005-10-17, 7:56 am

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!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com