1

I have a scenario in my code where i need to pass method as parameter to another methods which invokes.

My method has different parameters and return type also varies,

Public int Method1(int a, int b){
 return a+b;

}

public DataSet Method2(int a, string b, sting c, DataSet ds){
//make call to database and get results in dataset.
DataSet ds = new DataSet();
return ds;
}

The above methods needs to be call from separate method

public void InvokeMethod(Action action){
   action();
}

Action parameter can be method1 or method2 but the problem is how can i get return types which are different for method1 and method2.

if i use Func i will not be able to tell the number of input parameters at runtime.

Actually, I am try to call service operations through wrapper on wcf channel so that i can handle any exceptions of any call in that method...

for example : Channel.GetAllNames(int a,string b) is a actual call. this call should go through a generic method.called CallAllOperations.

public void CallAllOperations(Action action){
try{ 
action();
}
catch(exception e){
//catch exceptions of all calls instead of one call.
}
}
Deepak
  • 23
  • 2
  • 7
  • We miss a bit of context here. What are you trying to achieve? what would be the use case? – Bruno Belmondo Oct 13 '17 at 10:06
  • If you want to create different delegates on runtime, take a look on https://stackoverflow.com/a/9507589/5794617 – Artavazd Balayan Oct 13 '17 at 10:07
  • Ok, and suppose you create a list of 10 delegates. What are you going to do next? the answer much depends on that. – Bruno Belmondo Oct 13 '17 at 10:09
  • Actually, I am try to call service operations through wrapper on wcf channel so that i can handle any exceptions of any call in that method... for example : Channel.GetAllNames(int a,string b) is a actual call. this call should go through a generic method.called CallAllOperations. public void CallAllOperations(Action action){ try{ action(); } catch(exception e){ //catch exceptions of all calls instead of one call. } } – Deepak Oct 13 '17 at 10:14

1 Answers1

1

You can wrap your delegate in a lambda. For instance:

Suppose you create two delegate:

Func<DateTime> getTime = BuildGetTimeDelegate();
Func<int, int, int> getSum = BuildSumDelegate();

And now want to use them on specific data:

DateTime time;
int sum;
int a = 5;
int b = 10;

You can give your invoke method lambdas:

InvokeMethod(()=>time = getTime());
InvokeMethod(()=>sum = getSum(a,b));

This means you have to resolve input and output variable when converting your delegate into Action.

Bruno Belmondo
  • 2,299
  • 8
  • 18
  • how do i resolve input and return values, also what does invokemethod do..i mean where do i call it? – Deepak Oct 13 '17 at 10:27
  • InvokeMethod is your method so I would have expected that you knew where to call it... The same for input and output values. If you don't know, when you are doing the call nor the data to place inside, what is the purpose of your question? – Bruno Belmondo Oct 13 '17 at 10:30