Get types used inside a C# method body
Asked Answered
F

8

18

Is there a way to get all types used inside C# method?

For example,

public int foo(string str)
{
    Bar bar = new Bar();
    string x = "test";
    TEST t = bar.GetTEST();
}

would return: Bar, string and TEST.

All I can get now is the method body text using EnvDTE.CodeFunction. Maybe there is a better way to achieve it than trying to parse this code.

Farfetched answered 14/4, 2011 at 18:26 Comment(0)
M
12

I'm going to take this opportunity to post up a proof of concept I did because somebody told me it couldn't be done - with a bit of tweaking here and there, it'd be relatively trivial to extend this to extract out all referenced Types in a method - apologies for the size of it and the lack of a preface, but it's somewhat commented:

void Main()
{
    Func<int,int> addOne = i => i + 1;
    Console.WriteLine(DumpMethod(addOne));
    Func<int,string> stuff = i =>
    {
        var m = 10312;        
        var j = i + m;
        var k = j * j + i;
        var foo = "Bar";
        var asStr = k.ToString();
        return foo + asStr;
    };
    Console.WriteLine(DumpMethod(stuff));

    Console.WriteLine(DumpMethod((Func<string>)Foo.GetFooName));

    Console.WriteLine(DumpMethod((Action)Console.Beep));
}

public class Foo
{
    public const string FooName = "Foo";
    public static string GetFooName() { return typeof(Foo).Name + ":" + FooName; }
}

public static string DumpMethod(Delegate method)
{
    // For aggregating our response
    StringBuilder sb = new StringBuilder();

    // First we need to extract out the raw IL
    var mb = method.Method.GetMethodBody();
    var il = mb.GetILAsByteArray();

    // We'll also need a full set of the IL opcodes so we
    // can remap them over our method body
    var opCodes = typeof(System.Reflection.Emit.OpCodes)
        .GetFields()
        .Select(fi => (System.Reflection.Emit.OpCode)fi.GetValue(null));

    //opCodes.Dump();

    // For each byte in our method body, try to match it to an opcode
    var mappedIL = il.Select(op => 
        opCodes.FirstOrDefault(opCode => opCode.Value == op));

    // OpCode/Operand parsing: 
    //     Some opcodes have no operands, some use ints, etc. 
    //  let's try to cover all cases
    var ilWalker = mappedIL.GetEnumerator();
    while(ilWalker.MoveNext())
    {
        var mappedOp = ilWalker.Current;
        if(mappedOp.OperandType != OperandType.InlineNone)
        {
            // For operand inference:
            // MOST operands are 32 bit, 
            // so we'll start there
            var byteCount = 4;
            long operand = 0;
            string token = string.Empty;

            // For metadata token resolution            
            var module = method.Method.Module;
            Func<int, string> tokenResolver = tkn => string.Empty;
            switch(mappedOp.OperandType)
            {
                // These are all 32bit metadata tokens
                case OperandType.InlineMethod:        
                    tokenResolver = tkn =>
                    {
                        var resMethod = module.SafeResolveMethod((int)tkn);
                        return string.Format("({0}())", resMethod == null ? "unknown" : resMethod.Name);
                    };
                    break;
                case OperandType.InlineField:
                    tokenResolver = tkn =>
                    {
                        var field = module.SafeResolveField((int)tkn);
                        return string.Format("({0})", field == null ? "unknown" : field.Name);
                    };
                    break;
                case OperandType.InlineSig:
                    tokenResolver = tkn =>
                    {
                        var sigBytes = module.SafeResolveSignature((int)tkn);
                        var catSig = string
                            .Join(",", sigBytes);
                        return string.Format("(SIG:{0})", catSig == null ? "unknown" : catSig);
                    };
                    break;
                case OperandType.InlineString:
                    tokenResolver = tkn =>
                    {
                        var str = module.SafeResolveString((int)tkn);
                        return string.Format("('{0}')",  str == null ? "unknown" : str);
                    };
                    break;
                case OperandType.InlineType:
                    tokenResolver = tkn =>
                    {
                        var type = module.SafeResolveType((int)tkn);
                        return string.Format("(typeof({0}))", type == null ? "unknown" : type.Name);
                    };
                    break;
                // These are plain old 32bit operands
                case OperandType.InlineI:
                case OperandType.InlineBrTarget:
                case OperandType.InlineSwitch:
                case OperandType.ShortInlineR:
                    break;
                // These are 64bit operands
                case OperandType.InlineI8:
                case OperandType.InlineR:
                    byteCount = 8;
                    break;
                // These are all 8bit values
                case OperandType.ShortInlineBrTarget:
                case OperandType.ShortInlineI:
                case OperandType.ShortInlineVar:
                    byteCount = 1;
                    break;
            }
            // Based on byte count, pull out the full operand
            for(int i=0; i < byteCount; i++)
            {
                ilWalker.MoveNext();
                operand |= ((long)ilWalker.Current.Value) << (8 * i);
            }

            var resolved = tokenResolver((int)operand);
            resolved = string.IsNullOrEmpty(resolved) ? operand.ToString() : resolved;
            sb.AppendFormat("{0} {1}", 
                    mappedOp.Name, 
                    resolved)
                .AppendLine();                    
        }
        else
        {
            sb.AppendLine(mappedOp.Name);
        }
    }
    return sb.ToString();
}

