Ejemplo n.º 1
0
        /// <summary>
        /// Converts a ConstantTypeCode to a PrimitiveTypeCode
        /// </summary>
        internal static PrimitiveTypeCode ToPrimitiveTypeCode(this ConstantTypeCode constantTypeCode)
        {
            switch (constantTypeCode)
            {
            case ConstantTypeCode.Boolean:
                return(PrimitiveTypeCode.Boolean);

            case ConstantTypeCode.Char:
                return(PrimitiveTypeCode.Char);

            case ConstantTypeCode.SByte:
                return(PrimitiveTypeCode.SByte);

            case ConstantTypeCode.Byte:
                return(PrimitiveTypeCode.Byte);

            case ConstantTypeCode.Int16:
                return(PrimitiveTypeCode.Int16);

            case ConstantTypeCode.UInt16:
                return(PrimitiveTypeCode.UInt16);

            case ConstantTypeCode.Int32:
                return(PrimitiveTypeCode.Int32);

            case ConstantTypeCode.UInt32:
                return(PrimitiveTypeCode.UInt32);

            case ConstantTypeCode.Int64:
                return(PrimitiveTypeCode.Int64);

            case ConstantTypeCode.UInt64:
                return(PrimitiveTypeCode.UInt64);

            case ConstantTypeCode.Single:
                return(PrimitiveTypeCode.Single);

            case ConstantTypeCode.Double:
                return(PrimitiveTypeCode.Double);

            case ConstantTypeCode.String:
                return(PrimitiveTypeCode.String);

            default:
                throw new MrException("Invalid constant type code");
            }
        }
        public object GetConstantValue(out ConstantTypeCode constantTypeCode)
        {
            var defaultValueConstantHandle = this.Definition.GetDefaultValue();

            if (defaultValueConstantHandle.IsNil)
            {
                // bugbug: happens with winRT projection turned on
                constantTypeCode = ConstantTypeCode.Invalid;
                return(null);
            }

            // bugbug: does this work for anything other than enum?

            var defaultValueConstant = this.DeclaringType.Assembly.Reader.GetConstant(defaultValueConstantHandle);
            var valueBlob            = defaultValueConstant.Value;
            var blobReader           = this.DeclaringType.Assembly.Reader.GetBlobReader(valueBlob);

            constantTypeCode = defaultValueConstant.TypeCode;
            switch (constantTypeCode)
            {
            case ConstantTypeCode.Int32:
                return(blobReader.ReadInt32());

            case ConstantTypeCode.Int16:
                return(blobReader.ReadInt16());

            case ConstantTypeCode.UInt32:
                return((int)blobReader.ReadUInt32());

            case ConstantTypeCode.UInt64:
                return((int)blobReader.ReadUInt64());

            case ConstantTypeCode.Int64:
                return((int)blobReader.ReadInt64());

            case ConstantTypeCode.UInt16:
                return((int)blobReader.ReadUInt16());

            case ConstantTypeCode.Byte:
                return((int)blobReader.ReadByte());

            default:
                throw new NotSupportedException("Unsupported ContentTypeCode");
            }
        }
        public bool TryGetDefaultValue(out object value, out ConstantTypeCode typeCode)
        {
            typeCode = ConstantTypeCode.Invalid;
            value    = null;

            var defaultValueConstantHandle = this.Definition.GetDefaultValue();

            if (defaultValueConstantHandle.IsNil)
            {
                // bugbug: happens with winRT projection turned on
                return(false);
            }

            var constant = this.DeclaringType.Assembly.Reader.GetConstant(defaultValueConstantHandle);

            if (constant.TypeCode != ConstantTypeCode.Invalid)
            {
                value    = constant.Value;
                typeCode = constant.TypeCode;
                return(true);
            }

            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Reads a constant value (see ECMA-335 Partition II section 22.9) from the current position.
        /// </summary>
        /// <exception cref="BadImageFormatException">Error while reading from the blob.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="typeCode"/> is not a valid <see cref="ConstantTypeCode"/>.</exception>
        /// <returns>
        /// Boxed constant value. To avoid allocating the object use Read* methods directly.
        /// Constants of type <see cref="ConstantTypeCode.String"/> are encoded as UTF16 strings, use <see cref="ReadUTF16(int)"/> to read them.
        /// </returns>
        public object ReadConstant(ConstantTypeCode typeCode)
        {
            // Partition II section 22.9:
            //
            // Type shall be exactly one of: ELEMENT_TYPE_BOOLEAN, ELEMENT_TYPE_CHAR, ELEMENT_TYPE_I1,
            // ELEMENT_TYPE_U1, ELEMENT_TYPE_I2, ELEMENT_TYPE_U2, ELEMENT_TYPE_I4, ELEMENT_TYPE_U4,
            // ELEMENT_TYPE_I8, ELEMENT_TYPE_U8, ELEMENT_TYPE_R4, ELEMENT_TYPE_R8, or ELEMENT_TYPE_STRING;
            // or ELEMENT_TYPE_CLASS with a Value of zero  (23.1.16)

            switch (typeCode)
            {
            case ConstantTypeCode.Boolean:
                return(ReadBoolean());

            case ConstantTypeCode.Char:
                return(ReadChar());

            case ConstantTypeCode.SByte:
                return(ReadSByte());

            case ConstantTypeCode.Int16:
                return(ReadInt16());

            case ConstantTypeCode.Int32:
                return(ReadInt32());

            case ConstantTypeCode.Int64:
                return(ReadInt64());

            case ConstantTypeCode.Byte:
                return(ReadByte());

            case ConstantTypeCode.UInt16:
                return(ReadUInt16());

            case ConstantTypeCode.UInt32:
                return(ReadUInt32());

            case ConstantTypeCode.UInt64:
                return(ReadUInt64());

            case ConstantTypeCode.Single:
                return(ReadSingle());

            case ConstantTypeCode.Double:
                return(ReadDouble());

            case ConstantTypeCode.String:
                return(ReadUTF16(RemainingBytes));

            case ConstantTypeCode.NullReference:
                // Partition II section 22.9:
                // The encoding of Type for the nullref value is ELEMENT_TYPE_CLASS with a Value of a 4-byte zero.
                // Unlike uses of ELEMENT_TYPE_CLASS in signatures, this one is not followed by a type token.
                if (ReadUInt32() != 0)
                {
                    throw new BadImageFormatException(SR.InvalidConstantValue);
                }

                return(null);

            default:
                throw new ArgumentOutOfRangeException(nameof(typeCode));
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Reads a constant value (see ECMA-335 Partition II section 22.9) from the current position.
        /// </summary>
        /// <exception cref="BadImageFormatException">Error while reading from the blob.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="typeCode"/> is not a valid <see cref="ConstantTypeCode"/>.</exception>
        /// <returns>
        /// Boxed constant value. To avoid allocating the object use Read* methods directly.
        /// Constants of type <see cref="ConstantTypeCode.String"/> are encoded as UTF16 strings, use <see cref="ReadUTF16(int)"/> to read them.
        /// </returns>
        public object ReadConstant(ConstantTypeCode typeCode)
        {
            // Partition II section 22.9:
            //
            // Type shall be exactly one of: ELEMENT_TYPE_BOOLEAN, ELEMENT_TYPE_CHAR, ELEMENT_TYPE_I1, 
            // ELEMENT_TYPE_U1, ELEMENT_TYPE_I2, ELEMENT_TYPE_U2, ELEMENT_TYPE_I4, ELEMENT_TYPE_U4, 
            // ELEMENT_TYPE_I8, ELEMENT_TYPE_U8, ELEMENT_TYPE_R4, ELEMENT_TYPE_R8, or ELEMENT_TYPE_STRING; 
            // or ELEMENT_TYPE_CLASS with a Value of zero  (23.1.16)

            switch (typeCode)
            {
                case ConstantTypeCode.Boolean:
                    return ReadBoolean();

                case ConstantTypeCode.Char:
                    return ReadChar();

                case ConstantTypeCode.SByte:
                    return ReadSByte();

                case ConstantTypeCode.Int16:
                    return ReadInt16();

                case ConstantTypeCode.Int32:
                    return ReadInt32();

                case ConstantTypeCode.Int64:
                    return ReadInt64();

                case ConstantTypeCode.Byte:
                    return ReadByte();

                case ConstantTypeCode.UInt16:
                    return ReadUInt16();

                case ConstantTypeCode.UInt32:
                    return ReadUInt32();

                case ConstantTypeCode.UInt64:
                    return ReadUInt64();

                case ConstantTypeCode.Single:
                    return ReadSingle();

                case ConstantTypeCode.Double:
                    return ReadDouble();

                case ConstantTypeCode.String:
                    return ReadUTF16(RemainingBytes);

                case ConstantTypeCode.NullReference:
                    // Partition II section 22.9:
                    // The encoding of Type for the nullref value is ELEMENT_TYPE_CLASS with a Value of a 4-byte zero.
                    // Unlike uses of ELEMENT_TYPE_CLASS in signatures, this one is not followed by a type token.
                    if (ReadUInt32() != 0)
                    {
                        throw new BadImageFormatException(SR.InvalidConstantValue);
                    }

                    return null;

                default:
                    throw new ArgumentOutOfRangeException(nameof(typeCode));
            }
        }
Ejemplo n.º 6
0
 public ConstInfo(string @namespace, string name, ConstantTypeCode typeCode)
 {
     this.Namespace        = @namespace;
     this.Name             = name;
     this.ConstantTypeCode = typeCode;
 }
 public ObjectAsConstantValue(object value)
 {
     TypeCode   = GetTypeCode(value);
     this.value = value;
 }