Question

I have the following delegation situation:

string myVal = "test";
Step(x => MethodToInvoke(myVal));


private T Step<T>(Func<string, T> method){
    str = ;//Where do I get the string myVal from???
    return method.Invoke(str);
}

private string MethodToInvoke(string str){
   return str + "_invoked"
}

how do I get inside method Step the param that has been sent? (string myVal = "test") I want before method.Invoke to make some logic and after the logic do the method.Invoke.

Is it possible? Am I missing something here? Thanks

Was it helpful?

Solution 2

This would enough to give the desired output:

string myVal = "test";
Step(x => MethodToInvoke(x), myVal);

private T Step<T>(Func<string, T> method, string str)
{
  return method.Invoke(str);
}

private string MethodToInvoke(string str)
{
   return str + "_invoked";
}

OTHER TIPS

Let's break this down.

Your Func Func<string, T> method is expecting a function which accepts a string parameter and returns a T.

When you initialise that Func<string, T> with x => MethodToInvoke(myVal) you are saying:

"Here's a function with a parameter called x which is implemented by calling MethodToInvoke(myVal), and the return type is whatever type MethodToInvoke() returns."

Although that function has a parameter called x, you are NOT actually using it on the right of the => because you are using myVal instead.

To fix it, use the x like so (and also add myVal as an extra parameter):

Step(x => MethodToInvoke(x), myVal);

Which can be simplified to:

Step(MethodToInvoke, myVal);

Then you will need to change your Step() method to take an additional string parameter and pass it to the Func that is also passed to Step():

T Step<T>(Func<string, T> method, string str)
{
    return method.Invoke(str);
}

Which can also be simplified to:

T Step<T>(Func<string, T> method, string str)
{
    return method(str);
}

There string hasn't been sent to the Step method. It's only used by the lambda expression that is sent to the Step method, and the lambda expression ignores the input variable, so it wouldn't help if you do anything to the string before calling it, it would still use the original string and ignore what you send to it.

Start by making the lambda expression use the input parameter instead of the variable, and then send the string along to the Step method:

Step(x => MethodToInvoke(x), "test");

private T Step<T>(Func<string, T> method, string value){
  str = value; // do whatever you like with the string
  return method(str);
}

Well, as the MethodToInvoke method already uses the input parameter, you don't really need the lambda expression at all, you can just make a delegate out of the method:

Step(MethodToInvoke, "test");

I think what you want is the following:

string myVal = "test";
Step(myVal, x => methodToInvoke(x));

private T Step<T>(string myVal, Func<string, T> method){
    str = myVal;//Where do I get the string myVal from???
    return method.Invoke(str);
}

private string MethodToInvoke(string str){
   return str + "_invoked"
}

You need to pass the myVal variable to the Step method, and then inside the StepMethod pass it to your Func.

The lambda expression you specified, just accesses myVal. If you want to pass a delegate with argument to a method do something like:

public void foo(Func<string, int> myFunc, string arg)
{
    int result = myFunc(arg);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top