如何在使用Expression <Func <T >>时设置参数

泰勒

我已经编写了以下代码来处理从数据库到数据类型的映射参数(相信我,我希望我可以使用标准ORM,但是由于许多原因,这是不可行的)

public void LoadDatabaseValue<T>(DataTable partData, string identifier, string mappingName, Expression<Func<T>> mappingProperty)
    {
        var partAttributeValue = mappingProperty.Name;
        var memberExpression = (MemberExpression)mappingProperty.Body;
        var prop = (PropertyInfo)memberExpression.Member;
        try
        {
            var selectedRow = partData.Select($"partattributename = '{mappingName}'");
            var selectedValue = selectedRow[0]["PartAttributeValue"];

            var typedOutput = (T)Convert.ChangeType(selectedValue, typeof(T));

            prop.SetValue(memberExpression.Expression, typedOutput, null);
        }
        catch (Exception exception)
        {
            _databaseImportError = true;
            // code to log this error
    }

当我尝试运行此程序时,出现以下异常

{System.Reflection.TargetException: Object does not match target type.
at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj,   BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)   }

当我调试它时,我的typedOutput会与我的属性类型对齐,因此我不确定为什么会引发此异常。

我用这样的方式称呼它

LoadDatabaseValue(partData, identifier, "Offset", () => Offset);
伊万·斯托夫(Ivan Stoev)

如果要保留当前的方法设计,则需要一种方法以某种方式评估memberExpression.Expression以便能够调用SetValue方法。

所以换行

prop.SetValue(memberExpression.Expression, typedOutput, null);

prop.SetValue(Evaluate(memberExpression.Expression), typedOutput, null);

然后使用以下实现之一:

(A)如果仅使用属性访问器,这将足够:

static object Evaluate(Expression e)
{
    if (e == null) return null;
    var me = e as MemberExpression;
    if (me != null)
        return ((PropertyInfo)me.Member).GetValue(Evaluate(me.Expression), null);
    return ((ConstantExpression)e).Value;
}

(B)这是更通用的,但较慢:

static object Evaluate(Expression e)
{
    if (e == null) return null;
    return Expression.Lambda(e).Compile().DynamicInvoke();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

函数内部的Expression <Func <T >>的设置值

如何从 Expression<Func<T, object[]>> 参数获取返回的属性

如何使用 Linq Skip and Take 调用 Expression<Func<T, bool>> 参数化函数

为什么要使用Expression <Func <T >>而不是Func <T>?

如何在类字段中存储 Expression<Func<T, object>>?

在方法内部使用Expression <Func <T,Boolean >>谓词时出错,提示“类型'T'必须是引用类型”

带out参数的Func <T>

Func <T,TResult>访问参数

如何将 Func<T> 传递给方法参数

使用反射将 Func<T,bool> 作为参数传递?

如何调用需要带有lambda Expression参数的Func <T>的方法

使用NSubstitute在单元测试中验证Expression <Func <T,bool >>参数

C#Func <T,bool>与Expression <Func <T,bool >>

将Func <T,TProperty>转换为Expression <Func <T,Property >>

方法重载中的 Expression<Func<T,bool>> 与 Func<T,bool>

带有参数IQueryable <T>和Expression <Func <T,T >>的.net通用方法

使用List <Func <T,object >>时,索引超出数组的范围

如何使用Func类型的Expression作为参数调用方法

如何使用类型变量中的类型参数创建Expression <Func <>>

如何在C#中使用未知数量和参数类型包装Func <T1 ... Tn>?

如何将Expression <Func <T,object >>转换为Expression <Func <T,bool >>?

如何获取Expression <Func <T,TK >>的T的实例

如何合并两个 Expression<Func<T, T>

设置任何Func <>的参数

如何使用Func <Task <T >>语法?

如何使用 Func<string, T> 作为参数之一调用方法

转到:如何在go中使用func()bool参数?

如何在C#中将LambdaExpression转换为Expression <Func <T,bool >>

使用MethodInfo创建func <T,T>