代理是一种特殊的,指向某个方法模块所在的地址。一般来讲,那个方法模块,可以是一个普通的方法,更多的时候,是一团匿名的lamda表达式,即一个匿名方法。现在简单理解一下代理的简写方式,即Action关键字。
class A
{
B b = new B();
public delegate string Show(string result);
public string Execute()
{
Show s = new Show(b.MyShow);
string str = s.Invoke("ttt");
return str;
}
}
class B
{
public string MyShow(string s)
{
return s + ">>>>>>>>>";
}
}
static void Main(string[] args)
{
A a = new A();
a.Execute();
}这样,使用A的时候,只改变B中MyShow的代码,就能定制A中Execute的执行结果。具有同样功能的代码,我们用Action类型来完成。 class C
{
D d = new D();
Action<string> action;
public void Execute()
{
action = d.MyShow2;
action.Invoke("ttt");
}
}
class D
{
public void MyShow2(string s)
{
Console.WriteLine(s + ">>>>>>>>>");
}
}
static void Main(string[] args)
{
A a = new A();
a.Execute();
}代理方法关键字Action与Fun的使用,布布扣,bubuko.com
原文:http://blog.csdn.net/sundacheng1989/article/details/31797231