Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 22nd, 2008, 3:33 PM   #1
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3 kruptof is on a distinguished road
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:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Mar 24th, 2008, 6:01 AM   #2
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,198
Rep Power: 5 lectricpharaoh will become famous soon enough
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
lectricpharaoh is offline   Reply With Quote
Old Mar 24th, 2008, 8:02 AM   #3
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3 kruptof is on a distinguished road
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:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Mar 24th, 2008, 4:23 PM   #4
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,198
Rep Power: 5 lectricpharaoh will become famous soon enough
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)
  1. if(plugin is IPlugin)
  2. // implements the interface
  3. else
  4. // oops
Perhaps include this kind of logic when you're registering the plugin.
__________________
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
lectricpharaoh is offline   Reply With Quote
Old Mar 25th, 2008, 4:02 PM   #5
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3 kruptof is on a distinguished road
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:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Mar 25th, 2008, 10:00 PM   #6
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,198
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: Reflection Casting

Quote:
Originally Posted by kruptof
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.
If you do this, I believe the compiler will raise an error if the interface isn't implemented (or more specifically, if the interface method you call isn't implemented). It may also raise an error for attempting the cast to the interface:
C# Syntax (Toggle Plain Text)
  1. // possible compile-time error
  2. IPlugin pluginObject = (IPlugin)someRandomObject;
  3.  
  4. // definite compile-time error if someRandomObject doesn't
  5. // contain 'someInterfaceMethod'
  6. (IPlugin)someRandomObject.someInterfaceMethod();
This is why you need to use reflection to invoke the method; it avoids the compile-time error. However, you can just specify that a plugin writer needs to implement your interface, and let them shoulder the responsibility. I mean, if they don't implement it properly, and your reflection and type testing causes the code to simply skip the method calls, the plugin is useless anyways. Why should you jump through hoops to ensure someone else's code compiles, when it won't work anyways?

This way, you can just have your interface:
C# Syntax (Toggle Plain Text)
  1. public interface IPlugin
  2. {
  3. void someInterfaceMethod();
  4. }
Then you tell the plugin writers to implement it. If they do, they can do stuff like
C# Syntax (Toggle Plain Text)
  1. IPlugin pluginObject = (IPlugin)someObject;
  2. pluginObject.someInterfaceMethod();
If they do their job, it works. If they don't, they get a compile-time error, and can fix their code. You get your dot notation, and everyone's happy.
__________________
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
lectricpharaoh is offline   Reply With Quote
Old Mar 26th, 2008, 10:32 AM   #7
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3 kruptof is on a distinguished road
Re: Reflection Casting

Quote:
Originally Posted by lectricpharaoh View Post
C# Syntax (Toggle Plain Text)
  1. IPlugin pluginObject = (IPlugin)someObject;
  2. pluginObject.someInterfaceMethod();
This is what I got the runtime error on, I will post the whole code when I get home. Though the code has changed by now, but I'll see if I can dig up an old copy.
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Mar 26th, 2008, 1:10 PM   #8
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3 kruptof is on a distinguished road
Re: Reflection Casting

I managed to recreate the problem, I created a new project and attached it to this post.
Attached Files
File Type: zip Test.zip (83.9 KB, 1 views)
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Mar 26th, 2008, 2:31 PM   #9
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Old Mar 26th, 2008, 2:41 PM   #10
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3 kruptof is on a distinguished road
Re: Reflection Casting

Quote:
Originally Posted by Dameon View Post
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.
Ahh, I didn't know that.

Quote:
Originally Posted by Dameon View Post
MyPlugin.dll
-References pluginbase.dll
How do I reference 'Pluginbase.dll' in MyPlugin.
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Do we need casting to match void * to char * kurt C++ 6 Jan 8th, 2008 7:48 PM
Casting signed int to unsigned int? null_ptr0 C++ 1 Nov 23rd, 2007 10:27 PM
Casting? iradic C++ 18 Dec 20th, 2006 7:20 AM
Casting a struct to s aimilar struct shoeyfighter C 6 Oct 27th, 2006 3:59 PM
Question regarding Malloc() and Type casting sparda C 6 Sep 29th, 2005 5:12 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC