Получить универсальный метод без использования GetMethods - PullRequest
26 голосов
/ 06 ноября 2008

Я хочу получить метод System.Linq.Queryable.OrderyBy<T, TKey>(the IQueryable<T> source, Expression<Func<T,TKey>> keySelector) метод, но я все время получаю нули.

var type = typeof(T);
var propertyInfo = type.GetProperty(group.PropertyName);
var propertyType = propertyInfo.PropertyType;

var sorterType = typeof(Func<,>).MakeGenericType(type, propertyType);
var expressionType = typeof(Expression<>).MakeGenericType(sorterType);

var queryType = typeof(IQueryable<T>);

var orderBy = typeof(System.Linq.Queryable).GetMethod("OrderBy", new[] { queryType, expressionType }); /// is always null.

У кого-нибудь есть понимание? Я бы предпочел не перебирать результат GetMethods.

Ответы [ 9 ]

21 голосов
/ 17 декабря 2008

Решено (взломом LINQ)!

Я видел ваш вопрос, исследуя ту же проблему. После того, как я не нашел хорошего решения, у меня появилась идея взглянуть на дерево выражений LINQ. Вот что я придумал:

public static MethodInfo GetOrderByMethod<TElement, TSortKey>()
{
    Func<TElement, TSortKey> fakeKeySelector = element => default(TSortKey);

    Expression<Func<IEnumerable<TElement>, IOrderedEnumerable<TElement>>> lamda
        = list => list.OrderBy(fakeKeySelector);

    return (lamda.Body as MethodCallExpression).Method;
}

static void Main(string[] args)
{
    List<int> ints = new List<int>() { 9, 10, 3 };
    MethodInfo mi = GetOrderByMethod<int, string>();           
    Func<int,string> keySelector = i => i.ToString();
    IEnumerable<int> sortedList = mi.Invoke(null, new object[] { ints, 
                                                                 keySelector }
                                           ) as IEnumerable<int>;

    foreach (int i in sortedList)
    {
        Console.WriteLine(i);
    }
}

выход: 10 3 9

РЕДАКТИРОВАТЬ: Вот как получить метод, если вы не знаете тип во время компиляции:

public static MethodInfo GetOrderByMethod(Type elementType, Type sortKeyType)
{
    MethodInfo mi = typeof(Program).GetMethod("GetOrderByMethod", Type.EmptyTypes);

    var getOrderByMethod = mi.MakeGenericMethod(new Type[] { elementType,
                                                             sortKeyType });
    return getOrderByMethod.Invoke(null, new object[] { }) as MethodInfo;
}

Обязательно замените typeof (Program) на typeof (Wh независимоClassYouDeclareTheseMethodsIn).

12 голосов
/ 06 ноября 2008

Вариант вашего решения, как метод расширения:

public static class TypeExtensions
{
    private static readonly Func<MethodInfo, IEnumerable<Type>> ParameterTypeProjection = 
        method => method.GetParameters()
                        .Select(p => p.ParameterType.GetGenericTypeDefinition());

    public static MethodInfo GetGenericMethod(this Type type, string name, params Type[] parameterTypes)
    {
        return (from method in type.GetMethods()
                where method.Name == name
                where parameterTypes.SequenceEqual(ParameterTypeProjection(method))
                select method).SingleOrDefault();
    }
}
5 голосов
/ 02 сентября 2010

Я думаю, что следующий метод расширения будет решением проблемы:

public static MethodInfo GetGenericMethod(
  this Type type, string name, Type[] generic_type_args, Type[] param_types, bool complain = true)
{
  foreach (MethodInfo m in type.GetMethods())
    if (m.Name == name)
    {
      ParameterInfo[] pa = m.GetParameters();
      if (pa.Length == param_types.Length)
      {
        MethodInfo c = m.MakeGenericMethod(generic_type_args);
        if (c.GetParameters().Select(p => p.ParameterType).SequenceEqual(param_types))
          return c;
      }
    }
  if (complain)
    throw new Exception("Could not find a method matching the signature " + type + "." + name +
      "<" + String.Join(", ", generic_type_args.AsEnumerable()) + ">" +
      "(" + String.Join(", ", param_types.AsEnumerable()) + ").");
  return null;
}

Вызов будет выглядеть примерно так (просто изменив последнюю строку вашего исходного кода):

var type = typeof(T);  
var propertyInfo = type.GetProperty(group.PropertyName);  
var propertyType = propertyInfo.PropertyType;  

var sorterType = typeof(Func<,>).MakeGenericType(type, propertyType);  
var expressionType = typeof(Expression<>).MakeGenericType(sorterType);  

var queryType = typeof(IQueryable<T>);  

var orderBy = typeof(Queryable).GetGenericMethod("OrderBy",
                                                 new Type[] { type, propertyType },
                                                 new[] { queryType, expressionType });

Чем отличаются другие решения: результирующий метод точно соответствует типам параметров, а не только их базовым типам.

4 голосов
/ 06 ноября 2008

