예제 #1
0
        public MethodDlg(MethodBody body)
        {
            InitializeComponent();
            this.body = body;
            this.propertyGrid1.SelectedObject = body;
            this.disassembler = new MSILDisassembler(body);
            this.Text         = "Method Body of " + body.Method.MetaDataToken.ToString("X8");

            try
            {
                this.Text += " (" + body.Method.ToString() + ")";
            }
            catch
            {
                this.Text += " (method.ToString() failed)";
            }

            MSILInstruction[] instructions = disassembler.Disassemble();
            foreach (MSILInstruction instruction in instructions)
            {
                ListViewItem item = new ListViewItem(new string[] {
                    "IL_" + instruction.Offset.ToString("X4"),
                    BytesToString(instruction.OpCode.Bytes) + " " + BytesToString(instruction.OperandBytes),
                    instruction.OpCode.Name,
                    instruction.GetOperandString(),
                });
                listView1.Items.Add(item);
            }

            if (body.Variables != null && body.Variables.Length > 0)
            {
                foreach (VariableDefinition variable in body.Variables)
                {
                    listView2.Items.Add(new ListViewItem(new string[] {
                        variable.Index.ToString(),
                        variable.VariableType.FullName,
                    }));
                }
            }
            if (body.HasExtraSections)
            {
                foreach (MethodBodySection section in body.ExtraSections)
                {
                    if (section.IsExceptionHandler && section.ExceptionHandlers.Length > 0)
                    {
                        foreach (ExceptionHandler handler in section.ExceptionHandlers)
                        {
                            listView3.Items.Add(new ListViewItem(new string[] {
                                handler.Type == ExceptionHandlerType.Catch ? "Catch -> " + handler.CatchType.FullName : handler.Type.ToString(),
                                handler.TryStart.ToString("X4"),
                                handler.TryEnd.ToString("X4"),
                                handler.HandlerStart.ToString("X4"),
                                handler.HandlerEnd.ToString("X4"),
                                handler.FilterStart.ToString("X4")
                            }));
                        }
                    }
                }
            }
        }
        private void SearchAsync()
        {
            List <ListViewItem> items = new List <ListViewItem>();

            NETHeader netHeader = currentAssembly.NETHeader;

            MetaDataTable methodTable = netHeader.TablesHeap.GetTable(MetaDataTableType.Method);


            foreach (MetaDataMember member in methodTable.Members)
            {
                MethodDefinition methodDef = member as MethodDefinition;

                string methodName;
                string fullName;
                try
                {
                    methodName = methodDef.Name;
                }
                catch
                {
                    methodName = string.Empty;
                }
                try
                {
                    fullName = methodDef.FullName;
                }
                catch
                {
                    fullName = string.Empty;
                }

                Invoke(new Action <string, double, double>(ProcessCallBack), "Scanning " + (fullName == string.Empty ? methodDef.MetaDataToken.ToString("X8") : fullName), member.MetaDataToken - ((int)member.TableType << 24), methodTable.Members.Count);
                if (methodDef.HasBody)
                {
                    try
                    {
                        MSILDisassembler  disassembler = new MSILDisassembler(methodDef.Body);
                        MSILInstruction[] instructions = disassembler.Disassemble();

                        foreach (MSILInstruction instruction in instructions)
                        {
                            if (instruction.OpCode.Code == MSILCode.Ldstr)
                            {
                                string value = instruction.Operand as string;
                                items.Add(new ListViewItem(new string[] {
                                    value,
                                    "IL_" + instruction.Offset.ToString("X4"),
                                    methodDef.MetaDataToken.ToString("X8"),
                                    methodName,
                                    fullName,
                                })
                                {
                                    Tag = methodDef
                                });
                            }
                        }
                    }
                    catch
                    {
                        // handle
                    }
                }
            }

            Invoke(new Action <List <ListViewItem> >(SearchCallBack), items);
        }