使用 Expression[] 调用 (params object[])

发布于 2024-11-09 23:10:45 字数 683 浏览 0 评论 0原文

我正在尝试从 Linq.Expression 树中调用 String.Format。这是一个简单的例子:

    var format = Expression.Constant("({0}) {1}");
    var company = Expression.Property(input, membernames.First());
    var project = Expression.Property(input, membernames.Last());
    var args = new Expression[] {format, company, project};
    var invoke = Expression.Call(method,args);

但是问题是 String.Format 具有以下签名:

String.Format(string format, params object[] args)

并且我正在尝试传入 Expression[]。

现在,我可以解决创建数组、用表达式结果填充数组的所有麻烦,但我真正想要的结果是这样的:

String.Format("({0}) {1}", input.foo, input.bar)

如何通过 Linq 表达式调用 params 函数?

I'm trying to call String.Format from with in a Linq.Expression tree. Here's a quick example:

    var format = Expression.Constant("({0}) {1}");
    var company = Expression.Property(input, membernames.First());
    var project = Expression.Property(input, membernames.Last());
    var args = new Expression[] {format, company, project};
    var invoke = Expression.Call(method,args);

The issue however is that String.Format has the signature of:

String.Format(string format, params object[] args)

and I'm trying to pass in Expression[].

Now I could go through all the trouble of creating an array, populating it with the results of my expressions, but what I really want the result to be, is something like this:

String.Format("({0}) {1}", input.foo, input.bar)

How do I go about calling a params function via Linq Expressions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

千纸鹤 2024-11-16 23:10:45

params 实际上所做的只是指定 ParamArrayAttribute 该参数。 C# 编译器理解这一点,并在幕后创建数组。

表达式不理解这一点,因此如果您想使用 params 调用方法,您实际上必须自己创建数组。这也可以从以下事实看出:当您使用 params 方法将 lambda 分配给表达式时,该表达式包含数组创建:

Expression<Func<string>> expression = () => string.Format("",1,2,3,4);
string expressionString = expression.ToString();

这里,expressionString 将包含此字符串:

() => Format("", new [] {Convert(1), Convert(2), Convert(3), Convert(4)})

要创建创建数组的表达式,请使用 Expression.NewArrayInit( )方法

话虽这么说,如果您只需要两个参数(或一三个参数),可以使用 可以直接从表达式使用的 string.Format() 的重载。

What params actually does is just to specify ParamArrayAttribute for that parameter. The C# compiler understands this, and creates the array behind the scenes.

Expressions don't understand this, so you actually have to create the array by yourself, if you want to call a method with params. This can be also seen by the fact that when you assign a lambda using params-method to an expression, the expression contains the array creation:

Expression<Func<string>> expression = () => string.Format("",1,2,3,4);
string expressionString = expression.ToString();

Here, expressionString will contain this string:

() => Format("", new [] {Convert(1), Convert(2), Convert(3), Convert(4)})

To create an expression that creates an array, use the Expression.NewArrayInit() method.

That being said, if you only want two parameters (or one or three), there is an overload of string.Format() that you can use directly from an expression.

后来的我们 2024-11-16 23:10:45

params 只是语法糖。最终参数只是一个数组。因此,参数类型应该是object[],并且描述此类数组的表达式是您应该作为第二个参数传递的内容。换句话说,您应该只有两个参数,而不是三个。第二个参数应该是一个双元素数组,其中包含当前的第二个和第三个参数。

params is just syntactic sugar. Ultimately the parameter is just an array. Therefore, the parameter type should be object[] and an expression describing such an array is what you should pass as the second argument. In other words, you should only have two arguments, not three. And the second argument should be a two-element array containing what is currently your 2nd and 3rd arguments.

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