My goal here is to create a method SortRecords
that accepts an IEnumerable<T>
and a PropertyInfo
as parameters. The IEnumerable<T>
is a list of records. The PropertyInfo
is a property of of T
. When invoked, SortRecords
should invoke the Enumerable.SortBy<T, typeof Property>
method with x => x.Property
. Note here that Enumerable.SortBy
has two generic parameters. Also, reflection cannot be used inside the lambda expression because (a) it is slow and (b) it won't work with Entity Framework.
I have written some code, but I keep seeing the error message Operation could destabilize the runtime
. Here is what my code looks like
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);
}
end first code snippet
Method definitions follow
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;
}
I think that this question may be related: DynamicMethod with generic type parameters
I haven't used
DynamicMethod
myself like this, but I suspect you just need toMakeGenericMethod
on thisEnumerableOrderBy
just like you're already doing forGetDefaultKeySelectorForProperty
. At the moment you're trying to call the generic method without specifying any type arguments.So something like:
(
MakeGenericMethod
uses a parameter array, so you don't need to explicitly construct theType[]
to pass in.)(If you need to work with Entity Framework, I've have thought you'd be looking at
Queryable
rather thanEnumerable
and building expression trees instead of delegates, but that's another matter.)