2

I want to cast any object from ParameterExpression class.

This is sample code (and pseudo what i want to say) :

public void Execute(Expression<Action<TService>> operation) { try { var param = operation.Parameters[0]; //myparameter to cast var obj = param as AnyCastObject; // I want to cast DoSomething(obj); // and do something this object without losing any assigned propery. operation.Compile().Invoke(OpenNewChannel); } finally { CloseChannel(); } } 

Edit:

This is my method body:

Execute(x => x.UserAuthentication(requestDto));

I want to manipulate requestDto.

7
  • You don't seem to comprehend how expressions work. param isn't the "value" of the parameter, it is a "parameter" in the sense "the descriptor of the parameter". "casting" the "descriptor of the parameter" doesn't have any sense. What you can do is create a new expression tree where the "value" of the parameter is casted to something else and then something is done. Commented Mar 31, 2015 at 9:08
  • I know param is not a value. I write this sample for pseudo that what i want yo say. But i dont understand what your saying. Commented Mar 31, 2015 at 9:32
  • So what do you want to cast? Do you want to have a "new" ParameterExpression for a different type (MyService instead of TService)? Do you want to change the parameter type of the operation from TService to MyService? Or what? Commented Mar 31, 2015 at 9:35
  • 1
    And when you operation.Compile(), do you want to compile the operation that was passed or do you want to compile an operation that you modified in some way? Commented Mar 31, 2015 at 9:36
  • In fact i want to copy or extract parameter before invoking. İf i want to create new instance of parameter then i use Activator.CreateInstance. But i dont want New instance i want to reference current instance parameter which passing to expression. Commented Mar 31, 2015 at 10:09

1 Answer 1

1

Here it is... With this you should be able to extract your requestDto.

Note that you don't need the Invoke() when you call compiled expressions.

operation.Compile()(OpenNewChannel); 

is enough.

Now, to extract the requestDto:

// Works for Execute(x => x.UserAuthentication(something)) // where something must be a constant value, a variable, // a field, a property var param = operation.Parameters[0]; //myparameter to cast var body = operation.Body as MethodCallExpression; if (body == null || body.Object != param || body.Method.Name != "UserAuthentication") { throw new NotSupportedException(); } // If you have a type for the parameter, replace it here: // object -> yourtype object requestValue; var constantExpression = body.Arguments[0] as ConstantExpression; if (constantExpression == null) { // For nearly all the types of expression, the only way // to extract the final value is to compile them and then // execute them (the execution is the last "()" ) // If you have a type for the parameter, replace it here: // Func<object> -> Func<yourtype> requestValue = Expression.Lambda<Func<object>>(body.Arguments[0]).Compile()(); } else { // Constant expression values can be directly extracted // If you have a type for the parameter, replace it here: // (yourtype)constantExpression.Value requestValue = constantExpression.Value; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.