0

I have this code:

public class ClassMigration: Migration
{
   public override void RunMigration()
   {
      Utility.AddLanguage(new CultureInfo("en-PH"));
      Utility.AddLanguage(new CultureInfo("en-US"));
      Utility.ServerSetting("MAIN_LANGUAGE", "en-US"));
   }
}

I'm using reflection to get the the void method RunMigration() through I'm having a hard time to get what is inside the RunMigration() method.

What I am trying to achieve is to get all methods inside RunMigration and get all CultureInfo and ServerSetting parameter.

I already have this code:

private static IEnumerable<CultureInfo> GetLanguages(Type type)
{
   var languages = new List<CultureInfo>();
   if(type == null || !type.Name.Contains("Class")) return languages;
   foreach(var method in type.Getmethods())
   {
      // how to get those methods that is declared inside the void method.
   }
}

How do I achieve this?

Alvin Quezon
  • 1,089
  • 3
  • 11
  • 29
  • 4
    You cant do this with reflection, you will need to parse the IL or some other magic. Why are you in this situation? – TheGeneral Aug 19 '20 at 07:39
  • You have to obtain the method body and interpret the IL, see https://stackoverflow.com/questions/5667816/get-types-used-inside-a-c-sharp-method-body – CodeCaster Aug 19 '20 at 07:39
  • Hi @MichaelRandall, I need to fetch the `CultureInfo` because I was trying to export those data into excel file because some clients needs it to create a generic class generator. – Alvin Quezon Aug 19 '20 at 07:50
  • 2
    Sounds extremely hacky ... – Fildor Aug 19 '20 at 07:55
  • The question is misleading. What you actually want is to find out which methods are called, not which of them are declared inside a given method. – Georg Aug 19 '20 at 07:57
  • _"export those data into excel file because some clients needs it to create a generic class generator"_ - go write down your exact requirements. The Excel and "class generator" are out of scope. You want a piece of code that extracts some strings from a method. Can you use a T4 template? Show the input and output you want. – CodeCaster Aug 19 '20 at 07:59
  • If you have access to the source code, you can just use Roslyn analyzer to get the data out rather than use Reflection – zaitsman Aug 19 '20 at 23:41

0 Answers0