![]() |
Reflection Casting
Is it possible to cast a Class retrieved using Reflection to another type, Like it's parent class. At the moment whatever I do I can't seem to cast it to anything.
|
Re: Reflection Casting
I haven't really used reflection, but I'm fairly certain that the information is retrieved at run time from the assembly metadata; thus there is no way to do compile-time casting. You can check to see if object X is an instance of class Y (including a class derived from class Y), or if it implements interface Z. In most cases, this should be all you need.
What is it you're trying to do, exactly? Why do you want to determine the cast type at run time? |
Re: Reflection Casting
Well I am trying to my make program easy to develop for. So developers would be given an interface to implement. Once they implement that Interface my program would validate the plugin to see if it implements the interface. And if it does, cast it to the interface add it to a list of available plugins. But I can't seem to cast to that type. Not at my desk now, so i can't include the exact error.But it was something like cannot convert namespace.classA to interface.
I even tried to replace the interface and have the developers extend an abstract class and then check if the class was extended by the plugin at run time but I still get the same error |
Re: Reflection Casting
Ahh, so you don't need to determine the 'cast to' type at runtime. All you need to do is verify it implements the specified interface.
Say your interface is IPlugin and the programmer has an object plugin, which is an instance of their class that is supposed to implement the interface.. Something like this should work::
|
Re: Reflection Casting
yeah i don't need to cast the it to my interface, but i was doing out of laziness because i need to call three methods from the plugin and atm I get each method and store it in a MethodInfo object and invoke it using that. I just wanted to use the MyInterface dot method name notation, which would have been easier.
|
Re: Reflection Casting
Quote:
:
This way, you can just have your interface: :
:
|
Re: Reflection Casting
Quote:
|
Re: Reflection Casting
1 Attachment(s)
I managed to recreate the problem, I created a new project and attached it to this post.
|
Re: Reflection Casting
You just happen to have two identically declared interfaces in two different assemblies. But they are still considered different. The 'Station' interface you are casting to is from a different assembly than the 'Station' interface that is implemented.
I wrote a plugin loader for a project back in the 1.1 days. The change you need to make is simple enough. Example: PluginBase.dll -Contains just the interface definition MyPlugin.dll -References pluginbase.dll MyFancyPluginLoader.exe -Also references pluginbase.dl People that need to write plugins just need a copy of PluginBase.dll. No headers or such needed. Assembly metadata covers all of that. |
Re: Reflection Casting
Quote:
Quote:
|
| All times are GMT -5. The time now is 4:16 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC