1

I want to know if is possible to get the method body via c# reflection and identify expressions, conditions, loops etc.

For example, assume I have a class,

class Employee
{
private int Number1{get;set;}
private int Number2{get;set;}

public int GetNumber()
{
  if(Number1>0)
  {
     return Number1;
  }
  else if(Number2>0)
  {
     return Number2;
  }
  return Number1 + Number2;
  }
}

So here if you see the above class is having a method with some conditions. In reflection I want to read the method body and identify these conditions. Like,

var methodBody = methodInfo.GetMethodBody();
methodBody.Condition?????
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 1
    I don't think you can make it work like that, especially when the code is optimized because a lot of your conditions may be removed. What is the actual problem you are trying to solve here? – Ron Beyer Jul 28 '15 at 14:49
  • Reflection is for extracting _metadata_ - it is not a decompiler. – D Stanley Jul 28 '15 at 14:51
  • There's a reason that the [System.Linq.Expressions](https://msdn.microsoft.com/en-us/library/system.linq.expressions(v=vs.110).aspx) namespace came into existence to support Linq, and it wasn't that they wanted to do all of the work twice. – Damien_The_Unbeliever Jul 28 '15 at 14:55
  • I am trying to build a graph of possible call paths for a given method. Like, if the user gives me the class name I wanna parse all methods and tell the user, "hey boss, your class has these many methods and each has these many possible execution paths.." with graph.. – user3383568 Jul 29 '15 at 10:23

0 Answers0