Exemplo n.º 1
0
        private static void AppendOperand(TextBuilder text, OperandType operandType, object operand)
        {
            text.Append(' ');
            switch (operandType)
            {
            case OperandType.ShortInlineBrTarget:
            case OperandType.InlineBrTarget:
            {
                if (operand is Instruction opInst)
                {
                    AppendLabel(text, opInst);
                }
                else
                {
                    throw new InvalidOperationException();
                }

                break;
            }

            case OperandType.InlineSwitch:
            {
                if (operand is Instruction[] labels)
                {
                    text.AppendDelimit(',', labels, AppendLabel !);
                }
                else
                {
                    throw new InvalidOperationException();
                }

                break;
            }

            case OperandType.InlineString:
            {
                if (!(operand is string str))
                {
                    str = operand.ToString() ?? string.Empty;
                }

                text.Append('"')
                .Append(str)
                .Append('"');
                break;
            }

            case OperandType.InlineField:
            {
                if (operand is FieldInfo field)
                {
                    text.AppendDump(field);
                }
                else
                {
                    throw new InvalidOperationException();
                }
                break;
            }

            case OperandType.InlineI:
            case OperandType.ShortInlineI:
            {
                if (operand is IntPtr intPtr)
                {
                    text.AppendFormat(intPtr, "X");
                }
                else if (operand is int integer)
                {
                    text.Append(integer);
                }
                else if (operand is sbyte signedByte)
                {
                    text.Append(signedByte);
                }
                else
                {
                    throw new InvalidOperationException();
                }

                break;
            }

            case OperandType.InlineI8:
            {
                if (operand is byte b)
                {
                    text.Append(b)
                    .Append('b');
                }
                else
                {
                    throw new InvalidOperationException();
                }
                break;
            }

            case OperandType.InlineMethod:
            {
                if (operand is MethodBase methodBase)
                {
                    text.AppendDump(methodBase);
                }
                else
                {
                    throw new InvalidOperationException();
                }
                break;
            }

            case OperandType.InlineR:
            case OperandType.ShortInlineR:
            {
                if (operand is float f)
                {
                    text.Append(f)
                    .Append('f');
                }
                else if (operand is double d)
                {
                    text.Append(d)
                    .Append('d');
                }
                else if (operand is decimal m)
                {
                    text.Append(m)
                    .Append('m');
                }
                else
                {
                    throw new InvalidOperationException();
                }
                break;
            }

            case OperandType.InlineType:
            {
                if (operand is Type type)
                {
                    text.AppendDump(type);
                }
                else
                {
                    throw new InvalidOperationException();
                }
                break;
            }

            case OperandType.InlineTok:
            {
                if (operand is MemberInfo member)
                {
                    text.AppendDump(member);
                }
                else
                {
                    throw new InvalidOperationException();
                }
                break;
            }

            case OperandType.InlineVar:
            case OperandType.ShortInlineVar:
            {
                // Variables?
                text.AppendDump(operand);
                break;
            }

            case OperandType.InlineNone:
            //case OperandType.InlinePhi:
            case OperandType.InlineSig:
            default:
            {
                Hold.Debug(operandType, operand);
                text.Append(operand);
                break;
            }
            }
        }