9

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

curious
  • 107
  • 1
  • 4
  • 3
    It's very difficult, and may not be possible unless you define the `Delegate` as an `Expression`. See [this question](http://stackoverflow.com/questions/4922307/net-dump-statement-lambda-body-to-string) for details on how to extract lambda body as strings. – Xenolightning Mar 16 '15 at 01:30
  • 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. – Peter Duniho Mar 16 '15 at 01:46
  • 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: http://stackoverflow.com/q/648051/3538012, http://stackoverflow.com/q/8379204/3538012, http://stackoverflow.com/q/2305988/3538012 – Peter Duniho Mar 16 '15 at 01:51

1 Answers1

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!

  • 2
    Unfortunately the question is about converting a Func to a string and not an Expression as above. – pasx Sep 12 '19 at 11:22
  • 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. –  Sep 12 '19 at 14:29
  • 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://stackoverflow.com/questions/9377635/create-expression-from-func/9377714. – pasx Sep 13 '19 at 16:51
  • 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. –  Sep 14 '19 at 00:21
  • I came here looking for a solution to converting a func - not an expression - to string and left empty handed :( – Jon H Mar 04 '22 at 16:13