I have IEnumerable<object>
in memory.
Let's say this:
IEnumerable<object>() addedEntities = // ... some Linq-To-Object query
Also, I have a method with this signature:
public static IEnumerable<TSource> FilterByUniqueProp<TSource>
(this IEnumerable<TSource> query, TSource model)
{
// Do something accourding to this type
var type = model.GetType();
}
As you see, this is an extension method. So, I can't dynamically call it, I must use MethodInfo
for executing it in runtime.
In runtime, I must dynamically call this method for some Enumerable<T>
of runtime-known type. But, it didn't matter what I did, it doesn't work. Either model.GetType()
is always Object
or exception is thrown.
Object of type 'System.Linq.Enumerable+WhereSelectArrayIterator
2[System.Data.Objects.ObjectStateEntry,System.Object]' cannot be converted to type System.Collections.Generic.IEnumerable
1[PersonDetail]'.
Here what I had tried:
IEnumerable<object>() addedEntities = // ... some Linq-To-Object query
Type listType = typeof(List<>);
Type constructed = listType.MakeGenericType(model.GetType());
dynamic myList = Activator.CreateInstance(constructed);
myList = addedEntities;
MethodInfo mesthod = typeof(DynamicLinqExtensions).GetMethod("FilterByUniqueProp");
MethodInfo genericMethod= mesthod.MakeGenericMethod(model.GetType());
dynamic sameEntitiesInContext = genericMethod.Invoke(this, new object[] { myList, model });
Aucun commentaire:
Enregistrer un commentaire