Я не верю, что есть простой способ сделать это - это, по сути, недостающая особенность рефлексии, IIRC. Вы должны пройтись по методам, чтобы найти тот, который вам нужен: (

2 голосов
/ 06 ноября 2008
var orderBy =
        (from methodInfo in typeof(System.Linq.Queryable).GetMethods()
         where methodInfo.Name == "OrderBy"
         let parameterInfo = methodInfo.GetParameters()
         where parameterInfo.Length == 2
         && parameterInfo[0].ParameterType.GetGenericTypeDefinition() == typeof(IQueryable<>)
         && parameterInfo[1].ParameterType.GetGenericTypeDefinition() == typeof(Expression<>)
         select
            methodInfo
        ).Single();
1 голос
/ 29 марта 2013

Если вы знаете типы во время компиляции, вы можете сделать это с меньшим количеством кода, не используя тип Expression или вообще не зависеть от Linq, например:

public static MethodInfo GetOrderByMethod<TElement, TSortKey>() {
    IEnumerable<TElement> col = null;
    return new Func<Func<TElement, TSortKey>, IOrderedEnumerable<TElement>>(col.OrderBy).Method;
}
1 голос
/ 11 августа 2010

Используя лямбда-выражения, вы можете легко получить универсальный метод

    var method = type.GetGenericMethod
            (c => c.Validate((IValidator<object>)this, o, action));

Подробнее об этом здесь:

http://www.nerdington.com/2010/08/calling-generic-method-without-magic.html

http://web.archive.org/web/20100911074123/http://www.nerdington.com/2010/08/calling-generic-method-without-magic.html

0 голосов
/ 21 октября 2013

Просто еще один комментарий (он должен быть, но так как он слишком длинный, я должен опубликовать его как ответ) после ответа @NeilWhitaker -s (здесь используется Enumerable.Count), поскольку мы находимся в процессе очистки струны :) почему бы не использовать деревья выражений в вашем методе bytype? Что-то вроде:

    #region Count
    /// <summary>
    /// gets the 
    /// public static int Count&lt;TSource>(this IEnumerable&lt;TSource> source);
    /// methodinfo
    /// </summary>
    /// <typeparam name="TSource">type of the elements</typeparam>
    /// <returns></returns>
    public static MethodInfo GetCountMethod<TSource>()
    {
        Expression<Func<IEnumerable<TSource>, int>> lamda = list => list.Count();
        return (lamda.Body as MethodCallExpression).Method;
    }

    /// <summary>
    /// gets the 
    /// public static int Count&lt;TSource>(this IEnumerable&lt;TSource> source);
    /// methodinfo
    /// </summary>
    /// <param name="elementType">type of the elements</param>
    /// <returns></returns>
    public static MethodInfo GetCountMethodByType(Type elementType)
    {
        // to get the method name, we use lambdas too
        Expression<Action> methodNamer = () => GetCountMethod<object>();
        var gmi = ((MethodCallExpression)methodNamer.Body).Method.GetGenericMethodDefinition();
        var mi = gmi.MakeGenericMethod(new Type[] { elementType });
        return mi.Invoke(null, new object[] { }) as MethodInfo;
    }
    #endregion Disctinct
0 голосов
/ 25 августа 2012

Я думаю, что это может быть сделано с классом так:

public static class SortingUtilities<T, TProperty>
{
    public static IOrderedQueryable<T> ApplyOrderBy(IQueryable<T> query, Expression<Func<T, TProperty>> selector)
    {
        return query.OrderBy(selector);
    }


    public static IOrderedQueryable<T> ApplyOrderByDescending(IQueryable<T> query, Expression<Func<T, TProperty>> selector)
    {
        return query.OrderByDescending(selector);
    }

    public static IQueryable<T> Preload(IQueryable<T> query, Expression<Func<T, TProperty>> selector)
    {
        return query.Include(selector);
    }
}

И вы можете использовать это даже так:

public class SortingOption<T> where T: class
{
    private MethodInfo ascendingMethod;
    private MethodInfo descendingMethod;
    private LambdaExpression lambda;
    public string Name { get; private set; }

    public SortDirection DefaultDirection { get; private set; }

    public bool ApplyByDefault { get; private set; }

    public SortingOption(PropertyInfo targetProperty, SortableAttribute options)
    {
        Name = targetProperty.Name;
        DefaultDirection = options.Direction;
        ApplyByDefault = options.IsDefault;
        var utilitiesClass = typeof(SortingUtilities<,>).MakeGenericType(typeof(T), targetProperty.PropertyType);
        ascendingMethod = utilitiesClass.GetMethod("ApplyOrderBy", BindingFlags.Static | BindingFlags.Public | BindingFlags.IgnoreCase);
        descendingMethod = utilitiesClass.GetMethod("ApplyOrderByDescending", BindingFlags.Static | BindingFlags.Public | BindingFlags.IgnoreCase);
        var param = Expression.Parameter(typeof(T));
        var getter = Expression.MakeMemberAccess(param, targetProperty);
        lambda = Expression.Lambda(typeof(Func<,>).MakeGenericType(typeof(T), targetProperty.PropertyType), getter, param);
    }

    public IQueryable<T> Apply(IQueryable<T> query, SortDirection? direction = null)
    {
        var dir = direction.HasValue ? direction.Value : DefaultDirection;
        var method = dir == SortDirection.Ascending ? ascendingMethod : descendingMethod;
        return (IQueryable<T>)method.Invoke(null, new object[] { query, lambda });
    }
}

с таким атрибутом:

public class SortableAttribute : Attribute 
{
    public SortDirection Direction { get; set; }
    public bool IsDefault { get; set; }
}

и это перечисление:

public enum SortDirection
{
    Ascending,
    Descending
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...