C# Func

时间:2021-05-18 16:02:28
    using System;

    namespace FuncDemo
{
internal class Program
{
private static void Main()
{
//类似委托功能
Func<InputArgs, Result> func = TsetFunction;

          System.Diagnostics.Debug.WriteLine("第一种方式:");

          System.Diagnostics.Debug.WriteLine(func(new InputArgs("zhangqs008", "123456")));
          System.Diagnostics.Debug.WriteLine("=============================================");
          System.Diagnostics.Debug.WriteLine("第二种方式:");

                CallMethod(func, new InputArgs("zhangqs008", "")); //或者
CallMethod(TsetFunction, new InputArgs("zhangqs008", "")); Console.Read();
} public static Result TsetFunction(InputArgs input)
{
Result result = new Result();
result.Flag = String.Compare("zhangqs008", input.UserName, StringComparison.OrdinalIgnoreCase) == &
String.Compare("", input.Password, StringComparison.OrdinalIgnoreCase) == ;
result.Msg = "当前调用者:" + input.UserName;
return result;
} public static void CallMethod<T>(Func<T, Result> func, T item)
{
Result result = func(item);
System.Diagnostics.Debug.WriteLine(result.ToString());
}
/// <summary>
/// 方法参数
/// </summary>
public class InputArgs
{
public InputArgs(string userName, string password)
{
UserName = userName;
Password = password;
} public string UserName { get; set; }
public string Password { get; set; }
} /// <summary>
/// 方法结果
/// </summary>
public class Result
{
public string Msg { get; set; }
public bool Flag { get; set; }
public override string ToString()
{
return string.Format("Flag:{0},Msg:{1}", Flag, Msg);
}
} }
}

http://blog.csdn.net/a497785609/article/details/8282785

http://msdn.microsoft.com/zh-cn/library/Bb534960(v=vs.110).aspx