|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
C# Casting following ReflectionI'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
Show quote
"Pete" <peter.hurf***@microcrest.com> wrote in message There's the problem. If the interface definied in each assembly, then it's 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. a different interface. Define the interface in a .dll, and have your main program and all the plug-ins reference that .dll. David [Removed non-existant group microsoft.public.dotnet.csharp.general]
Pete <peter.hurf***@microcrest.com> wrote: > I'm trying to make an application as extensible as possible, to allow <snip>> 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.) As David says, that's your problem. See http://www.pobox.com/~skeet/csharp/plugin.html for more information. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too |
|||||||||||||||||||||||