<转>学习Action和Func的使用

  早晨闲着,看到关于Action优化代码结构的小文章,看到后感觉的却要比普通的if...else,或者switch...case可读性和可扩展性要好很多。

  这里借别人的资料,自己再认识认识.

  可能从最初编程的时候,条件判断会这样用:

View Code
public void DoSomeThing(string str)
{
if (str == "A") {
DoSomethingForA(str);
}
else if (str == "B") {
DoSomethingForB(str);
}
else if (str == "C") {
DoSomethingForC(str);
}
}

  不过有点认识的大都会选择使用switch来分支,毕竟这样要比if...else要好看不少:  

View Code
 public void DoSomeThing(string str)
{
switch (str)
{
case "A":
DoSomethingForA(str);
break;
case "B":
DoSomethingForB(str);
break;
case "C":
DoSomethingForC(str);
break;
default:
break;
}
}

  看过那兄弟的文章后,觉得使用Action将代码重构后,代码质量提高不少:  

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ActionFuncApp.Actions
{
class ActionMethods
{
private static Dictionary<string, Action<string>> dic = new Dictionary<string, Action<string>>();
///<summary>
/// 将方法加入字典
///</summary>
public ActionMethods()
{
dic.Add("A", DoSomethingForA);
dic.Add("B", DoSomethingForB);
dic.Add("C", DoSomethingForC);
dic.Add("D", DoSomethingForD);
}

public void DoSomethingForA(string str)
{
Console.WriteLine(str + "-->Call DoSomeThingForA");

}

public void DoSomethingForB(string str)
{
Console.WriteLine(str + "-->Call DoSomeThingForB");
}

public void DoSomethingForC(string str)
{
Console.WriteLine(str + "-->Call DoSomeThingForC");
}

public void DoSomethingForD(string str)
{
Console.WriteLine(str + "-->Call DoSomeThingForD");
}

///<summary>
/// 外部只要调用这个方法,传入参数就OK
///</summary>
///<param name="str"></param>
///<param name="strDo"></param>
public void DoSomething(string str, string strDo)
{
if (dic.ContainsKey(str))
{
Action<string> action = dic[str] as Action<string>;
//通过Action调用方法,这里前提是调用的参数类型相同!而且不带返回值
action.Invoke(strDo);
}
}
}
}

  调用的时候,需要New一个ActionMethods对象:  

View Code
 ActionFuncApp.Actions.ActionMethods actions = new Actions.ActionMethods();

actions.DoSomething("B", "BBBB");

Console.ReadLine();

  

  使用Action只能调用不带返回值的方法,如果有返回值,那么需要使用Func:  

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ActionFuncApp.Func
{
class FuncMethods
{
private static Dictionary<string, Func<string, string>> dic = new Dictionary<string, Func<string, string>>();

public FuncMethods()
{
dic.Add("A", DoSomethingForA);
dic.Add("B", DoSomethingForB);
dic.Add("C", DoSomethingForC);
dic.Add("D", DoSomethingForD);
}

public string DoSomethingForA(string str)
{
Console.WriteLine(str + "-->Call DoSomeThingForA");
return "Return For A";
}

public string DoSomethingForB(string str)
{
Console.WriteLine(str + "-->Call DoSomeThingForB");
return "Return For B";
}

public string DoSomethingForC(string str)
{
Console.WriteLine(str + "-->Call DoSomeThingForC");
return "Return For C";
}

public string DoSomethingForD(string str)
{
Console.WriteLine(str + "-->Call DoSomeThingForD");
return "Return For D";
}

public string DoSomething(string str, string strDo)
{
string strReturn = string.Empty;
if (dic.ContainsKey(str))
{
Func<string, string> action = dic[str] as Func<string, string>;
strReturn = action.Invoke(strDo);
}

return strReturn;
}
}
}

  执行和Action类似,实例一个Func对象:  

View Code
  ActionFuncApp.Func.FuncMethods funcs = new Func.FuncMethods();

Console.WriteLine(funcs.DoSomething("C", "CCCC"));

Console.ReadLine();

  原文地址:http://www.cnblogs.com/zhaohuayang/archive/2011/11/22/2259522.html

 

posted @ 2011-11-24 13:56  liver.wang  阅读(265)  评论(0编辑  收藏  举报