public static Add ( |
||
left | An |
|
right | An |
|
return |
// Adding two integer expressions ParameterExpression num1 = Expression.Parameter(typeof(int), "num1"); ParameterExpression num2 = Expression.Parameter(typeof(int), "num2"); BinaryExpression sumExp = Expression.Add(num1, num2); Console.WriteLine(sumExp); // prints "num1 + num2"
// Adding two string expressions ParameterExpression str1 = Expression.Parameter(typeof(string), "str1"); ParameterExpression str2 = Expression.Parameter(typeof(string), "str2"); MethodInfo concatMethod = typeof(string).GetMethod("Concat", new[] { typeof(string), typeof(string) }); MethodCallExpression concatExp = Expression.Call(concatMethod, str1, str2); Console.WriteLine(concatExp); // prints "Concat(str1, str2)"In this example, we create two parameter expressions of type string and then use the Expression Call method to call the string Concat method and pass the two string expressions as arguments. The resulting expression represents the concatenation of str1 and str2. The System.Linq.Expressions namespace is included as a part of the .NET Standard Library, which is a set of APIs that are available across multiple platforms and can be used with any .NET language.
public static Add ( |
||
left | An |
|
right | An |
|
return |