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:
// possible compile-time error
IPlugin pluginObject = (IPlugin)someRandomObject;
// definite compile-time error if someRandomObject doesn't
// contain 'someInterfaceMethod'
(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:
public interface IPlugin
{
void someInterfaceMethod();
}
Then you tell the plugin writers to implement it. If they do, they can do stuff like
IPlugin pluginObject = (IPlugin)someObject;
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.