samedi 2 mai 2015

Is there any way to translate an Expression to C#?

I am modifying some code that uses reflection in order to generate Lamda Func delegate and I am getting there but I am finding it gets confusing at points and I lose track of exactly what I am reflecting.

This got me to wondering if there is any way to translate the actual C# string from the expression?

Like for instance, given the input of:

Expression.PropertyOrField(
            Expression.Constant(obj),
            "sample"
        );

It would output:

obj.sample

Obviously that is a simple example and doesn't need translating but this is my first time with reflection and will longer more complex statements its not so obvious for me at a glance what it does and would love to be able to just see the translation.

I did some searching and found a post that said as soon as the expression is assigned, the assigned value is now MSIL so you can't find the actual C# string that you would type into IDE.

I don't need to access this at runtime, I just want to translate the Expression to see what I have declared but I can't find anything at all on this (Possibly I am using incorrect terminology? Or perhaps this doesn't exist and I just need practice :P).

I have seen third party libraries that will turn a text string into an expression but no obvious way to translate the Expression to string.

Is it possible?





Aucun commentaire:

Enregistrer un commentaire