views:

134

answers:

3

I have a data driven mapping application where I need to implement custom functions as plugins. The name of the custom method that I need to execute will also be in the mapping data. I know that I can call the method using the invoke command; but, how can I ensure that each method has the appropriate signature?

+1  A: 

First of all I don't think you should allow arbitrary names for the overridden methods - just define an interface and keep it simple.

You could define an interface with a method that returns a delegate to the method that does the work.

Otherwise, you will just have to use reflection to get the MethodInfo for the mapped method and check at runtime that it hasd the correct signature.

Matt Howells
The names are not arbitrary. An in-house developer will create the custom functions and put the names in the database so that our users and apply them to a particular map. When my service is applying the map it will need to call the custom function to complete the task. I am trying to avoid recompiling the application every time a new custom function is needed. Frankly, reflection/interfaces/delegates are a bit more complicated than I am used to. So in the interim I'm probaby going to go with early binding. But I need to know how to do this in a more expandable manner.
Angela
Interfaces aren't complicated at all, and any beginner's C# book will teach you how to use them.
Matt Howells
The 'custom functions' - are they SQL, or C#, or what? Your service is C#?
Matt Howells
Yes the functions are C# and the service is C#.
Angela
+1  A: 

Typically, when developing a plugin architecture, you define an interface with the methods that you will invoke on the plugin. The plugin is required to implement the interface. When you load it you cast it as the interface (if it doesn't implement it, this will fail) and use it in your code as the interface. If the behavior of the plugin is more generic, you simply define your interface more generically, for example using configuration to establish the plugin's parameters and then using a simple method without arguments to invoke the plugin's functionality. I think you'll find it easier to work with a pre-defined interface and it shouldn't limit you too much.

tvanfosson
+1  A: 

You will have to use reflection.

You will first have to call the GetMethod() method in order to get the MethodInfo object for the method in question.

Then you need to use the .GetParameters() method to get hold of the parameters to the method, then you need to compare those against what you expect the method to have.

Lasse V. Karlsen