Пример #1
0
 private IEnumerable <MethodInfo> GetMethodInfos(MethodDefinitionHandleCollection methodDefinitionHandles)
 {
     foreach (var methodHandle in methodDefinitionHandles)
     {
         yield return(GetMethod(methodHandle));
     }
 }
        //<SnippetPrintMethods>
        static void PrintMethods(PEReader reader, MetadataReader mr, TypeDefinition tdef)
        {
            MethodDefinitionHandleCollection methods = tdef.GetMethods();

            foreach (MethodDefinitionHandle mdefh in methods)
            {
                MethodDefinition mdef  = mr.GetMethodDefinition(mdefh);
                string           mname = mr.GetString(mdef.Name);
                Console.WriteLine($"Method: {mname}");

                // Get the relative address of the method body in the executable
                int rva = mdef.RelativeVirtualAddress;

                if (rva == 0)
                {
                    Console.WriteLine("Method body not found");
                    Console.WriteLine();
                    continue;
                }

                // Get method body information
                MethodBodyBlock mb = reader.GetMethodBody(rva);
                Console.WriteLine($"  Maximum stack size: {mb.MaxStack}");
                Console.WriteLine($"  Local variables initialized: {mb.LocalVariablesInitialized}");

                byte[] il = mb.GetILBytes();
                Console.WriteLine($"  Method body size: {il.Length}");
                Console.WriteLine($"  Exception regions: {mb.ExceptionRegions.Length}");
                Console.WriteLine();

                foreach (var region in mb.ExceptionRegions)
                {
                    Console.WriteLine(region.Kind.ToString());
                    Console.WriteLine($"  Try block offset: {region.TryOffset}");
                    Console.WriteLine($"  Try block length: {region.TryLength}");
                    Console.WriteLine($"  Handler offset: {region.HandlerOffset}");
                    Console.WriteLine($"  Handler length: {region.HandlerLength}");
                    Console.WriteLine();
                }
            }
        }
        public void ParseMethods(MethodDefinitionHandleCollection methods, string name, MethodSignature signature)
        {
            foreach (var methodHandle in methods)
            {
                var method = Reader.GetMethodDefinition(methodHandle);
                var methodName = Reader.GetString(method.Name);

                if (name == string.Empty || name == methodName)
                {

                    if (signature != null)
                    {
                        var sig = ILReader.ParseMethodSignature(this, Reader.GetBlobBytes(method.Signature));
#if DEBUG_SIG
                        Console.WriteLine("=== Sig ===");
                        foreach (var b in sig)
                            Console.Write("{0:X2} ", b);
                        Console.WriteLine();
#endif
                        if (sig != signature)
                            continue;
                    }

                    var m = ParseMethodHeader(methodHandle, false);
                    ParseMethodBody(m);
                }
            }
        }