NetCore 委托Func的使用

@{
    ViewData["Title"] = "列表";
    var getUrl = ViewBag.GetUrl as Func<string, string, string>;
}

//Request.Query;
?rGroupId=1&rTypeId=1&page=3
<x:page
    x-route-rGroupId="@Model.rGroupId"
    x-route-rTypeId="@Model.rTypeId"  
></x:page> // <a href="?@getUrl("rGroupId", "1")">分组一</a> ?rGroupId=1&rTypeId=1&page=3 <a href="?@getUrl("rTypeId", "5")">分类五</a> ?rGroupId=1&rTypeId=5&page=3 <a href="?@getUrl("page", "3")">页3</a> ?rGroupId=1&rTypeId=1&page=3 //
public IActionResult List(ListQuery query)
{
  query.PageSize = 5;
  ListQuery list = _iListManager.Load(query);
  
ViewBag.GetUrl = new Func<string, string, string>(GetUrlDiy);
  return View(list);
}

//
private string GetUrlDiy(string key, string value) //rTypeId,5 { // Dictionary<string, string> listDiy = new Dictionary<string, string>(); var queryPar = Request.Query; //rGroupId|1,rTypeId|1,page|3 foreach (var item in queryPar) { listDiy[item.Key] = item.Value;//rGroupId|1,rTypeId|1,page|3 } listDiy[key] = value; //rGroupId|1,rTypeId|5,page|3 IEnumerable<string> arrPar = listDiy.Select(x => $"{x.Key}={x.Value}");//rGroupId=1,rTypeId=5,page=3 string urlPar = string.Join("&", arrPar); //?rGroupId=1&rTypeId=5&page=3 return urlPar; }

 

    // 摘要:
    //     封装一个具有一个参数并返回 TResult 参数指定的类型值的方法。
    //
    // 参数:
    //   arg:
    //     此委托封装的方法的参数。
    //
    // 类型参数:
    //   T:
    //     此委托封装的方法的参数类型。
    //
    //   TResult:
    //     此委托封装的方法的返回值类型。
    //
    // 返回结果:
    //     此委托封装的方法的返回值。
    [TypeForwardedFrom("System.Core, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089")]
    public delegate TResult Func<in T, out TResult>(T arg);

in T 代表输入参数                     1
out TResult 表示输出参数          2
再看返回值是 TResult                3
构造方法需要的参数是T               4

1与4,2与3进行对比,你发现了什么?!参数类型一样对吧。         5

Func是一个委托,委托里面可以存方法。

那我们就来建一个与之匹配的方法:以Func<int,bool>为例:

private bool IsNumberLessThen5(int number)

{return number < 5;} 

Func<int,bool> f1 = IsNUmberLessThen5;

调用:
bool flag= f1(4);


using
System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Func<int, bool> f1 = IsNumberLessThen5; bool flag = f1(4); Console.WriteLine(flag); //以下是其它的用法,与IsNumberLessThen5作用相同。只是写法不同而已。 Func<int, bool> f2 = i => i < 5; Func<int, bool> f3 = (i) => { return i < 5; }; Func<int, bool> f4 = delegate(int i) { return i < 5; }; flag = f2(4); Console.WriteLine(flag); flag = f3(4); Console.WriteLine(flag); flag = f4(4); Console.WriteLine(flag); Console.ReadLine(); } private static bool IsNumberLessThen5(int number) { if (number < 5) return true; return false; } } }

 

Func是有返回值的泛型委托

Func<int> 表示无参,返回值为int的委托

Func<object,string,int> 表示传入参数为object, string 返回值为int的委托

Func<object,string,int> 表示传入参数为object, string 返回值为int的委托

Func<T1,T2,,T3,int> 表示传入参数为T1,T2,,T3(泛型)返回值为int的委托

Func至少0个参数,至多16个参数,根据返回值泛型返回。必须有返回值,不可void

 

posted @ 2018-03-11 12:37  回到梦想  阅读(2263)  评论(0编辑  收藏  举报