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
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