public static class Ext
{
    public static FieldInfo SafeResolveField(this Module m, int token)
    {
        FieldInfo fi;
        m.TryResolveField(token, out fi);
        return fi;
    }
    public static bool TryResolveField(this Module m, int token, out FieldInfo fi)
    {
        var ok = false;
        try { fi = m.ResolveField(token); ok = true; }
        catch { fi = null; }    
        return ok;
    }
    public static MethodBase SafeResolveMethod(this Module m, int token)
    {
        MethodBase fi;
        m.TryResolveMethod(token, out fi);
        return fi;
    }
    public static bool TryResolveMethod(this Module m, int token, out MethodBase fi)
    {
        var ok = false;
        try { fi = m.ResolveMethod(token); ok = true; }
        catch { fi = null; }    
        return ok;
    }
    public static string SafeResolveString(this Module m, int token)
    {
        string fi;
        m.TryResolveString(token, out fi);
        return fi;
    }
    public static bool TryResolveString(this Module m, int token, out string fi)
    {
        var ok = false;
        try { fi = m.ResolveString(token); ok = true; }
        catch { fi = null; }    
        return ok;
    }
    public static byte[] SafeResolveSignature(this Module m, int token)
    {
        byte[] fi;
        m.TryResolveSignature(token, out fi);
        return fi;
    }
    public static bool TryResolveSignature(this Module m, int token, out byte[] fi)
    {
        var ok = false;
        try { fi = m.ResolveSignature(token); ok = true; }
        catch { fi = null; }    
        return ok;
    }
    public static Type SafeResolveType(this Module m, int token)
    {
        Type fi;
        m.TryResolveType(token, out fi);
        return fi;
    }
    public static bool TryResolveType(this Module m, int token, out Type fi)
    {
        var ok = false;
        try { fi = m.ResolveType(token); ok = true; }
        catch { fi = null; }    
        return ok;
    }
}
Marela answered 18/7, 2013 at 21:58 Comment(1)
this works perfect for synchro methods, but its don` work for async methods (((Chic
V
2

If you can access the IL for this method, you might be able to do something suitable. Perhaps look at the open source project ILSpy and see whether you can leverage any of their work.

Vachon answered 14/4, 2011 at 18:53 Comment(3)
As far I know I can't, it might be used before build phase on existing project.Farfetched
@Silx, fair enough. So what do you have access to? To be 100% sure what types are involved you will need the string form of the entire class (at least the using statements) as well as the list of referenced assemblies for the project.Vachon
I can get whole class code (not sure for partial classes but let's assume it's possible) I've checked the assemblies and all external like .dll can be accessed but cross project reference in same solution may not present before solution build.Farfetched
B
1

As others have mentioned, if you had the DLL you could use something similar to what ILSpy does in its Analyze feature (iterating over all the IL instructions in the assembly to find references to a specific type).

Otherwise, there is no way to do it without parsing the text into a C# Abstract Syntax Tree, AND employing a Resolver - something that can understand the semantics of the code well enough to know if "Bar" in your example is indeed a name of a type that is accessible from that method (in its "using" scope), or perhaps the name of a method, member field, etc... SharpDevelop contains a C# parser (called "NRefactory") and also contains such a Resolver, you can look into pursuing that option by looking at this thread, but beware that it is a fair amount of work to set it up to work right.

Beggar answered 16/4, 2011 at 0:5 Comment(0)
T
1

I just posted an extensive example of how to use Mono.Cecil to do static code analysis like this.

I also show a CallTreeSearch enumerator class that can statically analyze call trees, looking for certain interesting things and generating results using a custom supplied selector function, so you can plug it with your 'payload' logic, e.g.

    static IEnumerable<TypeUsage> SearchMessages(TypeDefinition uiType, bool onlyConstructions)
    {
        return uiType.SearchCallTree(IsBusinessCall,
               (instruction, stack) => DetectTypeUsage(instruction, stack, onlyConstructions));
    }

    internal class TypeUsage : IEquatable<TypeUsage>
    {
        public TypeReference Type;
        public Stack<MethodReference> Stack;

        #region equality
        // ... omitted for brevity ...
        #endregion
    }

    private static TypeUsage DetectTypeUsage(
        Instruction instruction, IEnumerable<MethodReference> stack, bool onlyConstructions)
    {
        TypeDefinition resolve = null;
        {
            TypeReference tr = null;

            var methodReference = instruction.Operand as MethodReference;
            if (methodReference != null)
                tr = methodReference.DeclaringType;

            tr = tr ?? instruction.Operand as TypeReference;

            if ((tr == null) || !IsInterestingType(tr))
                return null;

            resolve = tr.GetOriginalType().TryResolve();
        }

        if (resolve == null)
            throw new ApplicationException("Required assembly not loaded.");

        if (resolve.IsSerializable)
            if (!onlyConstructions || IsConstructorCall(instruction))
                return new TypeUsage {Stack = new Stack<MethodReference>(stack.Reverse()), Type = resolve};

        return null;
    }

This leaves out a few details

  • implementation of IsBusinessCall, IsConstructorCall and TryResolve as these are trivial and serve as illustrative only

Hope that helps

Trinitarianism answered 21/4, 2011 at 9:9 Comment(0)
D
0

With reflection you can get the method. This returns a MethodInfo object, and with this object you cannot get the types which are used in the method. So I think the answer is that you cannot get this native in C#.

Demount answered 14/4, 2011 at 18:33 Comment(0)
M
0

The closest thing to that that I can think of are expression trees. Take a look at the documentation from Microsoft.

They are very limited however and only work on simple expressions and not full methods with statement bodies.

Edit: Since the intention of the poster was to find class couplings and used types, I would suggest using a commercial tool like NDepend to do the code analysis as an easy solution.

Miskolc answered 14/4, 2011 at 18:46 Comment(3)
Any suggestion how to parse string to Expression Tree or lamda?Farfetched
What is the use that you intend for this functionality? Expression trees might be too limited for your needs. They are more meant for dynamic modification of code during runtime.Miskolc
I need a list of types used inside methods. It would be used to find class couplings (references, calls whatever you call it). So basicly I need to check if class A is calling class B anywhere in it's body. Since there might be thousends of classes in project brute force search is not an option.Farfetched
L
0

This definitely cannot be done from reflection (GetMethod(), Expression Trees, etc.). As you mentioned, using EnvDTE's CodeModel is an option since you get line-by-line C# there, but using it outside Visual Studio (that is, processing an already existing function, not in your editor window) is nigh-impossible, IMHO.

But I can recommend Mono.Cecil, which can process CIL code line-by-line (inside a method), and you can use it on any method from any assembly you have reference to. Then, you can check every line if it is a variable declaration (like string x = "test", or a methodCall, and you can get the types involved in those lines.

Likker answered 14/4, 2011 at 19:50 Comment(0)
S
0

For today you can get the information by calling LocalVariables from the MethodBody class. For example:

public class Example
{
    public static void Main()
    {
        var methodInfo = typeof(Example).GetMethod("foo");
        var methodBody = methodInfo?.GetMethodBody();
        
        foreach (var localVariableInfo in methodBody.LocalVariables)
        {
            Console.WriteLine("Type: {0}", localVariableInfo.LocalType);
        }
        //result:
        //Local variable type: Bar
        //Local variable type: System.String
        //Local variable type: TEST
    }
    
    public void foo(string str)
    {
        Bar bar = new Bar();
        string x = "test";
        TEST t = bar.GetTEST();
    }
}
Selfpropulsion answered 31/1 at 15:29 Comment(1)
Note that this does not work in release mode for provided example.Macmahon

© 2022 - 2024 — McMap. All rights reserved.