1

我的目标是创建一个SortRecords接受 anIEnumerable<T>和 aPropertyInfo作为参数的方法。这IEnumerable<T>是一个记录列表。PropertyInfo是 的属性T。调用时,SortRecordsEnumerable.SortBy<T, typeof Property>使用 调用该方法x => x.Property。注意这里Enumerable.SortBy有两个通用参数。此外,反射不能在 lambda 表达式中使用,因为 (a) 它很慢并且 (b) 它不适用于实体框架。

我已经编写了一些代码,但我一直看到错误消息Operation could destabilize the runtime。这是我的代码的样子

for (int i = 0; i < NumberOfSorts; i++)
        {
            string propertyName = PropertyNames[ColumnSortOrder[i]];
            PropertyInfo property = typeof(T).GetProperties().Single(p => p.Name == propertyName);              

            Func<IEnumerable<T>, PropertyInfo, IEnumerable<T>> sortingFunction = GetFunctionToSortRecords<T>(filteredRecords, property);
            sortedRecords = GetFunctionToSortRecords<T>(filteredRecords, property)(filteredRecords, property);
         }

结束第一个代码片段

方法定义如下

delegate IEnumerable<T> GetFunctionToSortRecordsDelegate<T>(IEnumerable<T> records, PropertyInfo propertyToSortOn);
public static Func<IEnumerable<T>, PropertyInfo, IEnumerable<T>> GetFunctionToSortRecords<T>(IEnumerable<T> records, PropertyInfo propertyToSortOn)
    {
        Type propertyType = propertyToSortOn.GetType();

        DynamicMethod method = new DynamicMethod("SortRecords", typeof(IEnumerable<T>), new Type[] { typeof(IEnumerable<T>), typeof(PropertyInfo) });            
        ILGenerator generator = method.GetILGenerator();            

        MethodInfo GetPropertyValue = propertyToSortOn.GetGetMethod();
        MethodInfo GetDefaultKeySelectorForProperty = typeof(DataTablesSorting).GetMethod("GetDefaultKeySelectorForProperty")                                                                                                         
            .MakeGenericMethod(new Type[] {typeof(T), propertyToSortOn.PropertyType });            

        MethodInfo EnumerableOrderBy = typeof(Enumerable).GetMethods()
            .Single(m => m.Name == "OrderBy" && m.GetParameters().Count()==3);

        // Get the default key selector for the property passed in.            
        generator.Emit(OpCodes.Ldarg_1); // property
        generator.Emit(OpCodes.Call, GetDefaultKeySelectorForProperty);

        // Save the default key selector at location 0
        generator.Emit(OpCodes.Stloc_0);

        generator.Emit(OpCodes.Ldarg_0); // records
        generator.Emit(OpCodes.Ldloc_0); // default key selector
        generator.Emit(OpCodes.Call, EnumerableOrderBy);
        generator.Emit(OpCodes.Ret);

        return ((GetFunctionToSortRecordsDelegate<T>)(method.CreateDelegate(typeof(GetFunctionToSortRecordsDelegate<T>)))).Invoke;
    }

    delegate TKey GetDefaultKeySelectorForPropertyDelegate<T, TKey>(T t);
    public static Func<T, TKey> GetDefaultKeySelectorForProperty<T, TKey>(PropertyInfo property)
    {
        DynamicMethod method = new DynamicMethod("GetKeySelector", typeof(TKey), new Type[] { typeof(T) });
        ILGenerator generator = method.GetILGenerator();

        MethodInfo GetPropertyValue = property.GetGetMethod();
        generator.Emit(OpCodes.Ldarg_0);
        generator.Emit(OpCodes.Callvirt, GetPropertyValue);
        generator.Emit(OpCodes.Ret);

        return ((GetDefaultKeySelectorForPropertyDelegate<T, TKey>)(method.CreateDelegate(typeof(GetDefaultKeySelectorForPropertyDelegate<T, TKey>)))).Invoke;
    }

我认为这个问题可能是相关的:DynamicMethod with generic type parameters

4

2 回答 2

0

我没有DynamicMethod像这样使用自己,但我怀疑你只需要像你已经在做的那样MakeGenericMethod做。目前,您正在尝试调用泛型方法而不指定任何类型参数。EnumerableOrderByGetDefaultKeySelectorForProperty

所以像:

MethodInfo EnumerableOrderBy = typeof(Enumerable).GetMethods()
    .Single(m => m.Name == "OrderBy" && m.GetParameters().Count() == 3)
    .MakeGenericMethod(typeof(T), propertyToSortOn.PropertyType);

MakeGenericMethod使用参数数组,因此您不需要显式构造Type[]要传入的。)

(如果您需要使用实体框架,我曾认为您会查看Queryable而不是Enumerable构建表达式树而不是委托,但这是另一回事。)

于 2013-08-15T14:51:40.330 回答
0

我更喜欢使用表达式来解决这类问题。这是一个适合您的示例。

    public static Func<IEnumerable<T>, PropertyInfo, IEnumerable<T>> GetFunctionToSortRecords<T>(IEnumerable<T> records, PropertyInfo property)
    {
        var propertyExpression = GetExpressionForProperty<T>(property);
        var method = typeof(TheCurrentClass).GetMethod("InternalGetFunctionToSortRecords", BindingFlags.NonPublic | BindingFlags.Static);

        return (Func<IEnumerable<T>, PropertyInfo, IEnumerable<T>>)method.MakeGenericMethod(typeof(T), property.PropertyType).Invoke(null, new object[] { propertyExpression });
    }

    private static Func<IEnumerable<T>, PropertyInfo, IEnumerable<T>> InternalGetFunctionToSortRecords<T, TProp>(Expression propertyExpression)
    {
        var lambdaExpression = propertyExpression as LambdaExpression;
        Func<T, TProp> keySelector = (Func<T, TProp>)lambdaExpression.Compile();
        Func<IEnumerable<T>, PropertyInfo, IEnumerable<T>> sorter = (x, y) => x.OrderBy(keySelector);

        return sorter.Invoke;
    }

    private static Expression GetExpressionForProperty<T>(PropertyInfo property)
    {
        var parameter = Expression.Parameter(typeof(T));
        var propertyExpression = Expression.Property(parameter, property);
        var lambdaExpression = Expression.Lambda(propertyExpression, parameter);

        return lambdaExpression;
    }
于 2013-08-15T19:25:04.653 回答