How to get il body of one method with roslyn?

I want to get il a single method in my C # source code file. I opened the solution with roslyn and found a method symbol as shown below

Roslyn.Compilers.Common.ISymbol s=GetMethodSymbolAtPosition (30); 

I have ISymbol, how to get il now?

+5
source share
1 answer

Unfortunately, IL generation is completely hidden inside Emit call to Roslyn. But I will give you just to get you started.

Suppose you start with an existing compilation:

 var initial = CSharpCompilation.Create("Existing") .AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location)) .AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(@" namespace Test { public class Program { public static void HelloWorld() { System.Console.WriteLine(""Hello World""); } } }")); var method = initial.GetSymbolsWithName(x => x == "HelloWorld").Single(); 

where method is your ISymbol . Then you can do the following:

 // 1. get source var methodRef = method.DeclaringSyntaxReferences.Single(); var methodSource = methodRef.SyntaxTree.GetText().GetSubText(methodRef.Span).ToString(); // 2. compile in-memory as script var compilation = CSharpCompilation.CreateScriptCompilation("Temp") .AddReferences(initial.References) .AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(methodSource, CSharpParseOptions.Default.WithKind(SourceCodeKind.Script))); using (var dll = new MemoryStream()) using (var pdb = new MemoryStream()) { compilation.Emit(dll, pdb); // 3. load compiled assembly var assembly = Assembly.Load(dll.ToArray(), pdb.ToArray()); var methodBase = assembly.GetType("Script").GetMethod(method.Name, new Type[0]); // 4. get il or even execute var il = methodBase.GetMethodBody(); methodBase.Invoke(null, null); } 

In a more complex case, you will probably need to fix the entire / initial compilation and get the generated method through reflection.

Refer to this post to find out what you can do with GetMethodBody() results.

+7
source

Source: https://habr.com/ru/post/1244275/


All Articles