Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Reflection Casting (http://www.programmingforums.org/showthread.php?t=15455)

kruptof Mar 22nd, 2008 3:33 PM

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.

lectricpharaoh Mar 24th, 2008 6:01 AM

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?

kruptof Mar 24th, 2008 8:02 AM

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

lectricpharaoh Mar 24th, 2008 4:23 PM

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

  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.

kruptof Mar 25th, 2008 4:02 PM

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.

lectricpharaoh Mar 25th, 2008 10:00 PM

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

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

  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
:

  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.

kruptof Mar 26th, 2008 10:32 AM

Re: Reflection Casting
 
Quote:

Originally Posted by lectricpharaoh (Post 142965)
:

  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.

kruptof Mar 26th, 2008 1:10 PM

Re: Reflection Casting
 
1 Attachment(s)
I managed to recreate the problem, I created a new project and attached it to this post.

Dameon Mar 26th, 2008 2:31 PM

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.

kruptof Mar 26th, 2008 2:41 PM

Re: Reflection Casting
 
Quote:

Originally Posted by Dameon (Post 142993)
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 (Post 142993)
MyPlugin.dll
-References pluginbase.dll

How do I reference 'Pluginbase.dll' in MyPlugin.


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