c# - 将类型传递给表达式函数输出
问题描述
我需要能够从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
类中使用。
解决方案
推荐阅读
- firebase - 您如何间隔运行云功能?
- bash - 从特定列中的 CSV 文件中删除小数部分?
- azure - 网络错误 - 使用 NFS 挂载 Azure Blob 存储时出现 53
- r - 在计算 R 中的“pnorm()”生成的两个极端概率之间的差异时绕过机器精度问题
- node.js - 我在本地存储中找到了用户身份验证令牌,但它在 SSR 中不起作用,有什么解决方案吗?
- android - 不推荐使用 Dagger Hilt @ViewModelInject 如何在 @HiltViewModel 中使用 @ApplicationContext
- python - Python:将列表中的字典提取到新行中
- wso2 - 是否有可能在 JWT 令牌中有范围而没有在 deployment.toml 文件中放置允许的范围?
- mongodb - bson.M 数组的顺序是否在 mongodb go 驱动程序中维护?
- javascript - Angular - [innerHTML] 在 Internet Explorer 上不起作用