C#Lambda与通用类T

时间:2016-12-07 19:25:58

标签: c# generics lambda

我有一堆POCO课程,让我们说其中一个看起来像下面

class Person{
      public string Name {get;set;}
      public int Age {get;set;}
}

这是我的通用类,

class Helper<T> where T:class{

    public  Func<IQueryable<T>, IOrderedQueryable<T>> GenerateOrderByExpression(string colName)
    {
    return x => x.OrderByDescending(y => y.colName);
    //colName = NAME can be here for class person, it can be the Age field or something else for some other classes
    //how do i become generic here, how can I be able to use which ever field i pass as colName

    }

}

1 个答案:

答案 0 :(得分:-4)

您可以使用反射来检查colName是否存在并获取其值。查看MSDN页面 - https://msdn.microsoft.com/en-us/library/mt656691.aspx