private object ReadOperand(CilOperandType operandType)
 {
     return(operandType switch
     {
         CilOperandType.InlineNone => (object)null,
         CilOperandType.ShortInlineI => Reader.ReadSByte(),
         CilOperandType.ShortInlineBrTarget => new CilOffsetLabel(Reader.ReadSByte() + (int)(Reader.Offset - Reader.StartOffset)),
         CilOperandType.ShortInlineVar => Reader.ReadByte(),
         CilOperandType.ShortInlineArgument => Reader.ReadByte(),
         CilOperandType.InlineVar => Reader.ReadUInt16(),
         CilOperandType.InlineArgument => Reader.ReadUInt16(),
         CilOperandType.InlineI => Reader.ReadInt32(),
         CilOperandType.InlineBrTarget => new CilOffsetLabel(Reader.ReadInt32() + (int)(Reader.Offset - Reader.StartOffset)),
         CilOperandType.ShortInlineR => Reader.ReadSingle(),
         CilOperandType.InlineI8 => Reader.ReadInt64(),
         CilOperandType.InlineR => Reader.ReadDouble(),
         CilOperandType.InlineField => new MetadataToken(Reader.ReadUInt32()),
         CilOperandType.InlineMethod => new MetadataToken(Reader.ReadUInt32()),
         CilOperandType.InlineSig => new MetadataToken(Reader.ReadUInt32()),
         CilOperandType.InlineString => new MetadataToken(Reader.ReadUInt32()),
         CilOperandType.InlineTok => new MetadataToken(Reader.ReadUInt32()),
         CilOperandType.InlineType => new MetadataToken(Reader.ReadUInt32()),
         CilOperandType.InlinePhi => throw new NotSupportedException(),
         CilOperandType.InlineSwitch => ReadSwitchTable(),
         _ => throw new ArgumentOutOfRangeException(nameof(operandType), operandType, null)
     });
Exemple #2
0
        private static object ReadRawOperand(IBinaryStreamReader reader, CilOperandType cilOperandType)
        {
            switch (cilOperandType)
            {
            case CilOperandType.InlineNone:
                return(null);

            case CilOperandType.InlineArgument:
            case CilOperandType.InlineVar:
                return(reader.ReadUInt16());

            case CilOperandType.InlineI:
            case CilOperandType.InlineBrTarget:
                return(reader.ReadInt32());

            case CilOperandType.ShortInlineArgument:
            case CilOperandType.ShortInlineVar:
                return(reader.ReadByte());

            case CilOperandType.ShortInlineBrTarget:
            case CilOperandType.ShortInlineI:
                return(reader.ReadSByte());

            case CilOperandType.ShortInlineR:
                return(reader.ReadSingle());

            case CilOperandType.InlineR:
                return(reader.ReadDouble());

            case CilOperandType.InlineI8:
                return(reader.ReadInt64());

            case CilOperandType.InlineField:
            case CilOperandType.InlineMethod:
            case CilOperandType.InlineSig:
            case CilOperandType.InlineTok:
            case CilOperandType.InlineType:
            case CilOperandType.InlineString:
                return(new MetadataToken(reader.ReadUInt32()));

            case CilOperandType.InlineSwitch:
                var offsets = new int[reader.ReadUInt32()];
                for (int i = 0; i < offsets.Length; i++)
                {
                    offsets[i] = reader.ReadInt32();
                }
                return(offsets);
            }
            throw new NotSupportedException();
        }
Exemple #3
0
        public virtual string FormatOperand(CilOperandType operandType, object operand)
        {
            switch (operandType)
            {
            case CilOperandType.InlineNone:
                return(string.Empty);

            case CilOperandType.ShortInlineBrTarget:
            case CilOperandType.InlineBrTarget:
                return(FormatBranchTarget(operand));

            case CilOperandType.InlineType:
            case CilOperandType.InlineField:
            case CilOperandType.InlineMethod:
            case CilOperandType.InlineTok:
                return(FormatMember(operand));

            case CilOperandType.InlineSig:
                return(FormatSignature(operand));

            case CilOperandType.ShortInlineI:
            case CilOperandType.InlineI:
            case CilOperandType.InlineI8:
                return(FormatInteger(operand));

            case CilOperandType.InlineR:
            case CilOperandType.ShortInlineR:
                return(FormatFloat(operand));

            case CilOperandType.InlineString:
                return(FormatString(operand));

            case CilOperandType.InlineSwitch:
                return(FormatSwitch(operand));

            case CilOperandType.InlineVar:
            case CilOperandType.ShortInlineVar:
                return(FormatVariable(operand));

            case CilOperandType.InlineArgument:
            case CilOperandType.ShortInlineArgument:
                return(FormatArgument(operand));

            default:
                throw new ArgumentOutOfRangeException(nameof(operandType), operandType, null);
            }
        }
 /// <summary>
 /// Formats an operand to a human readable string.
 /// </summary>
 /// <param name="operandType">The type of operand to format.</param>
 /// <param name="operand">The operand to format.</param>
 /// <returns>The formatted string.</returns>
 /// <exception cref="ArgumentOutOfRangeException">Occurs when the provided operand type is not valid.</exception>
 public virtual string FormatOperand(CilOperandType operandType, object operand) => operandType switch
 {
Exemple #5
0
        private object ReadOperand(CilOperandType operandType)
        {
            switch (operandType)
            {
            case CilOperandType.InlineNone:
                return(null);

            case CilOperandType.ShortInlineI:
                return(_reader.ReadSByte());

            case CilOperandType.ShortInlineBrTarget:
                return(new CilOffsetLabel(_reader.ReadSByte() + (int)(_reader.Offset - _reader.StartOffset)));

            case CilOperandType.ShortInlineVar:
                byte shortLocalIndex = _reader.ReadByte();
                return(_operandResolver?.ResolveLocalVariable(shortLocalIndex) ?? shortLocalIndex);

            case CilOperandType.ShortInlineArgument:
                byte shortArgIndex = _reader.ReadByte();
                return(_operandResolver?.ResolveParameter(shortArgIndex) ?? shortArgIndex);

            case CilOperandType.InlineVar:
                ushort longLocalIndex = _reader.ReadUInt16();
                return(_operandResolver?.ResolveLocalVariable(longLocalIndex) ?? longLocalIndex);

            case CilOperandType.InlineArgument:
                ushort longArgIndex = _reader.ReadUInt16();
                return(_operandResolver?.ResolveParameter(longArgIndex) ?? longArgIndex);

            case CilOperandType.InlineI:
                return(_reader.ReadInt32());

            case CilOperandType.InlineBrTarget:
                return(new CilOffsetLabel(_reader.ReadInt32() + (int)(_reader.Offset - _reader.StartOffset)));

            case CilOperandType.ShortInlineR:
                return(_reader.ReadSingle());

            case CilOperandType.InlineI8:
                return(_reader.ReadInt64());

            case CilOperandType.InlineR:
                return(_reader.ReadDouble());

            case CilOperandType.InlineString:
                var stringToken = new MetadataToken(_reader.ReadUInt32());
                return(_operandResolver?.ResolveString(stringToken) ?? stringToken);

            case CilOperandType.InlineField:
            case CilOperandType.InlineMethod:
            case CilOperandType.InlineSig:
            case CilOperandType.InlineTok:
            case CilOperandType.InlineType:
                var memberToken = new MetadataToken(_reader.ReadUInt32());
                return(_operandResolver?.ResolveMember(memberToken) ?? memberToken);

            case CilOperandType.InlinePhi:
                throw new NotSupportedException();

            case CilOperandType.InlineSwitch:
                return(ReadSwitchTable());

            default:
                throw new ArgumentOutOfRangeException(nameof(operandType), operandType, null);
            }
        }