Получить общий метод без использования GetMethods

Я хочу получить метод 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.

Ответ 1

Решено (путем взлома 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

EDIT: вот как получить метод, если вы не знаете тип во время компиляции:

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 (WhateverClassYouDeclareTheseMethodsIn).

Ответ 2

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

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();
    }
}

Ответ 3

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

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

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

Ответ 5

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();

Ответ 7

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

public static MethodInfo GetOrderByMethod<TElement, TSortKey>() {
    IEnumerable<TElement> col = null;
    return new Func<Func<TElement, TSortKey>, IOrderedEnumerable<TElement>>(col.OrderBy).Method;
}

Ответ 8

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

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
}

Ответ 9

Еще один комментарий (это должно быть, но с его слишком долгого, я должен опубликовать его как ответ), следуя за ответом @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