Convert Func delegate to a string
Asked Answered
S

1

10

Is there any way to convert an existing Func delegate to a string like that:

Func<int, int> func = (i) => i*2;
string str = someMethod(func); // returns "Func<int, int> func = (i) => i*2"

or at least smth close to it

Sonorous answered 16/3, 2015 at 0:20 Comment(3)
It's very difficult, and may not be possible unless you define the Delegate as an Expression. See this question for details on how to extract lambda body as strings.Cooperation
Is there any way? Yes, of course. But a fully general-purpose way is a lot of work. See tools like Reflector and dotPeek for examples of the kind of logic you'd have to implement. It's a highly non-trivial task. In any case, to achieve a StackOverflow-worthy/answerable question, you will need to significantly constrain the question with additional details, to avoid it being overly broad.Oud
Here are some posts that, while not strictly duplicates of this question, give some additional insight into the challenge of what you're asking about, as well as mention some possible alternatives, depending on your actual scenario: https://mcmap.net/q/650362/-converting-il-to-c-code/3538012, https://mcmap.net/q/1168546/-is-there-any-c-decompiler-that-can-show-the-coding-almost-identically-to-how-it-was-written/3538012, https://mcmap.net/q/1168547/-is-there-a-library-that-can-decompile-a-method-into-an-expression-tree-with-support-for-clr-4-0/3538012Oud
L
3

I found a similar question here. It more or less boils down to:

By @TheCloudlessSky,

Expression<Func<Product, bool>> exp = (x) => (x.Id > 5 && x.Warranty != false);

string expBody = ((LambdaExpression)exp).Body.ToString(); 
// Gives: ((x.Id > 5) AndAlso (x.Warranty != False))

var paramName = exp.Parameters[0].Name;
var paramTypeName = exp.Parameters[0].Type.Name;

// You could easily add "OrElse" and others...
expBody = expBody.Replace(paramName + ".", paramTypeName + ".")
             .Replace("AndAlso", "&&");


Console.WriteLine(expBody);
// Output: ((Product.Id > 5) && (Product.Warranty != False))

It doesn't return the Func<int, int> func = (i) => part like in your question, but it does get the underlying expression!

Lalise answered 1/4, 2018 at 20:19 Comment(5)
Unfortunately the question is about converting a Func to a string and not an Expression as above.Exposure
I believe the question specified the type of output desired. In this case, the expression is desired in addition to the function. Refer to the comment portion in the question.Lalise
I believe the question is: "Convert Func delegate to a string". Yes, someone mentioned in the comments that it is only possible for Expression and not Func. It would be better to make it obvious in your answer too and also mention that converting the Func to an Expression will not produce the desired result cf. https://mcmap.net/q/112607/-create-expression-from-func/….Exposure
That merely indicates the question was poorly titled. If you read the entire post, they specify what they are looking for. In this case, they want the expression including the part that comes directly before it. My answer solves that, but as i already indicate, it does not include the part before it. Not sure what you're getting at because they didn't ask for anything else.Lalise
I came here looking for a solution to converting a func - not an expression - to string and left empty handed :(Piet

© 2022 - 2024 — McMap. All rights reserved.