表达式使用参数调用构造函数并设置其值

发布于 12-17 04:14 字数 1109 浏览 4 评论 0原文

我试图从表达式调用参数化构造函数,而不是使用默认构造函数。这是获取构造函数参数的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ζ澈沫2024-12-24 04:14:22

您在代码的第 4 行中定义了一个名为 li 的参数。为了在 lambda 表达式中使用此参数,您需要有一个定义此参数的范围。您有两种选择:

  1. 创建一个包含 param 作为局部变量的 BlockExpression。然后使用该表达式作为 lambda 表达式的主体。
  2. 使用 param 作为 LambdaExpression 中的参数。

如果您使用选项 1,您还必须初始化该变量。否则你会收到不同类型的错误消息。

编辑

您发布的附加代码有两个问题:

  1. 您需要在整个表达式树中使用相同的参数对象。具有相同的名称和类型并不意味着两个 Parameter 对象相等。我只需将所有内容移至并包括创建 lambda 到 ConvertThis 方法,以便您可以重用 param 变量。然后,您只需编译 ConvertThis 的返回值即可获取委托。

  2. 创建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:

  1. Create a BlockExpression that contains param as a local variable. Then use this expression as the body of your lambda expression.
  2. Use param as a parameter in your LambdaExpression.

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:

  1. 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 the ConvertThis method so you can reuse the param variable. You can then just compile the return value of ConvertThis to get your delegate.

  2. When creating the BlockExpression, you need to pass param in as a local variable. You do this by adding an argument, new ParameterExpression[] { param } to the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文