Exemplo n.º 1
0
        internal void ToString(TextBuilder text)
        {
            if (this.ILGeneratorMethod != ILGeneratorMethod.None)
            {
                text.Append(this.ILGeneratorMethod)
                .Append('(');
                if (this.Argument is IEnumerable args)
                {
                    text.AppendDelimit(',', args.AsObjectEnumerable(), (tb, a) => tb.AppendDump(a));
                }
                else
                {
                    text.AppendDump(this.Argument);
                }

                text.Append(')');
            }
            else
            {
                var opCode = this.OpCode;
                AppendLabel(text, this);
                text.Append(": ")
                .Append(opCode.Name);
                var operand = this.Argument;
                if (operand is null)
                {
                    return;
                }
                AppendOperand(text, opCode.OperandType, operand);
            }
        }
Exemplo n.º 2
0
 public static TextBuilder AppendDump(this TextBuilder textBuilder, object?obj, DumpOptions options = default)
 {
     return(obj switch
     {
         // Big ol' switch statement
         null when options.Verbose => textBuilder.Append("null"),
         null => textBuilder,
         Type type => AppendDump(textBuilder, type, options),
         Array array => AppendDump(textBuilder, array, options),
         string str => textBuilder.Append(str),
         IEnumerable enumerable => AppendDump(textBuilder, enumerable, options),
         TimeSpan timeSpan => textBuilder.AppendDump(timeSpan, options),
         DateTime dateTime => textBuilder.AppendDump(dateTime, options),
         Guid guid => textBuilder.AppendDump(guid, options),
         _ => textBuilder.Append(obj)
     });
Exemplo n.º 3
0
        public static TextBuilder AppendDump(this TextBuilder textBuilder, FieldInfo?field, DumpOptions options = default)
        {
            if (field is null)
            {
                if (options.Verbose)
                {
                    return(textBuilder.Append("(FieldInfo)null"));
                }
                return(textBuilder);
            }

            if (options.Verbose)
            {
                var fieldAttributes = Attribute.GetCustomAttributes(field, true);
                if (fieldAttributes.Length > 0)
                {
                    textBuilder.AppendDelimit(Environment.NewLine,
                                              fieldAttributes,
                                              (tb, attr) => tb.Append('[').Append(attr).Append(']'))
                    .AppendLine();
                }

                var visibility = field.GetVisibility();
                if (visibility.HasFlag <Visibility>(Visibility.Private))
                {
                    textBuilder.Write("private ");
                }
                if (visibility.HasFlag <Visibility>(Visibility.Protected))
                {
                    textBuilder.Write("protected ");
                }
                if (visibility.HasFlag <Visibility>(Visibility.Internal))
                {
                    textBuilder.Write("internal ");
                }
                if (visibility.HasFlag <Visibility>(Visibility.Public))
                {
                    textBuilder.Write("public ");
                }
                if (field.IsStatic)
                {
                    textBuilder.Write("static ");
                }
            }

            return(textBuilder.AppendDump(field.FieldType, options)
                   .Append(' ')
                   .Append(field.Name));
        }
Exemplo n.º 4
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;
            }
            }
        }
Exemplo n.º 5
0
        public static TextBuilder AppendDump(this TextBuilder textBuilder, PropertyInfo?property, DumpOptions options = default)
        {
            if (property is null)
            {
                if (options.Verbose)
                {
                    return(textBuilder.Append("(PropertyInfo)null"));
                }
                return(textBuilder);
            }

            if (options.Verbose)
            {
                var fieldAttributes = Attribute.GetCustomAttributes(property, true);
                if (fieldAttributes.Length > 0)
                {
                    textBuilder.AppendDelimit(Environment.NewLine,
                                              fieldAttributes,
                                              (tb, attr) => tb.Append('[').Append(attr).Append(']'))
                    .AppendLine();
                }

                var visibility = property.GetVisibility();
                if (visibility.HasFlag <Visibility>(Visibility.Private))
                {
                    textBuilder.Write("private ");
                }
                if (visibility.HasFlag <Visibility>(Visibility.Protected))
                {
                    textBuilder.Write("protected ");
                }
                if (visibility.HasFlag <Visibility>(Visibility.Internal))
                {
                    textBuilder.Write("internal ");
                }
                if (visibility.HasFlag <Visibility>(Visibility.Public))
                {
                    textBuilder.Write("public ");
                }
                if (property.IsStatic())
                {
                    textBuilder.Write("static ");
                }
            }

            textBuilder.AppendDump(property.PropertyType, options)
            .Append(' ')
            .Append(property.Name);

            var indexParams = property.GetIndexParameters();

            if (indexParams.Length > 0)
            {
                textBuilder.Append('[')
                .AppendDelimit(", ", indexParams, (tb, pi) => tb.AppendDump(pi, options))
                .Append(']');
            }

            // Getter + Setter
            textBuilder.Append(" { ");
            var getter = property.GetGetter();

            if (getter != null)
            {
                if (options.Verbose)
                {
                }
                else
                {
                    textBuilder.Append("get; ");
                }
            }
            var setter = property.GetSetter();

            if (setter != null)
            {
                if (options.Verbose)
                {
                }
                else
                {
                    textBuilder.Append("set; ");
                }
            }
            return(textBuilder.Append(" }"));
        }