![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3
![]() |
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.
__________________
Quote:
|
|
|
|
|
|
|
#2 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,031
Rep Power: 5
![]() |
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?
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#3 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3
![]() |
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
__________________
Quote:
|
|
|
|
|
|
|
#4 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,031
Rep Power: 5
![]() |
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: C# Syntax (Toggle Plain Text)
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#5 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3
![]() |
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.
__________________
Quote:
|
|
|
|
|
|
|
#6 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,031
Rep Power: 5
![]() |
Re: Reflection Casting
Quote:
C# Syntax (Toggle Plain Text)
This way, you can just have your interface: C# Syntax (Toggle Plain Text)
C# Syntax (Toggle Plain Text)
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
|
#7 | ||
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3
![]() |
Re: Reflection Casting
Quote:
__________________
Quote:
|
||
|
|
|
|
|
#8 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3
![]() |
Re: Reflection Casting
I managed to recreate the problem, I created a new project and attached it to this post.
__________________
Quote:
|
|
|
|
|
|
|
#9 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
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.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#10 | ||
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3
![]() |
Re: Reflection Casting
Quote:
How do I reference 'Pluginbase.dll' in MyPlugin.
__________________
Quote:
|
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Do we need casting to match void * to char * | kurt | C++ | 6 | Jan 8th, 2008 6:48 PM |
| Casting signed int to unsigned int? | null_ptr0 | C++ | 1 | Nov 23rd, 2007 9:27 PM |
| Casting? | iradic | C++ | 18 | Dec 20th, 2006 6:20 AM |
| Casting a struct to s aimilar struct | shoeyfighter | C | 6 | Oct 27th, 2006 2:59 PM |
| Question regarding Malloc() and Type casting | sparda | C | 6 | Sep 29th, 2005 4:12 AM |