c# - C# 中的表达式树和惰性求值
问题描述
我有一小段代码,我在其中获取一个ParameterExpression
字符串数组并将特定索引转换为目标类型。我通过调用Parse
(如果类型是原始类型)或尝试进行原始转换(希望是字符串或隐式字符串)来做到这一点。
static Expression ParseOrConvert(ParameterExpression strArray, ConstantExpression index, ParameterInfo paramInfo)
{
Type paramType = paramInfo.ParameterType;
Expression paramValue = Expression.ArrayIndex(strArray, index);
if (paramType.IsPrimitive) {
MethodInfo parseMethod = paramType.GetMethod("Parse", new[] { typeof(string) });
// Fetch Int32.Parse(), etc.
// Parse paramValue (a string) to target type
paramValue = Expression.Call(parseMethod, paramValue);
}
else {
// Else attempt a raw conversion
paramValue = Expression.Convert(paramValue, paramType);
}
return paramValue;
}
这行得通,但我正在尝试这样重写条件。
paramValue = Expression.Condition(
Expression.Constant(paramType.IsPrimitive),
Expression.Call(parseMethod, paramValue),
Expression.Convert(paramValue, paramType)
);
这总是导致System.InvalidOperationException
,大概是因为它尝试了两种转换。我发现第二种风格写起来更直观,所以这很不幸。
我可以以将评估推迟到实际需要这些值的方式来编写它吗?
解决方案
调试通常就像新闻业……在新闻业中有五个 W:谁、什么、哪里、何时、为什么(加上如何)……在编程中它是相似的。谁抛出异常(那是什么)?让我们让代码更容易调试:
static Expression ParseOrConvert(ParameterExpression strArray, ConstantExpression index, ParameterInfo paramInfo)
{
Type paramType = paramInfo.ParameterType;
Expression paramValue = Expression.ArrayIndex(strArray, index);
MethodInfo parseMethod = paramType.GetMethod("Parse", new[] { typeof(string) });
var isPrimitive = Expression.Constant(paramType.IsPrimitive);
var call = Expression.Call(parseMethod, paramValue);
var convert = Expression.Convert(paramValue, paramType);
var paramValue2 = Expression.Condition(
isPrimitive,
call,
convert
);
return paramValue2;
}
然后这样称呼它:
public static void MyMethod(int par1)
{
}
接着
ParameterExpression strArray = Expression.Parameter(typeof(string[]));
// paramType int
var paramInfo = typeof(Program).GetMethod("MyMethod").GetParameters()[0];
var result = ParseOrConvert(strArray, Expression.Constant(0), paramInfo);
现在......谁抛出异常?Expression.Convert(paramValue, paramType)
抛出异常......为什么?因为您正在尝试执行以下操作:
string paramValue = ...;
convert = (int)paramValue;
那肯定是违法的!即使是“死”代码(无法访问的代码)也必须在 .NET(以其 IL 语言)中“可编译”。所以你的错误是试图在你的表达式中引入一些非法的死代码,那就是:
string paramValue = ...;
isPrimitive = true ? int.Parse(paramValue) : (int)paramValue;
这不会在 C# 中编译,甚至可能无法用 IL 代码编写。并且 Expression 类抛出它。
推荐阅读
- python - 如何通过Python中的类甚至带有属性的函数返回变量甚至值?
- plsql - 如何在pl/sql中执行绑定变量?
- c# - 包含货币值的输入字段的客户端验证错误
- docker - docker 被杀后 consul docker 无法持久化数据
- pysimplegui - pysimpleguiweb 更改托管端口
- ruby-on-rails - ActiveRecord 嵌套关联
- sql - 如何在标准 SQL BigQuery 中解析 JSON?
- vb.net - 新加入的数组缺少一个元素 - vb
- r - 是否可以定义一个“函数”并将其插入“公式”以在 R 中构造一个“model.frame”?
- r - 使用“setkey”函数将表与 R 中的常用值连接起来