Get ToString-expression from compiled class using reflection?

53 views Asked by At

I have a class such as:

public class Person
{
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public override string ToString() => $"{LastName}, {FirstName}";
}

and I would like to be able to extract the ToString-expression in runtime using reflection like so:

var exp = typeof(Person).GetMethod("ToString").GetMethodBodyAsString();

where exp would be something like this: "${LastName}, {FirstName}" or at least enough to make it possible to reconstruct the expression with some more effort..

Is it possible to extract this from the IL-code returned by reflection on the method-body?

0

There are 0 answers