首页 > 解决方案 > 将类型传递给表达式函数输出

问题描述

我需要能够从currentStep变量中获取类型并将该类型用于newExpression分配。这段代码正在做的是改变Expression输出类型。有没有办法可以newExpression根据 currentStep 类型动态设置 func out 类型?

PropertyInfo currentStep = wizardTransaction.GetCurrentStepPropertyInfo();

string currentStepName = wizardTransaction.GetCurrentStepPropertyInfo().PropertyType.Name;

var body = Expression.Property(expression.Body, currentStepName);

var newExpression = Expression.Lambda<Func<TModel, **currentStep.GetType()**>>(body, expression.Parameters[0]);

var model = ModelMetadata.FromLambdaExpression(newExpression, helper.ViewData).Model;

上面代码所在的方法是一个static类,因此标记为重复的解决方案的答案不适用,因为this在接受的答案中使用的关键字不能在static类中使用。

标签: c#genericsreflectionfunc

解决方案


推荐阅读