使用 Expression[] 调用 (params object[])
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
params
实际上所做的只是指定ParamArrayAttribute
该参数。 C# 编译器理解这一点,并在幕后创建数组。表达式不理解这一点,因此如果您想使用
params
调用方法,您实际上必须自己创建数组。这也可以从以下事实看出:当您使用params
方法将 lambda 分配给表达式时,该表达式包含数组创建:这里,
expressionString
将包含此字符串:要创建创建数组的表达式,请使用
Expression.NewArrayInit( )
方法。话虽这么说,如果您只需要两个参数(或一三个参数),可以使用 可以直接从表达式使用的
string.Format()
的重载。What
params
actually does is just to specifyParamArrayAttribute
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 usingparams
-method to an expression, the expression contains the array creation:Here,
expressionString
will contain this string: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.params
只是语法糖。最终参数只是一个数组。因此,参数类型应该是object[],并且描述此类数组的表达式是您应该作为第二个参数传递的内容。换句话说,您应该只有两个参数,而不是三个。第二个参数应该是一个双元素数组,其中包含当前的第二个和第三个参数。params
is just syntactic sugar. Ultimately the parameter is just an array. Therefore, the parameter type should beobject[]
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.