mardi 23 mars 2021

Is there a C# function to print the function instructions as String using reflection?

I would like to print the content of my Function as String, and I have tried different reflection methods and also a lib I found on GitHub, However, what it does is to print the instructions in IL code. Also, I'd like to mention that I have a lambda code, so that, it is printing instructions with the label lambda instead of the instructions I need to. It does make sense. This solution is based on the IL code created for running in the machine, but I need the version of the class document. This is for an automated tool to look for different places in the code using lambda expressions with a specific function.

I have tried the following but the result is not expected:

In addition, as a different approach, I tried to get the classpath to use the ReadLines C# function in order to read all lines and look for my target function, but, this is returning the assembly path but I cannot use ReadLines since the assembly is not a document text.

I need to do something like the following:

using System;
class Foo 
{
  public List<Item> SetItems() 
  {
      List<Item> items = new List<Item>();
      return new List<SomeType>(items).ForEach(
        i => AssignNewType(i)
      ).FindAll(i=> i.type == SomeType.newType);
  }
}

Another class:

ClassReader.Print(Foo, "SetItems");

Expected Result as String:

Function content: List<Item> items = new List<Item>(); return new List<SomeType>(items).ForEach( i => AssignNewType(i)).FindAll(i=> i.type == SomeType.newType);

Thanks for your help!





Aucun commentaire:

Enregistrer un commentaire