public static void InitInstructionsCombobox(ComboBox cbo, MethodDefinition method, Instruction selectedIns, ref string[] instructionsStr)
        {
            cbo.Enabled = true;
            cbo.Items.Clear();
            cbo.DropDownStyle = ComboBoxStyle.DropDownList;

            Collection <Instruction> ic = method.Body.Instructions;

            if (instructionsStr == null)
            {
                instructionsStr    = new string[ic.Count + 1];
                instructionsStr[0] = "N/A";
                for (int i = 1; i < instructionsStr.Length; i++)
                {
                    instructionsStr[i] = InsUtils.GetInstructionText(ic, i - 1);
                }
            }

            cbo.Items.AddRange(instructionsStr);

            if (cbo.Items.Count > 0 && selectedIns != null)
            {
                cbo.SelectedIndex = ic.IndexOf(selectedIns) + 1;
            }
            else
            {
                cbo.SelectedIndex = 0;
            }
        }
        private void InitInstructions(ComboBox cbo)
        {
            cbo.Enabled = true;
            cbo.Items.Clear();
            cbo.DropDownStyle = ComboBoxStyle.DropDownList;

            Collection <Instruction> ic = _method.Body.Instructions;

            for (int i = 0; i < ic.Count; i++)
            {
                cbo.Items.Add(InsUtils.GetInstructionText(ic, i));
            }

            if (cbo.Items.Count > 0)
            {
                if (_ins.Operand is Instruction)
                {
                    cbo.SelectedIndex = ic.IndexOf(_ins.Operand as Instruction);
                }
                else
                {
                    cbo.SelectedIndex = 0;
                }
            }
        }
        public static string GetOperandText(Collection <Instruction> ic, int insIndex)
        {
            string text = String.Empty;

            if (insIndex < 0)
            {
                return(text);
            }

            object operand = ic[insIndex].Operand;

            if (operand == null)
            {
                return(text);
            }

            switch (operand.GetType().FullName)
            {
            case "System.String":
                text = String.Format("\"{0}\"", operand);
                break;

            case "System.Int32":
            case "System.Int16":
            case "System.Int64":
                long l = Convert.ToInt64(operand);
                if (l < 100)
                {
                    text = l.ToString();
                }
                else
                {
                    text = String.Format("0x{0:x}", l);
                }
                break;

            case "System.UInt32":
            case "System.UInt16":
            case "System.UInt64":
                ulong ul = Convert.ToUInt64(operand);
                if (ul < 100)
                {
                    text = ul.ToString();
                }
                else
                {
                    text = String.Format("0x{0:x}", ul);
                }
                break;

            case "System.Decimal":
                text = operand.ToString();
                break;

            case "System.Double":
                text = operand.ToString();
                break;

            case "System.Byte":
            case "System.SByte":
                text = String.Format("0x{0:x}", operand);
                break;

            case "System.Boolean":
                text = operand.ToString();
                break;

            case "System.Char":
                text = String.Format("'{0}'", operand);
                break;

            case "System.DateTime":
                text = operand.ToString();
                break;

            case "Mono.Cecil.Cil.Instruction":
                //text = GetAddressText(operand as Instruction);
                text = InsUtils.GetInstructionText(ic, ic.IndexOf(operand as Instruction));
                break;

            case "Mono.Cecil.Cil.Instruction[]":
                Instruction[] ins = operand as Instruction[];
                StringBuilder sb  = new StringBuilder();
                if (ins.Length > 0)
                {
                    for (int i = 0; i < ins.Length; i++)
                    {
                        //sb.AppendFormat("{0}, ", GetAddressText(ins[i]));
                        sb.AppendFormat("{0}, ", ic.IndexOf(ins[i]));
                    }
                    sb.Remove(sb.Length - 2, 2);
                }
                text = sb.ToString();
                break;

            //case "Mono.Cecil.MethodDefinition":
            //case "Mono.Cecil.MethodReference":
            //    text = (operand as MethodReference).Name;
            //    break;
            //case "Mono.Cecil.FieldDefinition":
            //case "Mono.Cecil.FieldReference":
            //    text = (operand as FieldReference).Name;
            //    break;
            default:
                text = operand.ToString();
                break;
            }

            return(text);
        }
        public void InitForm(frmClassEdit mainForm, MethodDefinition md, int insIndex, EditModes mode)
        {
            if (_method == md && _insIndex == insIndex)
            {
                return;
            }

            _mainForm = mainForm;
            _method   = md;
            _ilp      = md.Body.GetILProcessor();
            _insIndex = insIndex;
            _mode     = mode;
            Collection <Instruction> ic = _method.Body.Instructions;

            switch (mode)
            {
            case EditModes.Edit:
                _ins = ic[_insIndex];
                if (_ins.Previous == null)
                {
                    txtPrevIns.Text = String.Empty;
                }
                else
                {
                    txtPrevIns.Text = InsUtils.GetInstructionText(ic, ic.IndexOf(_ins.Previous));
                }
                if (_ins.Next == null)
                {
                    txtNextIns.Text = String.Empty;
                }
                else
                {
                    txtNextIns.Text = InsUtils.GetInstructionText(ic, ic.IndexOf(_ins.Next));
                }
                break;

            case EditModes.InsertAfter:
                _ins            = _ilp.Create(OpCodes.Nop);
                txtPrevIns.Text = InsUtils.GetInstructionText(ic, _insIndex);
                if (ic[_insIndex].Next == null)
                {
                    txtNextIns.Text = String.Empty;
                }
                else
                {
                    txtNextIns.Text = InsUtils.GetInstructionText(ic, ic.IndexOf(ic[_insIndex].Next));
                }
                break;

            case EditModes.InsertBefore:
                _ins            = _ilp.Create(OpCodes.Nop);
                txtNextIns.Text = InsUtils.GetInstructionText(ic, _insIndex);
                if (ic[_insIndex].Previous == null)
                {
                    txtPrevIns.Text = String.Empty;
                }
                else
                {
                    txtPrevIns.Text = InsUtils.GetInstructionText(ic, ic.IndexOf(ic[_insIndex].Previous));
                }
                break;
            }


            InitOpCodes(cboOpCode);
        }
        private void cboOpCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnSelectMethod.Enabled = false;

            if (cboOpCode.SelectedItem == null)
            {
                DisableOperand();
                return;
            }

            OpCode op = (OpCode)cboOpCode.SelectedItem;

            lblOpCodeInfo.Text = String.Format("FlowControl: {0}\nOpCodeType: {1}\nOperandType: {2}", op.FlowControl.ToString(), op.OpCodeType.ToString(), op.OperandType.ToString());
            Collection <Instruction> ic = _method.Body.Instructions;

            switch (op.OperandType)
            {
            case OperandType.InlineBrTarget:
            case OperandType.ShortInlineBrTarget:
                InitInstructions(cboOperand);
                break;

            case OperandType.InlineString:
                cboOperand.Enabled = true;
                cboOperand.Items.Clear();
                cboOperand.DropDownStyle = ComboBoxStyle.DropDown;
                if (_ins.Operand != null)
                {
                    cboOperand.Items.Add(_ins.Operand);
                    cboOperand.SelectedIndex = 0;
                }
                break;

            case OperandType.InlineNone:
                DisableOperand();
                break;

            case OperandType.InlineI:
            case OperandType.InlineI8:
            case OperandType.ShortInlineI:
            case OperandType.InlineR:
            case OperandType.ShortInlineR:
                cboOperand.Enabled = true;
                cboOperand.Items.Clear();
                cboOperand.DropDownStyle = ComboBoxStyle.DropDown;
                cboOperand.Items.Add(InsUtils.GetOperandText(ic, _insIndex));
                cboOperand.SelectedIndex = 0;
                break;

            case OperandType.InlineSwitch:
                cboOperand.Enabled = true;
                cboOperand.Items.Clear();
                cboOperand.DropDownStyle = ComboBoxStyle.DropDown;
                cboOperand.Items.Add(InsUtils.GetOperandText(ic, _insIndex));
                cboOperand.SelectedIndex = 0;
                Instruction[] switchIns = _ins.Operand as Instruction[];
                cboOperand.Items.Add("----------------------------------------");
                for (int i = 0; i < switchIns.Length; i++)
                {
                    cboOperand.Items.Add(
                        String.Format("{0} (0x{0:x}): {1}", i, InsUtils.GetInstructionText(ic, ic.IndexOf(switchIns[i])))
                        );
                }
                break;

            case OperandType.InlineField:
                if (_ins.Operand == null || _method.DeclaringType.Fields.Contains(_ins.Operand as FieldDefinition))
                {
                    InitFields(cboOperand);
                }
                else
                {
                    cboOperand.Enabled = true;
                    cboOperand.Items.Clear();
                    cboOperand.DropDownStyle = ComboBoxStyle.DropDownList;
                    cboOperand.Items.Add(InsUtils.GetOperandText(ic, _insIndex));
                    cboOperand.SelectedIndex = 0;
                }
                break;

            case OperandType.InlineVar:
            case OperandType.ShortInlineVar:
                InitVars(cboOperand);
                break;

            case OperandType.ShortInlineArg:
            case OperandType.InlineArg:
                InitParams(cboOperand);
                break;

            case OperandType.InlineMethod:
                InitMethods(cboOperand);
                btnSelectMethod.Enabled = true;
                break;

            case OperandType.InlineType:
                cboOperand.Enabled = true;
                cboOperand.Items.Clear();
                cboOperand.DropDownStyle = ComboBoxStyle.DropDown;
                if (_ins.Operand != null)
                {
                    cboOperand.Items.Add(_ins.Operand);
                    cboOperand.SelectedIndex = 0;
                }
                ClassEditHelper.InitDropdownTypes(cboOperand, _method.Module);
                break;

            default:
                cboOperand.Enabled = true;
                cboOperand.Items.Clear();
                cboOperand.DropDownStyle = ComboBoxStyle.DropDownList;
                if (_ins.Operand != null)
                {
                    cboOperand.Items.Add(_ins.Operand);
                    cboOperand.SelectedIndex = 0;
                }
                break;
            }
        }