标签:blog 使用 io 2014 art cti 代码 div
代理是一种特殊的,指向某个方法模块所在的地址。一般来讲,那个方法模块,能够是一个普通的方法,很多其它的时候,是一团匿名的lamda表达式,即一个匿名方法。如今简单理解一下代理的简写方式,即Actionkeyword。
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();
}代理方法keywordAction与Fun的使用,布布扣,bubuko.com
标签:blog 使用 io 2014 art cti 代码 div
原文地址:http://www.cnblogs.com/hrhguanli/p/3872758.html