简体   繁体   English

使用方法参数作为泛型类型参数

[英]Using method parameter as generic type argument

I have a class (SearchParameters) that holds search parameters, i then create a linq query based on these using a generic class called Querybuilder. 我有一个保存搜索参数的类(SearchParameters),然后使用一个称为Querybuilder的通用类基于这些参数创建linq查询。 This returns the results and all works perfectly. 这将返回结果,并且一切运行正常。

The results are displayed in GridView, i am currently implementing custom sorting for the gridivew, I add the field to be searched to the SearchParameters object (using a fluent interface) 结果显示在GridView中,我当前正在为gridivew实现自定义排序,我将要搜索的字段添加到SearchParameters对象(使用流畅的界面)

SearchParameters=SearchParameters.SortResultsBy(e.SortExpression,e.NewSortOrder.ToString());

I need the datatype of the columns to be used as a generic parameter to my AddOrderByClause() method: 我需要将列的数据类型用作我的AddOrderByClause()方法的通用参数:

    public void AddOrderByClause<D>(string field, Type sourceDateType)
    {
        var orderExpression = Expression.Lambda<Func<T, D>>(Expression.Property(resultExpression, field), resultExpression);

        rootExpression = Expression.Call(
              typeof(Queryable),
              "OrderBy",
              new Type[] { typeof(T), typeof(D) },
       rootExpression,
       orderExpression);

    }

I can easily find the data type of the columns, but how do i pass it to the AddOrderByClause() (generic parameter D)? 我可以轻松找到列的数据类型,但是如何将其传递给AddOrderByClause() (通用参数D)?

public void AddOrderByClause<D,E>(string field, E sourceDataType)
{
    .....
}

Use reflection to get the method AddOrderByClause , then use MakeGenericMethod to get the generic. 使用反射获取方法AddOrderByClause ,然后使用MakeGenericMethod获取泛型。 This is the general idea (a bit vague because I don't know what all your types are named). 这是一般想法(有点含糊,因为我不知道您所有的类型都被命名为什么)。

Type MyTypeParameter;    // TODO set this to type parameter D
Type type;               // TODO set this to the type that contains the method "AddOrderByClause"
MethodInfo method = type.GetMethod("AddOrderByClause");
MethodInfo genericMethod = method.MakeGenericMethod(typeof(MyTypeParameter));
genericMethod.Invoke(MyClassInstance, FieldParam, SourceDataParam);

but how do i pass it to the AddOrderByClause() (generic parameter D)? 但是如何将其传递给AddOrderByClause()(通用参数D)?

Seems like you already have datatype of the columns, and you figuring out how to pass it to Generic method 似乎您已经具有列的数据类型,并且想出如何将其传递给Generic方法

Below is example 以下是示例

First modify AddOrderByClause to accept T (used later in your function) 首先修改AddOrderByClause以接受T(稍后在您的函数中使用)

public void AddOrderByClause<D,T>(String field)
{
....
}

Then call AddOrderByClause like 然后像这样调用AddOrderByClause

 var data = SearchParameter;// Use actual data if SearchParameter is not correct
 AddOrderByClause<D,date.GetType()>(fieldData);// assuming fieldData is something different 
 var data = SearchParameter;// Use actual data if SearchParameter is not currect

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM