How do i I开发者_开发问答nvoke items so the TestAction do write out "s.Hello"? Right now i don't do anything, it jumps over the "action = s.." line.
Or is the another way to do this? Since i don't want to return any code i use the Action instead of Func
I just started to work with Action.
public class Items
{
public string Hello { get; set; }
}
public class TestClass
{
public void TestAction(Action<Items> action)
{
action = s => Console.WriteLine(s.Hello);
}
public TestClass()
{
TestAction(b => b.Hello = "Hello world!");
}
}
Let's drill down your code, from the bottom of the stacktrace.
TestAction(b => b.Hello = "Hello world!");
You are supplying a lambda that assigns b.Hello
as "Hello World".
action = s => Console.WriteLine(s.Hello);
You are assigning that same delegate a new lambda.
You aren't actually doing anything with them - you are just generating a delegate. To execute that delegate, you need an argument of class Items
. What you really want is to call the action with such an argument.
public class TestClass
{
public void TestAction(Action<Items> action)
{
Items i = new Item() { Hello = "Hello World");
action(i);
}
public TestClass()
{
TestAction(b => Console.WriteLine(b.Hello));
}
}
精彩评论