So I've been tinkering with Linq.Expressions (and if anyone can suggest a more proper or more elegant way to do what I'm doing please feel free to chime in) and have hit a wall in trying to do something.
Let's imagine we have a simple math class:
public class SimpleMath {
public int AddNumbers(int number1, int number2) {
return number1 + number2;
}
}
I decide I want to convert our AddNumbers method to a simple Func<object, object, object> delegate.
To do this I do the following:
// Two collections, one for Type Object paramaters and one for converting to Type int.
List<ParameterExpression> parameters = new List<ParameterExpression>();
List<Expression> convertedParameters = new List<Expression>();
// Populate collections with Parameter and conversion
ParameterExpression parameter1 = Expression.Parameter(typeof(object));
parameters.Add(parameter1);
convertedParameters.Add(Expression.Convert(parameter1, typeof(int)));
ParameterExpression parameter2 = Expression.Parameter(typeof(object));
parameters.Add(parameter2);
convertedParameters.Add(Expression.Convert(parameter2, typeof(int)));
// Create instance of SimpleMath
SimpleMath simpleMath = new SimpleMath();
// Get the MethodInfo for the AddNumbers method
MethodInfo addNumebrsMethodInfo = simpleMath.GetType().GetMethods().Where(x => x.Name == "AddNumbers").ToArray()[0];
// Create MethodCallExpression using the SimpleMath object, the MethodInfo of the method we want and the converted parameters
MethodCallExpression returnMethodWithParameters = Expression.Call(Expression.Constant(simpleMath), addNumebrsMethodInfo, convertedParameters);
// Convert the MethodCallExpression to return an Object rather than int
UnaryExpression returnMethodWithParametersAsObject = Expression.Convert(returnMethodWithParameters, typeof(object));
// Create the Func<object, object, object> with our converted Expression and Parameters of Type Object
Func<object, object, object> func = Expression.Lambda<Func<object, object, object>>(returnMethodWithParametersAsObject, parameters).Compile();
object result = func(20, 40); // result = 60
So if you run that code func should return simple calculations. However, it accepts parameters of Type Object, which obviously leaves it open to problems at runtime, for example:
object result1 = func(20, "f"); // Throws InvalidCastException
So I want to wrap the method in a Try...Catch (obviously this exact problem would be picked up at compile time if we were dealing with a straight call to AddNumbers and passing a string as a parameter).
So to catch this exception I can do the following:
TryExpression tryCatchMethod = TryExpression.TryCatch(returnMethodWithParametersAsObject, Expression.Catch(typeof(InvalidCastException), Expression.Constant(55, typeof(object))));
Func<object, object, object> func = Expression.Lambda<Func<object, object, object>>(tryCatchMethod, parameters).Compile();
object result = func(20, "f"); // result = 55
The TryExpression.TryCatch takes an Expression body and then a collection of CatchBlock handlers. returnMethodWithParametersAsObject is the expression we wish to wrap, Expression.Catch defines that the Exception we want to catch is of Type InvalidCastException and its Expression body is a constant, 55.
So the exception is handled, but it's not much use unless I want to always return a static value when the exception is thrown. So returning to the SimpleMath class I add a new method HandleException:
public class SimpleMath {
public int AddNumbers(int number1, int number2) {
return number1 + number2;
}
public int HandleException() {
return 100;
}
}
And following the same process above I convert the new method to an Expression:
MethodInfo handleExceptionMethodInfo = simpleMath.GetType().GetMethods().Where(x => x.Name == "HandleException").ToArray()[0];
MethodCallExpression returnMethodWithParameters2 = Expression.Call(Expression.Constant(simpleMath), handleExceptionMethodInfo);
UnaryExpression returnMethodWithParametersAsObject2 = Expression.Convert(returnMethodWithParameters2, typeof(object));
Then using it when creating the TryCatch block:
TryExpression tryCatchMethod2 = TryExpression.TryCatch(returnMethodWithParametersAsObject, Expression.Catch(typeof(InvalidCastException), returnMethodWithParametersAsObject2));
Func<object, object, object> func = Expression.Lambda<Func<object, object, object>>(tryCatchMethod2, parameters).Compile();
object result = func(20, "f"); // result = 100
So this time when the InvalidCastException is thrown the SimpleMath.HandleException method will be executed. So far so good, I can now execute some code when there is an exception.
My problem now is, in a normal inline Try...Catch block you actually have the exception object at your disposal. E.g.
try {
// Do stuff that causes an exception
} catch (InvalidCastException ex) {
// Do stuff with InvalidCastException ex
}
I can execute code when an exception is thrown but I can't seem to figure out how to actually get my hands on the exception object like you would in a normal Try...Catch block.
Any help would be appreciated!
p.s. I know that you wouldn't actually organise anything in the way I've done above but I thought it was necessary for example purposes to show the mechanics of what I want to do.
You need to pass your catched exception to CatchBlock expression as parameter.
For this you should do:
Change signature of HandleException. It will takes a exception as argument:
public int HandleException(InvalidCastException exp)
{
// Put here some real logic. I tested it using line below
Console.WriteLine(exp.Message);
return 100;
}
Use CatchBlock.Variable to pass your handled exception into catch block. You can set it use constructor. Read comment in the code below:
// Create parameter that will be passed to catch block
var excepParam = Expression.Parameter(typeof(InvalidCastException));
MethodInfo handleExceptionMethodInfo = simpleMath.GetType().GetMethods().Where(x => x.Name == "HandleException").ToArray()[0];
MethodCallExpression returnMethodWithParameters2 = Expression.Call(Expression.Constant(simpleMath), handleExceptionMethodInfo, excepParam);
UnaryExpression returnMethodWithParametersAsObject2 = Expression.Convert(returnMethodWithParameters2, typeof(object));
// Put created parameter before to CatchBlock.Variable using Expression.Catch
// that takes the first argument as ParameterExpression
TryExpression tryCatchMethod2 = TryExpression.TryCatch(returnMethodWithParametersAsObject, Expression.Catch(excepParam, returnMethodWithParametersAsObject2));
var exppp = Expression.Lambda<Func<object, object, object>>(tryCatchMethod2, parameters);
Func<object, object, object> func2 = Expression.Lambda<Func<object, object, object>>(tryCatchMethod2, parameters).Compile();
object result2 = func2(20, "f"); // result = 100
#GeorgeAlexandria's answer is fully correct. But I found it very hard work to decode it all when I came to do the same thing.
Perhaps the following code (written as 2 helper methods), will help the next person who needs to do something like this ...
private Expression WrapActionExpressionIn_Try_Catch_ThrowNewMessage(Expression coreExpression, string newMessage)
{
return
Expression.TryCatch(
coreExpression,
Expression.Catch(typeof(Exception),
Expression.Throw(
Expression.Constant(new Exception(newMessage))
)
));
}
private Expression WrapActionExpressionIn_Try_Catch_RethrowWithAdditionalMessage(Expression coreExpression, string additionalMessage)
{
var caughtExceptionParameter = Expression.Parameter(typeof(Exception));
//We want to call `new Exception(additionalMessage, caughtException)`
var ctorForExceptionWithMessageAndInnerException = typeof(Exception).GetConstructor(new[] {typeof(string), typeof(Exception)});
var replacementExceptionExpresion = Expression.New(ctorForExceptionWithMessageAndInnerException, Expression.Constant(additionalMessage), caughtExceptionParameter);
return
Expression.TryCatch(
coreExpression,
Expression.Catch(caughtExceptionParameter,
Expression.Throw( replacementExceptionExpresion )
));
}
These two methods both start from a premise of "we already have an Expression representing an Action<TInstance> that we are about to invoke. And now we want to add a try-catch around that Action."
Without the Try-Catch, we would have done:
Action finalLamda = Expression.Lambda<Action<TInstance>>(actionExpression, instanceExpression).Compile();
now we do:
var actionWithTryCatchExpression = WrapActionExpressionIn_Try_Catch_RethrowWithAdditionalMessage(actionExpression);
Action finalLamda = Expression.Lambda<Action<TInstance>>(actionWithTryCatchExpression, instanceExpression).Compile();
Respectively the two helpers represent:
try
{
action();
}
catch(Exception)
{
throw new Exception(newMessage);
}
\\and
try
{
action();
}
catch(Exception e)
{
throw new Exception(additionalMessage, e);
}
Related
I need to divide two input double number,and with try catch to handle DivideByZeroException and others.
ParameterExpression dInput1 = Expression.Parameter(typeof(double), "input1");
ParameterExpression dInput2 = Expression.Parameter(typeof(double), "input2");
ParameterExpression e_DBZ = Expression.Parameter(typeof(DivideByZeroException));
ParameterExpression e_ = Expression.Parameter(typeof(Exception));
var _calc = Expression.Divide(dInput1, dInput2);
MethodInfo _consolewrite = typeof(Console).GetMethod("WriteLine",new Type[] { typeof(string) });
MethodCallExpression _output = Expression.Call(null, _consolewrite, Expression.Convert(Expression.Constant(_calc,typeof(double)), typeof(string)));
var _catch_DivideByZeroException = Expression.Catch(e_DBZ, Expression.Call(null, _consolewrite, Expression.Property(e_DBZ, "Message")));
var _catch_Exception = Expression.Catch(e_, Expression.Call(null, _consolewrite, Expression.Property(e_DBZ, "Message")));
var _calc_Block = Expression.Block(_calc, Expression.Constant(0));
var _try = Expression.TryCatch(_calc_Block, _catch_DivideByZeroException, _catch_Exception);
var _lambda = Expression.Lambda<Func<double,double, double>>(_try, dInput1,dInput2);
Console.WriteLine(_lambda.Body);
Console.WriteLine(_lambda.Compile().Invoke(1,0));
But the Expression.Convert throw System.ArgumentException.
You're entire approach doesn't seem right, when you call Expression.Convert and outside you issue Expression.Call()
But nonetheless, here is a working example of Expression.Convert:
// This expression represents a type convertion operation.
Expression convertExpr = Expression.Convert(
Expression.Constant(5.5),
typeof(Int16)
);
// Print out the expression.
Console.WriteLine(convertExpr.ToString());
// The following statement first creates an expression tree,
// then compiles it, and then executes it.
Console.WriteLine(Expression.Lambda<Func<Int16>>(convertExpr).Compile()());
// This code example produces the following output:
//
// Convert(5.5)
// 5
If you dig in into Expression.Convert doc, you will find a table with the following line:
Exceptions
Exception:
ArgumentNullException
Condition:
expression or type is null.
that means that either you're entire expression produces null, or your string is null.
What's wrong with this?
try
{
var e_DBZ = dInput1 / dInput2;
}
catch (Exception e)
{
// Handle Divide by Zero error
Console.WriteLine(e.Message);
}
I am trying to construct function that accepts more that one parameter. Lambda function accepting one parameter works fine. Here is the code.
var value = 22.55;
var method = typeof(TextWriter).GetMethod("WriteLine", new Type[] { value.GetType() });
ParameterExpression inputParameter = Expression.Parameter(typeof(TextWriter));
var block = Expression.Block(
inputParameter,
Expression.Call(inputParameter, method, Expression.Constant(value)));
var function = Expression.Lambda<Action<TextWriter>>(block, inputParameter).Compile();
function(Console.Out);
However when I add one more parameter that I do not even use then function throws null reference exception. I must be missing something but have no idea what. Here is the code that does not work:
var value = 22.55;
var method = typeof(TextWriter).GetMethod("WriteLine", new Type[] { value.GetType() });
ParameterExpression inputParameter = Expression.Parameter(typeof(TextWriter));
// Additional parameter here
ParameterExpression inputParameter2 = Expression.Parameter(typeof(double));
var block = Expression.Block(
// Function block accepts two parameters
new List<ParameterExpression>() { inputParameter, inputParameter2 },
Expression.Call(inputParameter, method, Expression.Constant(value)));
var function = Expression.Lambda<Action<TextWriter, double>>(block, inputParameter, inputParameter2).Compile();
// ....aaand null exception here. Why?
function(Console.Out, value);
What am I doing wrong here?
Judging by your comment, you're misunderstanding what you're passing to Block. The variables argument of Block is to define local variables, rather than parameters.
var block = Expression.Block(
// Function block accepts two parameters
new List<ParameterExpression>() { inputParameter, inputParameter2 },
Expression.Call(inputParameter, method, Expression.Constant(value)));
Should be simply
var block = Expression.Block(Expression.Call(inputParameter, method, Expression.Constant(value)));
What you're currently doing is equivelant to:
void function(TextWriter tw, double d) {
TextWriter tw = default(TextWriter);
double d = default(double);
tw.WriteLine(22.55);
}
While you actually just want:
void function(TextWriter tw, double d) {
tw.WriteLine(22.55);
}
Also, you're not actually using your second parameter. The complete working code is:
var method = typeof(TextWriter).GetMethod("WriteLine", new Type[] { typeof(double) });
ParameterExpression inputParameter = Expression.Parameter(typeof(TextWriter));
ParameterExpression inputParameter2 = Expression.Parameter(typeof(double));
var block = Expression.Block(Expression.Call(inputParameter, method, inputParameter2));
var function = Expression.Lambda<Action<TextWriter, double>>(block, inputParameter, inputParameter2).Compile();
function(Console.Out, 35.5);
Your first code worked because you used a different overload of block. Instead of declaring a local variable for inputParameter, you passed it as an expression itself. So, the code looked like this:
void function(TextWriter tw, double d) {
tw; //Illegal for the C# compiler, but not illegal as an expression. This will do nothing.
tw.WriteLine(22.55);
}
I am looking to create a cachable lambda expression just from a methodinfo object retrieved via Type.GetMethod() without coding a typed cast of the function.
I have gotten everthing to work except for the cast from an compiled expression to an typed invokable function.
var parameters = Array.ConvertAll(method.GetParameters(), input => Expression.Parameter(input.ParameterType));
var instanceExp = Expression.Constant(_implementation);
var call = Expression.Call(instanceExp, method, parameters);
var exp = Expression.Lambda(call, parameters).Compile();
What is missing is:
Func<T1,T2,T3> castExp = (Func<T1,T2,T3>)exp;
What I would like to do is cast to a function with a specific number of parameters without specifying the specic type:
Func<object,object,object> castExp = (Func<object,object,object>)exp;
This way I could call exp(o1, o2, o3) without ever coding the types of o1 etc.
But there is a runtime error casting a function of type Func to Func.
How can I cast the function to some form of func<,,> that allows for passing parameters of unspecified type?
(Btw.: It is not an option to change the signature of the methods which are to be called.)
-1I have partially solved the issue I had.
What I was able to do is create expressions for methods of unknown signature if the method does not use in/out/ref parameters. In that case I had to fall back to the MethodInfo.Invoke call.
private object CallMethod(MethodInfo method, object obj, object[] parameters) {
// Methods with void as return must be cast to Action instead of Function
var voidMethod = voidMethod = method.ReturnType == typeof(void);
// Methods with ref parameters can be called but the parameters won't work.
var refMethod = Array.FindAll(method.GetParameters(), info => info.ParameterType.IsByRef;
var paramExprs = getParamExpr(method);
var paramTypes = getParamTypes(method, paramExprs);
var instanceExp = Expression.Convert(paramExprs[0], method.DeclaringType);
Expression call = null;
if (voidMethod) {
call = Expression.Call(instanceExp, method, paramTypes);
} else {
call = Expression.Convert(Expression.Call(instanceExp, method, paramTypes), typeof(object));
}
exp = Expression.Lambda(call, paramExprs).Compile();
if (voidMethod) {
switch (method.GetParameters().Length) {
case 0:
((Action<object>)exp)(_obj);
break;
case 1:
((Action<object, object>)exp)(_obj, parameters[0]);
break;
// Continue here with more case statements.
}
} else {
switch (method.GetParameters().Length) {
case 0:
result = ((Func<object, object>)exp)(_obj);
break;
case 1:
result = ((Func<object, object, object>)exp)(_obj, parameters[0]);
break;
// Continue here with more case statements
}
}
// Error handling omited
return result;
}
private List<ParameterExpression> getParamExpr(MethodInfo method) {
var list = new List<ParameterExpression>();
list.Add(Expression.Parameter(typeof(object), "obj"));
list.AddRange(Array.ConvertAll(method.GetParameters(), input => Expression.Parameter(typeof(object))));
return list;
}
private List<Expression> getParamTypes(MethodInfo method, List<ParameterExpression> inList) {
var list = new List<Expression>();
var methParams = method.GetParameters();
list.AddRange(
// Skip the first item as this is the object on which the method is called.
inList.Skip(1).Select(
input => Expression.Convert(
input,
Type.GetType(
methParams[inList.IndexOf(input)-1].ParameterType.FullName.Replace("&", string.Empty)))));
return list;
}
I hope it is complete as I have left out a lot of boilerplate for error handling etc.
The expression object can be cached but have to go through the casting everytime you want to call them.
I'm not 100% sure what you want to do but you could create a function that will return your altered function:
Func<T1, T2, T3> GetAlteredFunction<T1, T2, T3>(Func<T1, T2, T3> func)
{
//execute your logic and return the result
}
so then you can call
Func<object,object,object> castExp = GetAlteredFunction(exp);
I'm using a combination of reflection and expression trees, and want to pass back certain property accessors from a class to a calling method. My current code has a method traversing the class and returning a list of MemberExpressions. The caller then iterates over the member expressions and creates lambdas, which should then be called with an instance of the inspected class to return the value of the property.
Here is a sample of what it would look like without the method calls (Runnable in LINQPad):
void Main()
{
var t = new Test { Prop = "Test" };
var property = t.GetType().GetProperty("Prop");
var baseType = Expression.Parameter(typeof(Test), "baseType");
var memberAccess = Expression.MakeMemberAccess(baseType, property);
var lambda = Expression.Lambda<Func<Test, string>>(memberAccess, Expression.Parameter(typeof(Test), "baseType"));
var func = lambda.Compile();
var result = func(t);
result.Dump();
}
class Test {
public string Prop { get; set; }
}
This does not work, throwing this exception:
InvalidOperationException: variable 'baseType' of type 'UserQuery+Test' referenced from scope '', but it is not defined
However, if I change the creation of the lambda to this:
var lambda = Expression.Lambda<Func<Test, string>>(memberAccess, baseType);
That is, replace the Expression.Parameter with the variable used earlier, then it works. This is not (easily) possible in the scenario where I want to use it, since I would have to return the original parameter along with the list (I could return a tuple, of course, but I would prefer not to, if it is not necessary).
Why does it work like this? Inspecting the DebugView of the lambda, they are exactly the same no matter what approach is used:
.Lambda #Lambda1<System.Func`2[UserQuery+Test,System.String]>(UserQuery+Test $baseType)
{
$baseType.S
}
Yes, you need to refer ParameterExpression, used earlier. This won't compile too:
private String Foo(Test myParam)
{
return myAnotherParam.MyProperty;
}
With instatiating new ParameterExpression in lambda, you're doing the same thing (but note, when making lambda, you're doing it in reversed order - first, you're constructing a method body, then - a method declaration):
// return myAnotherParam.MyProperty;
var baseType = Expression.Parameter(typeof(Test), "baseType");
var memberAccess = Expression.MakeMemberAccess(baseType, property);
// private String Foo(MyClass myParam)
var lambda = Expression.Lambda<Func<Test, string>>(memberAccess, Expression.Parameter(typeof(Test), "baseType"));
I am trying to serialize something based upon meeting particular criteria.
To this end my original hope was to use attributes containing a lambda expression on an object's properties.
However, as this cannot be done I've settled for having a Func<T,bool> member within the class and passing the type (or first parameter type) and name of this Func through the property attribute. E.g.:
Func<SomeObject, bool> func = (p => p.Value == 4);
[FuncAtt(typeof(SomeObject), "func")]
public SomeObject PropertyName { get; set;}
In my serializer I need to call this Func<T, bool>.
Let's assume I have a Type t which is equal to typeof(SomeObject) in this case, or more abstractly, typeof(T). I can also get the Func<T,bool> itself, but only through reflection as an object.
My naive approach is something along these lines:
object func = typeof(MyClass).GetField(attribute.FuncName).GetValue(MyClassInstance);
Type funcType = typeof(Func<,>).MakeGenericType(attribute.Type, typeof(bool));
ParameterExpression p = Expression.Parameter(attribute.Type, objectToSerialize);
LambdaExpression l = Expression.Lambda(funcType, func, p); /* Won't work */
But this leads to the problem of casting a lambda to a delegate which is apparently erroneous.
I tried this in place of 'func':
(Expression)((Action)(() => func))
But that relies on func being a method call not a lambda.
So, can anyone point me in the right direction?
You can just do something like this, without need for expressions:
public static class Test
{
public static Predicate<int> func = s => s > 20;
}
and to get the value:
private void Form1_Load(object sender, EventArgs e)
{
var a = typeof(Test).GetField("func");
bool validates = ((Predicate<int>)a.GetValue(null)).Invoke(100);
}
edit to get the value without knowing the type:
bool validates = (bool)((Delegate)a.GetValue(null)).DynamicInvoke(100);
I think you can use Compile method of a lambda expression to cast it to a delegate.
here's what I found on MSDN:
The Expression<(Of <(TDelegate>)>)
type provides the Compile method,
that compiles the code represented by
the expression tree into an executable
delegate. This executable code is
equivalent to the executable code that
would have been generated had the
lambda expression been assigned to a
delegate type originally.
Here you can find it.
Not sure this is working sample, but this is the way:
// not sure what are you doing in this line, but assume it should return
// a method name specified in the attribute, e.g. "func" in your example.
// Also "func" must be a method (static one in my example) of SomeObject class
String funcname = typeof(MyClass).GetField(attribute.FuncName).GetValue(MyClassInstance);
ParameterExpression param = Expression.Parameter(typeof(SomeObject), "p");
MethodCallExpression call = Expression.Call(SomeObject, funcname, new Type[] { typeof(SomeObject), typeof(Boolean) }, param);
LambdaExpression lambda = Expression.Lambda<Func<SomeObject, Boolean>>(call, param);
now you can call the "func" method like this:
Boolean result = lambda.Compile()(SomeObjectInstance);