|
|
|
start date: Thu, 2 Aug 2007 00:35:23 +1000,
posted on: microsoft.public.dotnet.framework
back
| Thread Index |
|
1
Lloyd Dupont net.galador@ld
|
|
2
Jon Skeet [C# MVP]
|
|
3
Lloyd Dupont net.galador@ld
|
problem with Linq Expressions
I'm trying to create some linq expression manually.
At some stage I want to create an expression to concatenate 2 strings, I'm
trying the following code:
using System.Linq.Expressions;
Expression e1, e2;
return Expression.Call(typeof(string), "Concat", new Type[] {
typeof(string), typeof(string) }, e1, e2);
this call yield the following error:
System.InvaldOperationExpression
No method 'Concat' on type 'System.String' is compatible with the supplied
arguments.
Well, well, I'm somewhat surprised...
what else could I try?
Date:Thu, 2 Aug 2007 00:35:23 +1000
Author:
|
Re: problem with Linq Expressions
Lloyd Dupont <net.galador@ld> wrote:
> I'm trying to create some linq expression manually.
>
> At some stage I want to create an expression to concatenate 2 strings, I'm
> trying the following code:
>
> using System.Linq.Expressions;
> Expression e1, e2;
> return Expression.Call(typeof(string), "Concat", new Type[] {
> typeof(string), typeof(string) }, e1, e2);
>
> this call yield the following error:
> System.InvaldOperationExpression
> No method 'Concat' on type 'System.String' is compatible with the supplied
> arguments.
>
>
> Well, well, I'm somewhat surprised...
> what else could I try?
Your new Type[] ... part is wrong. That means you're trying to call
string.Concat<string,string>(e1, e2).
Unfortunately, because there are multiple overloads of string.Concat
which take two parameters, you can't (AFAIK) use this form of
Expression.Call to create the expression. Instead, use
MethodInfo mi = typeof(string).GetMethod("Concat",
new [] {typeof(string), typeof(string) });
return Expression.Call(mi, e1, e2);
--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Date:Wed, 1 Aug 2007 19:21:37 +0100
Author:
|
Re: problem with Linq Expressions
Thanks Jon!
Looks promising I will give it a try tonight!
As a side note I'm thinking to stop using expression, which are very
demanding (for example Expression.Add() fail when one expression has an Int
value and the other and has a Float value) and using the DLR for the same
purpose.
But Thanks to your tip I can go one step further... I will see...
"Jon Skeet [C# MVP]" wrote in message
> Your new Type[] ... part is wrong. That means you're trying to call
> string.Concat<string,string>(e1, e2).
>
> Unfortunately, because there are multiple overloads of string.Concat
> which take two parameters, you can't (AFAIK) use this form of
> Expression.Call to create the expression. Instead, use
>
> MethodInfo mi = typeof(string).GetMethod("Concat",
> new [] {typeof(string), typeof(string) });
> return Expression.Call(mi, e1, e2);
Date:Thu, 2 Aug 2007 10:59:31 +1000
Author:
|
|
|