0

I Have the following function:

protected static T WrapException<T>(Func<T> function)
{
    T result = default(T);

    WrapException(delegate
    {
        result = function();
    });
    return result;
}

I use this function in all of my WCF Project services endpoint:

public List<ResultDTO> GetSomething(SessionDto sessionDto)
{
    return WrapException(() => _someFacade.Get(sessionDto));
}

what i need is to change the value of sessionDto at WrapException before the action is called.

I tried to access it by making the following cast:

((dynamic)function.Target)

enter image description here

it works fine, but when i try to access the arguments, it throws an exception.

enter image description here

What am i doing wrong?

Obs: sorry by my english, not my native language

Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
Douglas Franco
  • 591
  • 5
  • 13
  • If you change your windows language to English before you take your screenshots your error messages should be in English. This will greatly help other people on the site to help you. – Scott Chamberlain Dec 17 '15 at 21:40
  • 1
    At which point do you wanna access/change `sessionDto`? – Jan Köhler Dec 17 '15 at 21:41
  • Would changing WrapException to `T WrapException(Expression> function)` be acceptable? it makes the problem a lot easier. – Scott Chamberlain Dec 17 '15 at 21:43
  • Possible duplicate of [Func<> getting the parameter info](http://stackoverflow.com/questions/17692638/func-getting-the-parameter-info) – stuartd Dec 17 '15 at 21:43
  • @stuartd i need to change the value of parameter, access it i already can. – Douglas Franco Dec 17 '15 at 21:48
  • @ScottChamberlain i can try. – Douglas Franco Dec 17 '15 at 21:48
  • the exception in english say " 'object' does not contain definition to 'sessionDto' " – Douglas Franco Dec 17 '15 at 21:49
  • @khlr i need to change. – Douglas Franco Dec 17 '15 at 21:50
  • 1
    But you have shown that you cannot see the value, let alone change it - you're just seeing metadata in the watch window. As it says in the linked question - "If you want to get the parameter you will have to pass expression. By passing a "Func" you will pass the compiled lambda, so you cannot access the expression tree any more". – stuartd Dec 17 '15 at 22:00
  • 1
    Note also from [How to: Modify Expression Trees](https://msdn.microsoft.com/en-us/library/bb546136.aspx) - "Expression trees are immutable, which means that they cannot be modified directly. To change an expression tree, you must create a copy of an existing expression tree and when you create the copy, make the required changes." – stuartd Dec 17 '15 at 22:03
  • @stuartd Ok, i changed my function to Expression>. How can i change the value before it is invoked ? – Douglas Franco Dec 17 '15 at 22:04
  • There's an example in [Replace parameter in lambda expression](http://stackoverflow.com/questions/11159697/replace-parameter-in-lambda-expression) – stuartd Dec 17 '15 at 22:12
  • @stuartd in all of examples i found ways to change value from Expression> in my case i have only Expression> what is the difference – Douglas Franco Dec 17 '15 at 22:29
  • 1
    `Func` has a parameter of type T and returns an entity of type object - `Func` has no parameters and returns an entity of type T. – stuartd Dec 17 '15 at 22:43
  • @ScottChamberlain it is surprisingly hard to get rid of all .NET Framework localization! I failed to do it on one production server which is now spewing a mix of German and English. – usr Dec 17 '15 at 23:13
  • I still don't understand the question - if you don't want the parameter value you've actually been given, why don't you change the caller to provide the one you want? There's not enough context about why you're trying to do this... – Jon Skeet Dec 18 '15 at 12:30
  • i need to change the parameters of the called function before it is invoked. – Douglas Franco Dec 18 '15 at 12:31

1 Answers1

2

The answer to your question is, it's impossible to do this in a safe way.

You could try to edit sessionDto in the Target object. This is an auto-generated lambda closure field. This structure is not documented. It could change at any time.

Even if you make it work this is bad architecture because you are reaching into internals of another piece of code.

Probably, you should refactor your code so that it is not necessary to edit local variables of another function(!).

usr
  • 168,620
  • 35
  • 240
  • 369
  • the problem is that i cannot change the sessionDto in Target, when i try to do this, it thrown the exeption in the last screenshot of my question. 'Object' do not contains 'sessionDto' – Douglas Franco Dec 18 '15 at 12:38
  • You can't use dynamic to access private members. Use Reflection. – usr Dec 18 '15 at 19:42
  • i did it, on take a look on the questions screenshot – Douglas Franco Dec 19 '15 at 13:25
  • What do you mean by that? I understand that you "did this" but it's wrong and you shouldn't do that. Use Reflection. – usr Dec 19 '15 at 14:24