get local variable names defined in a method using reflection in C#.net

But again how do i map this information with the information(LocalVariables defined in a method)"

With SymReader the locals can be found via ISymbolMethod.RootScope. Here's a short example:

var locals = typeof(TestClass).GetMethod("GetStringRepresentation").GetMethodBody().LocalVariables;

foreach (var scope in met.RootScope.GetChildren()) {
    foreach (var loc in scope.GetLocals()) {
        Console.WriteLine("{0}: {1} {2}", loc.AddressField1, locals.FirstOrDefault(l => l.LocalIndex == loc.AddressField1).LocalType, loc.Name);
    }
}

It's incomplete (should be recursive to be able to enumerate all scopes, may need some sanity checks like verifying if the AddressKind is ILOffset) but you should get the idea.

posted @ 2018-01-30 17:12  何人之名  阅读(178)  评论(0)    收藏  举报