获取C#方法体内使用的类型

时间:2011-04-14 18:26:41

标签: c# .net visual-studio envdte

有没有办法在C#方法中使用所有类型?

例如,

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

会返回:Bar,string和TEST。

我现在可以获得的是使用EnvDTE.CodeFunction的方法正文。也许有更好的方法来实现它,而不是尝试解析这段代码。

7 个答案:

答案 0 :(得分:8)

我将借此机会发布我做过的概念验证,因为有人告诉我无法做到这一点 - 在这里和那里进行一些调整,将此扩展到相关是相对微不足道的提取出一个方法中所有被引用的类型 - 为它的大小道歉并且没有前言,但它有点评论:

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;
    }
}

答案 1 :(得分:2)

如果您可以访问此方法的IL,您可能可以做一些合适的事情。也许看看开源项目ILSpy,看看你是否可以利用他们的任何工作。

答案 2 :(得分:1)

正如其他人所提到的,如果你有DLL,你可以使用与ILSpy does in its Analyze feature类似的东西(迭代程序集中的所有IL指令来查找对特定类型的引用)。

否则,如果不将文本解析为C#抽象语法树,并使用解析器,就无法做到这一点 - 这样可以很好地理解代码的语义,以便知道示例中的“Bar”是否确实如此可以从该方法访问的类型的名称(在其“使用”范围内),或者可能是方法,成员字段等的名称... SharpDevelop包含一个C#解析器(称为“NRefactory”)并且还包含一个解析器,您可以通过查看this thread来考虑寻求该选项,但要注意,将其设置为正常工作是相当多的工作。

答案 3 :(得分:1)

我刚刚发布了一个广泛的 how to use Mono.Cecil to do static code analysis 示例。

我还展示了一个CallTreeSearch枚举器类,它可以静态分析调用树,查找某些有趣的东西并使用自定义提供的选择器函数生成结果,因此您可以使用“有效负载”逻辑插入它,例如

    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;
    }

这遗漏了一些细节

  • IsBusinessCallIsConstructorCallTryResolve的实施,因为这些是微不足道的,只是说明性的

希望有所帮助

答案 4 :(得分:0)

通过反思你可以get the method。这将返回一个MethodInfo对象,使用此对象无法获取方法中使用的类型。所以我认为答案是你不能在C#中获得这个原生。

答案 5 :(得分:0)

我能想到的最接近的是表达树。看看the documentation from Microsoft

它们非常有限,只适用于简单表达式,而不适用于带语句体的完整方法。

编辑:由于海报的目的是找到类联接和使用类型,我建议使用像NDepend这样的商业工具来进行代码分析,作为一种简单的解决方案。

答案 6 :(得分:0)

这绝对不能通过反射(GetMethod(),Expression Trees等)来完成。正如您所提到的,使用EnvDTE的CodeModel是一个选项,因为您在那里逐行C#,但在Visual Studio外部使用它(即处理现有的函数,而不是在编辑器窗口中)是 - 不可能,恕我直言。

但我可以推荐Mono.Cecil,它可以逐行处理CIL代码(在方法内),并且可以在任何引用的程序集中使用它。然后,您可以检查每一行是否为变量声明(如字符串x =“test”或methodCall,并且您可以获取这些行中涉及的类型。