Пример #1
0
        private void WriteNumber(object value, X86OperandSize size)
        {
            switch (size)
            {
            case X86OperandSize.Byte:
                if (value is sbyte || value is short || value is int || value is long)
                {
                    _writer.WriteSByte(Convert.ToSByte(value));
                }
                else
                {
                    _writer.WriteByte(Convert.ToByte(value));
                }
                break;

            case X86OperandSize.Word:
                if (value is sbyte || value is short || value is int || value is long)
                {
                    _writer.WriteInt16(Convert.ToInt16(value));
                }
                else
                {
                    _writer.WriteUInt16(Convert.ToUInt16(value));
                }
                break;

            case X86OperandSize.WordOrDword:
            case X86OperandSize.Dword:
                if (value is sbyte || value is short || value is int || value is long)
                {
                    _writer.WriteInt32(Convert.ToInt32(value));
                }
                else
                {
                    _writer.WriteUInt32(Convert.ToUInt32(value));
                }
                break;

            default:
                throw new NotSupportedException();
            }
        }
Пример #2
0
        private void WriteValue(IBinaryStreamWriter writer, TypeSignature argumentType, ITypeCodedIndexProvider provider, object value)
        {
            if (argumentType.IsTypeOf("System", "Type"))
            {
                writer.WriteSerString(TypeNameBuilder.GetAssemblyQualifiedName((TypeSignature)value));
                return;
            }

            switch (argumentType.ElementType)
            {
            case ElementType.Boolean:
                writer.WriteByte((byte)((bool)value ? 1 : 0));
                break;

            case ElementType.Char:
                writer.WriteUInt16((char)value);
                break;

            case ElementType.I1:
                writer.WriteSByte((sbyte)value);
                break;

            case ElementType.U1:
                writer.WriteByte((byte)value);
                break;

            case ElementType.I2:
                writer.WriteInt16((short)value);
                break;

            case ElementType.U2:
                writer.WriteUInt16((ushort)value);
                break;

            case ElementType.I4:
                writer.WriteInt32((int)value);
                break;

            case ElementType.U4:
                writer.WriteUInt32((uint)value);
                break;

            case ElementType.I8:
                writer.WriteInt64((long)value);
                break;

            case ElementType.U8:
                writer.WriteUInt64((ulong)value);
                break;

            case ElementType.R4:
                writer.WriteSingle((float)value);
                break;

            case ElementType.R8:
                writer.WriteDouble((double)value);
                break;

            case ElementType.String:
                writer.WriteSerString(value as string);
                break;

            case ElementType.Object:
                var valueType    = value.GetType();
                var innerTypeSig = argumentType.Module.CorLibTypeFactory.FromName(valueType.Namespace, valueType.Name);
                TypeSignature.WriteFieldOrPropType(writer, innerTypeSig);
                WriteValue(writer, innerTypeSig, provider, value);
                break;

            case ElementType.Class:
            case ElementType.Enum:
            case ElementType.ValueType:
                var enumTypeDef = argumentType.Resolve();
                if (enumTypeDef != null && enumTypeDef.IsEnum)
                {
                    WriteValue(writer, enumTypeDef.GetEnumUnderlyingType(), provider, Value);
                }
                else
                {
                    throw new NotImplementedException();
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Пример #3
0
        /// <inheritdoc />
        public override void Write(MetadataBuffer buffer, IBinaryStreamWriter writer)
        {
            if (Value == null)
            {
                writer.WriteSerString(null);
                return;
            }

            switch (Type.GetTypeCode(Value.GetType()))
            {
            case TypeCode.Boolean:
                writer.WriteByte((byte)((bool)Value ? 1 : 0));
                break;

            case TypeCode.Byte:
                writer.WriteByte((byte)Value);
                break;

            case TypeCode.Char:
                writer.WriteUInt16((char)Value);
                break;

            case TypeCode.Double:
                writer.WriteDouble((double)Value);
                break;

            case TypeCode.Int16:
                writer.WriteInt16((short)Value);
                break;

            case TypeCode.Int32:
                writer.WriteInt32((int)Value);
                break;

            case TypeCode.Int64:
                writer.WriteInt64((long)Value);
                break;

            case TypeCode.SByte:
                writer.WriteSByte((sbyte)Value);
                break;

            case TypeCode.Single:
                writer.WriteSingle((float)Value);
                break;

            case TypeCode.String:
                writer.WriteSerString((string)Value);
                break;

            case TypeCode.UInt16:
                writer.WriteUInt16((ushort)Value);
                break;

            case TypeCode.UInt32:
                writer.WriteUInt32((uint)Value);
                break;

            case TypeCode.UInt64:
                writer.WriteUInt64((ulong)Value);
                break;

            default:
                if (Value is TypeSignature typeSignature)
                {
                    writer.WriteSerString(TypeNameBuilder.GetAssemblyQualifiedName(typeSignature));
                }
                else
                {
                    throw new NotSupportedException();
                }
                break;
            }
        }