表达式使用参数调用构造函数并设置其值
我试图从表达式调用参数化构造函数,而不是使用默认构造函数。这是获取构造函数参数的代码:
ConstructorInfo ci = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, CallingConventions.HasThis, new[] { typeof(bool) }, new ParameterModifier[] { });
ParameterInfo[] paramsInfo = ci.GetParameters();
//create a single param of type object[]
ParameterExpression param = Expression.Parameter(typeof(bool), "il");
Expression[] argsExp = new Expression[paramsInfo.Length];
//pick each arg from the params array
//and create a typed expression of them
for (int i = 0; i < paramsInfo.Length; i++)
{
Expression index = Expression.Constant(i);
Type paramType = paramsInfo[i].ParameterType;
Expression paramAccessorExp = param;
//Expression.ArrayIndex(param, index);
Expression paramCastExp =
Expression.Convert(paramAccessorExp, paramType);
argsExp[i] = param;
}
NewExpression ci2 = Expression.New(ci, argsExp);
但是如果我尝试编译 lambda 表达式,则会收到以下错误:
从范围“”引用了“System.Boolean”类型的变量“il”,但未定义”
我缺少什么?任何帮助和/或提示表示赞赏。
i am trying to call an parameterized constructor from an expression instead of using the default ctor. this is the code that gets the constructor parameter(s):
ConstructorInfo ci = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, CallingConventions.HasThis, new[] { typeof(bool) }, new ParameterModifier[] { });
ParameterInfo[] paramsInfo = ci.GetParameters();
//create a single param of type object[]
ParameterExpression param = Expression.Parameter(typeof(bool), "il");
Expression[] argsExp = new Expression[paramsInfo.Length];
//pick each arg from the params array
//and create a typed expression of them
for (int i = 0; i < paramsInfo.Length; i++)
{
Expression index = Expression.Constant(i);
Type paramType = paramsInfo[i].ParameterType;
Expression paramAccessorExp = param;
//Expression.ArrayIndex(param, index);
Expression paramCastExp =
Expression.Convert(paramAccessorExp, paramType);
argsExp[i] = param;
}
NewExpression ci2 = Expression.New(ci, argsExp);
But if i try to compile the lambda expression i am getting the following error:
variable 'il' of type 'System.Boolean' referenced from scope '', but it is not defined"
What am i missing? Any help and/ or hint is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您在代码的第 4 行中定义了一个名为
li
的参数。为了在 lambda 表达式中使用此参数,您需要有一个定义此参数的范围。您有两种选择:param
作为局部变量的BlockExpression
。然后使用该表达式作为 lambda 表达式的主体。param
作为LambdaExpression
中的参数。如果您使用选项 1,您还必须初始化该变量。否则你会收到不同类型的错误消息。
编辑
您发布的附加代码有两个问题:
您需要在整个表达式树中使用相同的参数对象。具有相同的名称和类型并不意味着两个 Parameter 对象相等。我只需将所有内容移至并包括创建 lambda 到
ConvertThis
方法,以便您可以重用param
变量。然后,您只需编译ConvertThis
的返回值即可获取委托。创建
BlockExpression
时,需要将param
作为局部变量传入。您可以通过向方法添加参数new ParameterExpression[] { param }
来实现此目的。You define a parameter called
li
in the 4th line of your code. In order to use this in a lambda expression, you need to have a scope in which this parameter is defined. You have two choices:BlockExpression
that containsparam
as a local variable. Then use this expression as the body of your lambda expression.param
as a parameter in yourLambdaExpression
.If you use option 1, you'll also have to initialize the variable. Otherwise you'll get a different kind of error message.
EDIT
There are two problems with the additional code you posted:
You need to use the same parameter object throughout your expression tree. Having the same name and type does not make two
Parameter
objects equal. I would simply move everything up to and including creating the lambda to theConvertThis
method so you can reuse theparam
variable. You can then just compile the return value ofConvertThis
to get your delegate.When creating the
BlockExpression
, you need to passparam
in as a local variable. You do this by adding an argument,new ParameterExpression[] { param }
to the method.