I have the following method:
Task<IEnumerable<T>> FindAsync<T>(Expression<Func<T, bool>> expression, IEnumerable<string> columns = null, CancellationToken cancellationToken = default(CancellationToken)) where T : BaseOdataModel;
This is the way we call it:
var somedata = (await oDataClient.FindAsync(GetFilter<SomeClass>(), null)).ToList();
Where GetFilter() is declared like this:
public Expression<Func<T, bool>> GetFilter<T>() where T : BaseOdataModel
{
return od => od.Id == od.ParentId;
}
Now I need to invoke FindAsync for several classes. Since we're using generics, I tried this code:
var theClass = "OtherClass";
Type argType = Type.GetType($"ApplicationLayer.Entities.{theClass}, "ApplicationLayer");
Type filterType = this.GetType();
MethodInfo fi = filterType.GetMethod("GetFilter");
MethodInfo fi2 = fi.MakeGenericMethod(new Type[] { argType });
Type classType = odataClient.GetType();
MethodInfo mi = classType.GetMethod("FindAsync");
MethodInfo mi2 = mi.MakeGenericMethod(new Type[] { argType });
var someData = mi2.Invoke(odataClient, new object[] {fi2, Enumerable.Empty<string>(), null });
When I execute the below code, I'm facing the following error when I call the Invoke method:
Message=Object of type 'System.Reflection.RuntimeMethodInfo' cannot be converted to type 'System.Linq.Expressions.Expression
1[System.Func
2[ApplicationLayer.Entities.OtherClass,System.Boolean]]'. Source=System.Private.CoreLib
What does this mean? I won't be able to cast a method runtime to a expression because the the expression expects a generic parameter which is resolved a compilation time?
Is there a shortcut or any modification to make this works?
Thanks in advance
I changed the name of some classes and variables to mischaracterize the company code