[英]Build expression tree to call function with 1 or 2 parameters
所以,嘗試一下表達式樹。 這是一個想法:我想返回一個Func<long, byte?, object>
對象。 根據我想要使用該函數的類型,我將使用以下兩種方法之一: LoadById(long, byte?)
或LoadByID(long)
。 兩者都返回一個對象。 所以我正在嘗試執行以下操作:根據類型是否實現某個接口,我使用classToUseFunctionOn.LoadById(long, byte?)
或classToUseFunctionOn.LoadByID(long)
。 所以,基本上我希望如果它實現了接口,則返回以下代碼: (long id, byte? options) => new TestFacade().LoadById(id, options) and (long id, byte? options) => new TestFacade().LoadByID(id)。 我只是不知道該怎么做。 最后幾行出錯了。 Lambda 調用聲明參數數量不正確。 以下是我到目前為止的代碼:
private static Func GetDataExtractorForTypeWithId(Type type)
{
var paramId = Expression.Parameter(typeof(long), "id");
ParameterExpression paramOptions = null;
//gets the ConstructorInfo for the constructor of type T with a single parameter of type IDataReader
var facadetype = GetFacadeType(type.Name);
MethodInfo loadMethod;
var linkedEntitiesInterface = facadetype.GetInterface(typeof(IFacadeLoadLinkedEntities).Name);
var lamdaParameterExpressions = new List() { paramId };
if (linkedEntitiesInterface != null)
{
loadMethod = facadetype.GetMethod("LoadById");
paramOptions = Expression.Parameter(linkedEntitiesInterface.GetGenericTypeDefinition().GenericTypeArguments[1], "options");
lamdaParameterExpressions.Add(paramOptions);
}
else
{
paramOptions = Expression.Parameter(typeof(byte?));
loadMethod = facadetype.GetMethod("LoadByID", new Type[1]{typeof(long)});
}
var facadeConstructor = facadetype.GetConstructor(new Type[0]);
var newFacade = Expression.New(facadeConstructor);
var callLoad = Expression.Call(newFacade, loadMethod, lamdaParameterExpressions);
lamdaParameterExpressions.Add(paramOptions);
var returnValue = Expression.Parameter(typeof(object));
lamdaParameterExpressions.Add(returnValue);
var entityVariable = Expression.Variable(typeof(object), "entity");
Expression.Assign(entityVariable, callLoad);
var lambda = Expression.Lambda>(
entityVariable, lamdaParameterExpressions.ToArray());
//compiles the Expression to a usable delegete.
return lambda.Compile();
}
與此同時,我找到了讓它工作的方法:
private static Func GetDataExtractorForTypeWithId(Type type)
{
var paramId = Expression.Parameter(typeof(long), "id");
ParameterExpression paramOptions = null;
var facadetype = GetFacadeType(type.Name) ?? typeof(AzzFacade).MakeGenericType(type);
MethodInfo loadMethod;
var linkedEntitiesInterface = facadetype.GetInterface(typeof(IFacadeLoadLinkedEntities).Name);
if (linkedEntitiesInterface != null)
{
loadMethod = facadetype.GetMethod("LoadById");
paramOptions = Expression.Parameter(typeof(byte), "options");
}
else
{
paramOptions = Expression.Parameter(typeof(byte));
loadMethod = facadetype.GetMethod("LoadByID", new Type[1]{typeof(long)});
}
var facadeConstructor = facadetype.GetConstructor(new Type[0]);
if(facadeConstructor==null)
throw new NullReferenceException($"No parameterless constructor found for facade for type {type.Name}");
MethodCallExpression callLoad;
var newFacade = Expression.New(facadeConstructor);
if (linkedEntitiesInterface != null)
{
var conversionExpression = Expression.Convert(paramOptions, linkedEntitiesInterface.GetGenericArguments()[1]);
// ReSharper disable once AssignNullToNotNullAttribute
callLoad = Expression.Call(newFacade, loadMethod, paramId, conversionExpression);
}
else
{
// ReSharper disable once AssignNullToNotNullAttribute
callLoad = Expression.Call(newFacade, loadMethod, paramId);
}
var lambda = Expression.Lambda>(
// ReSharper disable once AssignNullToNotNullAttribute
callLoad, paramId, paramOptions);
//compiles the Expression to a usable delegate.
return lambda.Compile();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.