-1

I'm looking for way to call Methods with Parameters using a Function Delegate.

You could use the function delegate in the place instead of calling processOperationB. but looking for any way that the below way can be achieved.

public class Client
{

    public AOutput OperationA (string param1, string param2)
    {
    //Some Operation 
    }

    public BOutput  OperationB(string param1, string param2)
    {
        //Some Operation 
    }
}


public class Manager 
{
    private Client cl;



    public Manager()
    {
        cl=new Client();
    }


    private void processOperationA(string param1, string param2)
    {

        var res = cl.OperationA(param1,param2); 
        //...   

    }

    private void processOperationB(string param1, string param2)
    {
        var res = cl.OperationB(param1,param2); 

        // trying to Call using the GetData , in that case I could get rid of individual menthods for processOperationA, processOperationB

        var res= GetData<BOutput>( x=> x.OperationB(param1,param2));
    }


    // It could have been done using Action, but it should return a value 
    private T GetData<T>(Func<Client,T> delegateMethod)
    {

    // how a Function delegate with params can be invoked 
    // Compiler expects the arguments to be passed here. But have already passed all params .

        delegateMethod();


    }

}
vonbalaji
  • 249
  • 2
  • 10
  • 2
    Ok, and what's the problem with that code? Are you getting an error? What error? – JLRishe Nov 05 '16 at 02:16
  • It is not clear what you are trying to do... Why would be the purpose of calling `GetData` as you can directly execute the code... – Phil1970 Nov 05 '16 at 03:26

1 Answers1

2

Your comment reads:

Compiler expects the arguments to be passed here

But that's not really true. Yes, it expects an argument, but not what you think it expects.

Your delegateMethod parameter is a Func<Client, T>, which means it requires a single argument, of type Client, and returns a value of type T. Based on the code you've shown, you should write this instead:

private T GetData<T>(Func<Client,T> delegateMethod)
{
    return delegateMethod(cl);
}

It is not clear to me what broader problem you're trying to solve is. I don't see the GetData<T>() method adding anything here; the callers could just call the appropriate "Operation..." method in each case, I'd think (i.e. as in your processOperationA() method).

But at least we can solve the compiler error. If you'd like help with that broader problem, you can post a new question. Make sure to include a good Minimal, Verifiable, and Complete code example that shows clearly what you're trying to do, and explain precisely what you've tried and what's not working.

Community
  • 1
  • 1
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136