Пример #1
0
 internal static bool TryGetPrimitiveTypeCode(
     MetadataType type,
     out PrimitiveTypeCode typeCode)
 {
     MaybeLoadPrimitiveTables();
     return(s_typeToPrimitive.TryGetValue(type, out typeCode));
 }
Пример #2
0
 internal static bool TryGetMetadataType(
     PrimitiveTypeCode typeCode,
     out MetadataType type)
 {
     MaybeLoadPrimitiveTables();
     return(s_primitiveToType.TryGetValue(typeCode, out type));
 }
        public string GetPrimitiveType(PrimitiveTypeCode typeCode)
        {
            switch (typeCode)
            {
            case PrimitiveTypeCode.Boolean: return(BooleanName);

            case PrimitiveTypeCode.String: return(StringName);

            case PrimitiveTypeCode.Void: return(VoidName);

            case PrimitiveTypeCode.Int32: return(Int32Name);

            case PrimitiveTypeCode.Byte: return(ByteName);

            case PrimitiveTypeCode.Object: return(ObjectName);

            case PrimitiveTypeCode.Int64: return(Int64Name);

            case PrimitiveTypeCode.IntPtr: return(IntPtrName);

            case PrimitiveTypeCode.Double: return(DoubleName);

            case PrimitiveTypeCode.Char: return(CharName);

            default: throw new System.NotImplementedException(typeCode.ToString());
            }
        }
Пример #4
0
        /// <summary>
        /// Writes primitive type code.
        /// </summary>
        /// <param name="type">Any primitive type code except for <see cref="PrimitiveTypeCode.TypedReference"/> and <see cref="PrimitiveTypeCode.Void"/>.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="type"/> is not valid in this context.</exception>
        public void PrimitiveType(PrimitiveTypeCode type)
        {
            switch (type)
            {
            case PrimitiveTypeCode.Boolean:
            case PrimitiveTypeCode.Byte:
            case PrimitiveTypeCode.SByte:
            case PrimitiveTypeCode.Char:
            case PrimitiveTypeCode.Int16:
            case PrimitiveTypeCode.UInt16:
            case PrimitiveTypeCode.Int32:
            case PrimitiveTypeCode.UInt32:
            case PrimitiveTypeCode.Int64:
            case PrimitiveTypeCode.UInt64:
            case PrimitiveTypeCode.Single:
            case PrimitiveTypeCode.Double:
            case PrimitiveTypeCode.IntPtr:
            case PrimitiveTypeCode.UIntPtr:
            case PrimitiveTypeCode.String:
            case PrimitiveTypeCode.Object:
                Builder.WriteByte((byte)type);
                return;

            case PrimitiveTypeCode.TypedReference:
            case PrimitiveTypeCode.Void:
            default:
                Throw.ArgumentOutOfRange(nameof(type));
                return;
            }
        }
Пример #5
0
 public FindTypeInAttributeDecoder(MetadataModule currentModule, ITypeDefinition type)
 {
     this.currentModule   = currentModule;
     this.declaringModule = type.ParentModule?.PEFile ?? throw new InvalidOperationException("Cannot use MetadataModule without PEFile as context.");
     this.handle          = (TypeDefinitionHandle)type.MetadataToken;
     this.primitiveType   = type.KnownTypeCode == KnownTypeCode.None ? 0 : type.KnownTypeCode.ToPrimitiveTypeCode();
 }
        public ArrayType[] RemoveEmptyEntries(ArrayType[] arr)
        {
            List <ArrayType> temp = new List <ArrayType>();

            foreach (ArrayType item in arr)
            {
                PrimitiveTypeCode type = ConvertUtils.GetTypeCode(GetType());
                switch (type)
                {
                case PrimitiveTypeCode.Object:
                    if (item != null)
                    {
                        temp.Add(item);
                    }
                    break;

                case PrimitiveTypeCode.String:
                    if (!String.IsNullOrEmpty((string)(object)item))
                    {
                        temp.Add(item);
                    }
                    break;

                default:
                    if (item != null)
                    {
                        temp.Add(item);
                    }
                    break;
                }
            }
            return(temp.ToArray());
        }
        public void BoolToIntSetForAllIntegerTypes(PrimitiveTypeCode integerType)
        {
            var cppMarshallable = new CppParameter("param")
            {
                TypeName       = "Integer",
                ArrayDimension = "0",
                IsArray        = true,
                Rule           =
                {
                    MappingType = "bool"
                }
            };

            var typeRegistry = new TypeRegistry(Logger, A.Fake <IDocumentationLinker>());

            typeRegistry.BindType("bool", TypeRegistry.Boolean);
            typeRegistry.BindType("Integer", TypeRegistry.ImportPrimitiveType(integerType));

            var marshalledElementFactory = new MarshalledElementFactory(Logger, new GlobalNamespaceProvider(), typeRegistry);

            var csMarshallable = marshalledElementFactory.Create(cppMarshallable, cppMarshallable.Name);

            Assert.Equal(TypeRegistry.Boolean, csMarshallable.PublicType);
            Assert.Equal(TypeRegistry.ImportPrimitiveType(integerType), csMarshallable.MarshalType);
            Assert.True(csMarshallable.IsBoolToInt);
            Assert.False(csMarshallable.HasPointer);
            Assert.False(csMarshallable.IsArray);
            Assert.Equal(0, csMarshallable.ArrayDimensionValue);
        }
Пример #8
0
 internal FindTypeDecoder(TypeDefinitionHandle handle, PEFile declaringModule)
 {
     this.handle          = handle;
     this.declaringModule = declaringModule;
     this.primitiveType   = 0;
     this.currentModule   = null;
 }
Пример #9
0
        public void DefineLocalConstant(string name, object value, PrimitiveTypeCode typeCode, uint constantSignatureToken)
        {
            if (value == null)
            {
                // ISymUnmanagedWriter2.DefineConstant2 throws an ArgumentException
                // if you pass in null - Dev10 appears to use 0 instead.
                // (See EMITTER::VariantFromConstVal)
                value    = 0;
                typeCode = PrimitiveTypeCode.Int32;
            }

            if (typeCode == PrimitiveTypeCode.String)
            {
                DefineLocalStringConstant(name, (string)value, constantSignatureToken);
            }
            else
            {
                try
                {
                    this.symWriter.DefineConstant2(name, value, constantSignatureToken);
                }
                catch (Exception ex)
                {
                    throw new PdbWritingException(ex);
                }
            }
        }
Пример #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonPrimitiveContract"/> class.
        /// </summary>
        /// <param name="underlyingType">The underlying type for the contract.</param>
        public JsonPrimitiveContract(Type underlyingType) : base(underlyingType)
        {
            ContractType = JsonContractType.Primitive;

            TypeCode = ConvertUtils.GetTypeCode(underlyingType);
            IsReadOnlyOrFixedSize = true;
        }
Пример #11
0
            public Name GetPrimitiveType(PrimitiveTypeCode typeCode)
            {
                var pooled = PooledStringBuilder.GetInstance();

                pooled.Builder.Append(GetPrimitiveTypeQualifiedName(typeCode));
                return(new Name(pooled, _useAssemblyQualification ? _model._lazyCorlibAssemblyRef.Value : default));
            }
    public void BoolToIntSetForAllIntegerTypes(PrimitiveTypeCode integerType)
    {
        var cppMarshallable = new CppParameter("param")
        {
            TypeName       = "Integer",
            ArrayDimension = "0",
            Rule           =
            {
                MappingType = "bool"
            }
        };

        var integerPrimitiveType = TypeRegistry.ImportPrimitiveType(integerType);

        TypeRegistry.BindType("bool", TypeRegistry.Boolean);
        TypeRegistry.BindType("Integer", integerPrimitiveType);


        var csMarshallable = marshalledElementFactory.Create(cppMarshallable, cppMarshallable.Name);

        Assert.Equal(TypeRegistry.Boolean, csMarshallable.PublicType);
        Assert.Equal(integerPrimitiveType, csMarshallable.MarshalType);
        Assert.True(csMarshallable.IsBoolToInt);
        Assert.False(csMarshallable.HasPointer);
        Assert.False(csMarshallable.IsArray);
        Assert.Equal(0, csMarshallable.ArrayDimensionValue);
    }
Пример #13
0
        private static object ReadValue(PrimitiveTypeCode primitiveTypeCode, BinaryReader reader)
        {
            switch (primitiveTypeCode)
            {
            case PrimitiveTypeCode.Boolean: return(reader.ReadBoolean());

            case PrimitiveTypeCode.Char: return(reader.ReadChar());

            case PrimitiveTypeCode.Float32: return(reader.ReadSingle());

            case PrimitiveTypeCode.Float64: return(reader.ReadDouble());

            case PrimitiveTypeCode.Int16: return(reader.ReadInt16());

            case PrimitiveTypeCode.Int32: return(reader.ReadInt32());

            case PrimitiveTypeCode.Int64: return(reader.ReadInt64());

            case PrimitiveTypeCode.Int8: return(reader.ReadSByte());

            case PrimitiveTypeCode.UInt16: return(reader.ReadUInt16());

            case PrimitiveTypeCode.UInt32: return(reader.ReadUInt32());

            case PrimitiveTypeCode.UInt64: return(reader.ReadUInt64());

            case PrimitiveTypeCode.UInt8: return(reader.ReadByte());

            default:
                Debug.Assert(false);
                break;
            }
            return(null);
        }
Пример #14
0
        public static TypeReference GetPrimitiveType(PrimitiveTypeCode typeCode, Assembly assembly)
        {
            var typeRef = assembly.PrimitiveTypeSignatures[(int)typeCode];

            if (typeRef != null)
            {
                return(typeRef);
            }

            typeRef = new TypeReference();

            var primitiveTypeInfo = CodeModelUtils.PrimitiveTypes[(int)typeCode];

            typeRef._name        = primitiveTypeInfo.Name;
            typeRef._namespace   = "System";
            typeRef._isValueType = primitiveTypeInfo.IsValueType;

            if (assembly.Name != "mscorlib")
            {
                typeRef._owner = AssemblyReference.GetMscorlib(assembly);
            }

            assembly.Module.AddSignature(ref typeRef);
            assembly.PrimitiveTypeSignatures[(int)typeCode] = typeRef;

            return(typeRef);
        }
Пример #15
0
        public static bool IsEnum(this TypeDefinition typeDefinition, MetadataReader reader,
                                  out PrimitiveTypeCode underlyingType)
        {
            underlyingType = 0;
            EntityHandle baseType = typeDefinition.GetBaseTypeOrNil();

            if (baseType.IsNil)
            {
                return(false);
            }
            if (!baseType.IsKnownType(reader, KnownTypeCode.Enum))
            {
                return(false);
            }
            foreach (var handle in typeDefinition.GetFields())
            {
                var field = reader.GetFieldDefinition(handle);
                if ((field.Attributes & FieldAttributes.Static) != 0)
                {
                    continue;
                }
                var blob = reader.GetBlobReader(field.Signature);
                if (blob.ReadSignatureHeader().Kind != SignatureKind.Field)
                {
                    return(false);
                }
                underlyingType = (PrimitiveTypeCode)blob.ReadByte();
                return(true);
            }
            return(false);
        }
Пример #16
0
        XSharpType ISimpleTypeProvider <XSharpType> .GetPrimitiveType(PrimitiveTypeCode typeCode)
        {
            switch (typeCode)
            {
            case PrimitiveTypeCode.Boolean:
                return(XSharpType.Boolean);

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

            case PrimitiveTypeCode.Int16:
                return(XSharpType.Short);

            case PrimitiveTypeCode.Int32:
                return(XSharpType.Integer);

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

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

            case PrimitiveTypeCode.UInt16:
                return(XSharpType.Word);

            case PrimitiveTypeCode.UInt32:
                return(XSharpType.DWord);

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

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

            case PrimitiveTypeCode.Void:
                return(XSharpType.Void);

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

            case PrimitiveTypeCode.Single:
                return(XSharpType.Real4);

            case PrimitiveTypeCode.Double:
                return(XSharpType.Real8);

            case PrimitiveTypeCode.IntPtr:
                return(XSharpType.Ptr);

            case PrimitiveTypeCode.UIntPtr:
                return(XSharpType.Create("UINTPTR"));

            case PrimitiveTypeCode.Object:
                return(XSharpType.Object);

            case PrimitiveTypeCode.TypedReference:
                break;
            }
            return(XSharpType.Create("UNKNOWN"));
        }
		/// <summary>
		/// Initializes a new instance of the <see cref="JsonPrimitiveContract"/> class.
		/// </summary>
		/// <param name="underlyingType">The underlying type for the contract.</param>
		public JsonPrimitiveContract(Type underlyingType) : base(underlyingType)
		{
			ContractType = JsonContractType.Primitive;

			TypeCode = ConvertUtils.GetTypeCode(underlyingType);
			IsReadOnlyOrFixedSize = true;
		}
Пример #18
0
        internal static void WritePrimitiveType(BlobBuilder builder, PrimitiveTypeCode type)
        {
            switch (type)
            {
            case PrimitiveTypeCode.Boolean:
            case PrimitiveTypeCode.Byte:
            case PrimitiveTypeCode.SByte:
            case PrimitiveTypeCode.Char:
            case PrimitiveTypeCode.Int16:
            case PrimitiveTypeCode.UInt16:
            case PrimitiveTypeCode.Int32:
            case PrimitiveTypeCode.UInt32:
            case PrimitiveTypeCode.Int64:
            case PrimitiveTypeCode.UInt64:
            case PrimitiveTypeCode.Single:
            case PrimitiveTypeCode.Double:
            case PrimitiveTypeCode.IntPtr:
            case PrimitiveTypeCode.UIntPtr:
            case PrimitiveTypeCode.String:
            case PrimitiveTypeCode.Object:
                builder.WriteByte((byte)type);
                return;

            // TODO: should we allow these?
            case PrimitiveTypeCode.TypedReference:
            case PrimitiveTypeCode.Void:
            default:
                Throw.ArgumentOutOfRange(nameof(type));
                return;
            }
        }
Пример #19
0
        private void DefineLocalConstant(string name, object value, PrimitiveTypeCode typeCode, uint constantSignatureToken)
        {
            if (value == null)
            {
                // ISymUnmanagedWriter2.DefineConstant2 throws an ArgumentException
                // if you pass in null - Dev10 appears to use 0 instead.
                // (See EMITTER::VariantFromConstVal)
                value    = 0;
                typeCode = PrimitiveTypeCode.Int32;
            }

            if (typeCode == PrimitiveTypeCode.String)
            {
                DefineLocalStringConstant(name, (string)value, constantSignatureToken);
            }
            else if (value is DateTime)
            {
                // Marshal.GetNativeVariantForObject would create a variant with type VT_DATE and value equal to the
                // number of days since 1899/12/30.  However, ConstantValue::VariantFromConstant in the native VB
                // compiler actually created a variant with type VT_DATE and value equal to the tick count.
                // http://blogs.msdn.com/b/ericlippert/archive/2003/09/16/eric-s-complete-guide-to-vt-date.aspx
                _symWriter.DefineConstant2(name, new VariantStructure((DateTime)value), constantSignatureToken);
            }
            else
            {
                try
                {
                    _symWriter.DefineConstant2(name, value, constantSignatureToken);
                }
                catch (Exception ex)
                {
                    throw new PdbWritingException(ex);
                }
            }
        }
Пример #20
0
        /// <inheritdoc />
        public TypeDescriptor GetPrimitiveType(PrimitiveTypeCode typeCode)
        {
            switch (typeCode)
            {
            case PrimitiveTypeCode.Boolean:
                return(new TypeDescriptor("bool"));

            case PrimitiveTypeCode.Byte:
                return(new TypeDescriptor("byte"));

            case PrimitiveTypeCode.Char:
                return(new TypeDescriptor("char"));

            case PrimitiveTypeCode.Double:
                return(new TypeDescriptor("double"));

            case PrimitiveTypeCode.Int16:
                return(new TypeDescriptor("short"));

            case PrimitiveTypeCode.Int32:
                return(new TypeDescriptor("int"));

            case PrimitiveTypeCode.IntPtr:
                return(new TypeDescriptor("System", "IntPtr"));

            case PrimitiveTypeCode.Int64:
                return(new TypeDescriptor("long"));

            case PrimitiveTypeCode.Object:
                return(new TypeDescriptor("object"));

            case PrimitiveTypeCode.SByte:
                return(new TypeDescriptor("sbyte"));

            case PrimitiveTypeCode.Single:
                return(new TypeDescriptor("float"));

            case PrimitiveTypeCode.String:
                return(new TypeDescriptor("string"));

            case PrimitiveTypeCode.UInt16:
                return(new TypeDescriptor("ushort"));

            case PrimitiveTypeCode.UInt32:
                return(new TypeDescriptor("uint"));

            case PrimitiveTypeCode.UInt64:
                return(new TypeDescriptor("ulong"));

            case PrimitiveTypeCode.UIntPtr:
                return(new TypeDescriptor("System", "UIntPtr"));

            case PrimitiveTypeCode.Void:
                return(new TypeDescriptor("void"));

            default:
                throw new System.NotImplementedException();
            }
        }
Пример #21
0
        private JsonSchemaType GetJsonSchemaType(Type type, Required valueRequired)
        {
            JsonSchemaType schemaType = JsonSchemaType.None;

            if (valueRequired != Required.Always && ReflectionUtils.IsNullable(type))
            {
                schemaType = JsonSchemaType.Null;
                if (ReflectionUtils.IsNullableType(type))
                {
                    type = Nullable.GetUnderlyingType(type);
                }
            }

            PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(type);

            switch (typeCode)
            {
            case PrimitiveTypeCode.Empty:
            case PrimitiveTypeCode.Object:
                return(schemaType | JsonSchemaType.String);

            case PrimitiveTypeCode.Boolean:
                return(schemaType | JsonSchemaType.Boolean);

            case PrimitiveTypeCode.Char:
                return(schemaType | JsonSchemaType.String);

            case PrimitiveTypeCode.SByte:
            case PrimitiveTypeCode.Byte:
            case PrimitiveTypeCode.Int16:
            case PrimitiveTypeCode.UInt16:
            case PrimitiveTypeCode.Int32:
            case PrimitiveTypeCode.UInt32:
            case PrimitiveTypeCode.Int64:
            case PrimitiveTypeCode.UInt64:
            case PrimitiveTypeCode.BigInteger:
                return(schemaType | JsonSchemaType.Integer);

            case PrimitiveTypeCode.Single:
            case PrimitiveTypeCode.Double:
            case PrimitiveTypeCode.Decimal:
                return(schemaType | JsonSchemaType.Float);

            // convert to string?
            case PrimitiveTypeCode.DateTime:
            case PrimitiveTypeCode.DateTimeOffset:
                return(schemaType | JsonSchemaType.String);

            case PrimitiveTypeCode.String:
            case PrimitiveTypeCode.Uri:
            case PrimitiveTypeCode.Guid:
            case PrimitiveTypeCode.TimeSpan:
            case PrimitiveTypeCode.Bytes:
                return(schemaType | JsonSchemaType.String);

            default:
                throw new JsonException("Unexpected type code '{0}' for type '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeCode, type));
            }
        }
Пример #22
0
 public string GetPrimitiveType(PrimitiveTypeCode typeCode)
 {
     if (typeCode == PrimitiveTypeCode.String)
     {
         return("string");
     }
     throw new Exception();
 }
Пример #23
0
 public KnownType GetPrimitiveType(PrimitiveTypeCode typeCode)
 {
     return(typeCode switch
     {
         PrimitiveTypeCode.Int32 => KnownType.I4,
         PrimitiveTypeCode.String => KnownType.String,
         _ => KnownType.Unknown
     });
Пример #24
0
 public string GetPrimitiveType(PrimitiveTypeCode typeCode)
 {
     if (typeCode == PrimitiveTypeCode.String)
     {
         return(valueReader.ReadSerializedString());
     }
     return("");
 }
Пример #25
0
        public TypeDefinitionHandleWrapper(MetadataReader reader, PrimitiveTypeCode typeCode)
        {
            Debug.Assert(reader != null);

            Reader               = reader;
            TypeCode             = typeCode;
            IsTypeCode           = true;
            TypeDefinitionHandle = new TypeDefinitionHandle();
        }
            public string GetPrimitiveType(PrimitiveTypeCode typeCode)
            {
                if (PrimitiveTypeMappings.TryGetValue(typeCode, out var type))
                {
                    return(type.FullName);
                }

                throw new ArgumentOutOfRangeException(nameof(typeCode), typeCode, @"Unexpected type code.");
            }
Пример #27
0
 public string GetPrimitiveType(PrimitiveTypeCode typeCode)
 {
     if (typeCode == PrimitiveTypeCode.String)
     {
         return(_valueReader.ReadSerializedString());
     }
     else
     {
         return(string.Empty);
     }
 }
        public IHandleTypeNamedWrapper GetPrimitiveType(PrimitiveTypeCode typeCode)
        {
            var element = typeCode.ToKnownTypeCode().ToTypeWrapper(MetadataRepository);

            if (element == null)
            {
                throw new InvalidOperationException("type definition is null for a primitive type.");
            }

            return(element);
        }
        public string GetPrimitiveType(PrimitiveTypeCode typeCode)
        {
            switch (typeCode)
            {
            case PrimitiveTypeCode.String:
                return("string");

            default:
                throw new System.NotImplementedException();
            }
        }
Пример #30
0
        // Token: 0x060011BF RID: 4543 RVA: 0x00061784 File Offset: 0x0005F984
        public JsonPrimitiveContract(Type underlyingType) : base(underlyingType)
        {
            this.ContractType          = JsonContractType.Primitive;
            this.TypeCode              = ConvertUtils.GetTypeCode(underlyingType);
            this.IsReadOnlyOrFixedSize = true;
            ReadType internalReadType;

            if (JsonPrimitiveContract.ReadTypeMap.TryGetValue(this.NonNullableUnderlyingType, out internalReadType))
            {
                this.InternalReadType = internalReadType;
            }
        }
Пример #31
0
        private bool TryGetPrimitiveTypeCode(out PrimitiveTypeCode code)
        {
            if (ContainingType == null &&
                ContainingNamespace?.Name == Cx.SystemNamespace.Name &&
                primitiveTypeCodeMapping.TryGetValue(Name, out code))
            {
                return(true);
            }

            code = default;
            return(false);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonPrimitiveContract"/> class.
        /// </summary>
        /// <param name="underlyingType">The underlying type for the contract.</param>
        public JsonPrimitiveContract(Type underlyingType)
            : base(underlyingType)
        {
            ContractType = JsonContractType.Primitive;

            TypeCode = ConvertUtils.GetTypeCode(underlyingType);
            IsReadOnlyOrFixedSize = true;

            ReadType readType;
            if (ReadTypeMap.TryGetValue(NonNullableUnderlyingType, out readType))
            {
                InternalReadType = readType;
            }
        }
 private void EmitParametersOfFunctionPointer(bool first, IEnumerable<IParameterTypeInformation> parameters, PrimitiveTypeCode typeCode) {
   Contract.Requires(parameters != null);
   foreach (var par in parameters) {
     Contract.Assume(par != null);
     var parType = par.Type.ResolvedType;
     if (first) first = false;
     else this.sourceEmitter.EmitString(", ");
     if (par.IsByReference || (parType.IsValueType && !IsScalarInC(parType)))
       this.sourceEmitter.EmitString("uintptr_t ");
     else
       this.EmitTypeReference(parType, storageLocation: true);
   }
   if (typeCode != PrimitiveTypeCode.Void) {
     if (first) first = false;
     else this.sourceEmitter.EmitString(", ");
     this.sourceEmitter.EmitString("uintptr_t");
   }
   this.sourceEmitter.EmitString(")");
 }
Пример #34
0
        /// <summary>
        /// Allocates a type definition that is a member of a namespace definition.
        /// </summary>
        /// <param name="host">Provides a standard abstraction over the applications that host components that provide or consume objects from the metadata model.</param>
        /// <param name="containingUnitNamespace">The namespace that contains the referenced type.</param>
        /// <param name="name">The name of the referenced type.</param>
        /// <param name="genericParameterCount">The number of generic parameters. Zero if the type is not generic.</param>
        /// <param name="isEnum">True if the type is an enumeration (it extends System.Enum and is sealed). Corresponds to C# enum.</param>
        /// <param name="isValueType">True if the referenced type is a value type.</param>
        /// <param name="typeCode">A value indicating if the type is a primitive type or not.</param>
        public NamespaceTypeReference(IMetadataHost host, IUnitNamespaceReference containingUnitNamespace, IName name, ushort genericParameterCount, bool isEnum, bool isValueType, PrimitiveTypeCode typeCode)
            : base(host, isEnum, isValueType)
        {
            Contract.Requires(host != null);
              Contract.Requires(containingUnitNamespace != null);
              Contract.Requires(name != null);

              this.containingUnitNamespace = containingUnitNamespace;
              this.name = name;
              this.genericParameterCount = genericParameterCount;
              this.typeCode = typeCode;
        }
Пример #35
0
 public INamespaceTypeReference GetTypeFor(PrimitiveTypeCode typeCode)
 {
     switch (typeCode) {
     case PrimitiveTypeCode.Float32: return this.SystemFloat32;
     case PrimitiveTypeCode.Float64: return this.SystemFloat64;
     case PrimitiveTypeCode.Int16: return this.SystemInt16;
     case PrimitiveTypeCode.Int32: return this.SystemInt32;
     case PrimitiveTypeCode.Int64: return this.SystemInt64;
     case PrimitiveTypeCode.Int8: return this.SystemInt8;
     case PrimitiveTypeCode.IntPtr: return this.SystemIntPtr;
     case PrimitiveTypeCode.UInt16: return this.SystemUInt16;
     case PrimitiveTypeCode.UInt32: return this.SystemUInt32;
     case PrimitiveTypeCode.UInt64: return this.SystemUInt64;
     case PrimitiveTypeCode.UInt8: return this.SystemUInt8;
     case PrimitiveTypeCode.UIntPtr: return this.SystemUIntPtr;
     case PrimitiveTypeCode.Void: return this.SystemVoid;
     default:
       //^ assume false; //TODO: make Boogie aware of distinction between bit maps and enums
       return Dummy.NamespaceTypeReference;
       }
 }
Пример #36
0
 /// <summary>
 /// Creates a type reference to a namespace type from the given assembly, where the last element of the names
 /// array is the name of the type and the other elements are the names of the namespaces.
 /// </summary>
 /// <param name="assemblyReference">A reference to the assembly that contains the type for which a reference is desired.</param>
 /// <param name="isValueType">True if the referenced type is known to be a value type.</param>
 /// <param name="genericParameterCount">The number of generic parameters, if any, that the type has must. Must be zero or more.</param>
 /// <param name="typeCode">A code that identifies what kind of type is being referenced.</param>
 /// <param name="names">The last entry of this array is the name of the type, the others are the names of the containing namespaces.</param>
 public INamespaceTypeReference CreateReference(IAssemblyReference assemblyReference, bool isValueType, ushort genericParameterCount, PrimitiveTypeCode typeCode, params string[] names)
 {
     IUnitNamespaceReference ns = new RootUnitNamespaceReference(assemblyReference);
       for (int i = 0, n = names.Length-1; i < n; i++)
     ns = new NestedUnitNamespaceReference(ns, this.host.NameTable.GetNameFor(names[i]));
       return new NamespaceTypeReference(this.host, ns, this.host.NameTable.GetNameFor(names[names.Length-1]), genericParameterCount, false, isValueType, typeCode);
 }
Пример #37
0
 /// <summary>
 /// Creates a type reference to a namespace type from the given assembly, where the last element of the names
 /// array is the name of the type and the other elements are the names of the namespaces.
 /// </summary>
 /// <param name="assemblyReference">A reference to the assembly that contains the type for which a reference is desired.</param>
 /// <param name="typeCode">A code that identifies what kind of type is being referenced.</param>
 /// <param name="names">The last entry of this array is the name of the type, the others are the names of the containing namespaces.</param>
 public INamespaceTypeReference CreateReference(IAssemblyReference assemblyReference, PrimitiveTypeCode typeCode, params string[] names)
 {
     return this.CreateReference(assemblyReference, true, 0, typeCode, names);
 }
    /// <summary>
    /// Initializes a new instance of the <see cref="JsonPrimitiveContract"/> class.
    /// </summary>
    /// <param name="underlyingType">The underlying type for the contract.</param>
    public JsonPrimitiveContract(Type underlyingType)
      : base(underlyingType)
    {
      ContractType = JsonContractType.Primitive;

      // get the underlying enum value
      Type t;
      if (ReflectionUtils.IsNullableType(underlyingType))
        t = Nullable.GetUnderlyingType(underlyingType).IsEnum() ? ReflectionUtils.MakeGenericType(typeof(Nullable<>), Enum.GetUnderlyingType(Nullable.GetUnderlyingType(underlyingType))) : underlyingType;
      else
        t = underlyingType.IsEnum() ? Enum.GetUnderlyingType(underlyingType) : underlyingType;

      PrimitiveTypeCode typeCode;
      TypeCodeMap.TryGetValue(t, out typeCode);
      TypeCode = typeCode;
    }
Пример #39
0
        private void DefineLocalConstant(string name, object value, PrimitiveTypeCode typeCode, uint constantSignatureToken)
        {
            if (value == null)
            {
                // ISymUnmanagedWriter2.DefineConstant2 throws an ArgumentException
                // if you pass in null - Dev10 appears to use 0 instead.
                // (See EMITTER::VariantFromConstVal)
                value = 0;
                typeCode = PrimitiveTypeCode.Int32;
            }

            if (typeCode == PrimitiveTypeCode.String)
            {
                DefineLocalStringConstant(name, (string)value, constantSignatureToken);
            }
            else if (value is DateTime)
            {
                // Marshal.GetNativeVariantForObject would create a variant with type VT_DATE and value equal to the
                // number of days since 1899/12/30.  However, ConstantValue::VariantFromConstant in the native VB
                // compiler actually created a variant with type VT_DATE and value equal to the tick count.
                // http://blogs.msdn.com/b/ericlippert/archive/2003/09/16/eric-s-complete-guide-to-vt-date.aspx
                var dt = (DateTime)value;
                _symWriter.DefineConstant2(name, new VariantStructure(dt), constantSignatureToken);
                if (_callLogger.LogOperation(OP.DefineConstant2))
                {
                    _callLogger.LogArgument(name);
                    _callLogger.LogArgument(constantSignatureToken);
                    _callLogger.LogArgument(dt.ToBinary());
                }
            }
            else
            {
                try
                {
                    DefineLocalConstantImpl(name, value, constantSignatureToken);
                    if (_callLogger.LogOperation(OP.DefineConstant2))
                    {
                        _callLogger.LogArgument(name);
                        _callLogger.LogArgument(constantSignatureToken);
                        _callLogger.LogArgument(value);
                    }
                }
                catch (Exception ex)
                {
                    throw new PdbWritingException(ex);
                }
            }
        }
Пример #40
0
    internal static void WriteValue(JsonWriter writer, PrimitiveTypeCode typeCode, object value)
    {
      switch (typeCode)
      {
        case PrimitiveTypeCode.Char:
          writer.WriteValue((char)value);
          break;
        case PrimitiveTypeCode.CharNullable:
          writer.WriteValue((value == null) ? (char?)null : (char)value);
          break;
        case PrimitiveTypeCode.Boolean:
          writer.WriteValue((bool)value);
          break;
        case PrimitiveTypeCode.BooleanNullable:
          writer.WriteValue((value == null) ? (bool?)null : (bool)value);
          break;
        case PrimitiveTypeCode.SByte:
          writer.WriteValue((sbyte)value);
          break;
        case PrimitiveTypeCode.SByteNullable:
          writer.WriteValue((value == null) ? (sbyte?)null : (sbyte)value);
          break;
        case PrimitiveTypeCode.Int16:
          writer.WriteValue((short)value);
          break;
        case PrimitiveTypeCode.Int16Nullable:
          writer.WriteValue((value == null) ? (short?)null : (short)value);
          break;
        case PrimitiveTypeCode.UInt16:
          writer.WriteValue((ushort)value);
          break;
        case PrimitiveTypeCode.UInt16Nullable:
          writer.WriteValue((value == null) ? (ushort?)null : (ushort)value);
          break;
        case PrimitiveTypeCode.Int32:
          writer.WriteValue((int)value);
          break;
        case PrimitiveTypeCode.Int32Nullable:
          writer.WriteValue((value == null) ? (int?)null : (int)value);
          break;
        case PrimitiveTypeCode.Byte:
          writer.WriteValue((byte)value);
          break;
        case PrimitiveTypeCode.ByteNullable:
          writer.WriteValue((value == null) ? (byte?)null : (byte)value);
          break;
        case PrimitiveTypeCode.UInt32:
          writer.WriteValue((uint)value);
          break;
        case PrimitiveTypeCode.UInt32Nullable:
          writer.WriteValue((value == null) ? (uint?)null : (uint)value);
          break;
        case PrimitiveTypeCode.Int64:
          writer.WriteValue((long)value);
          break;
        case PrimitiveTypeCode.Int64Nullable:
          writer.WriteValue((value == null) ? (long?)null : (long)value);
          break;
        case PrimitiveTypeCode.UInt64:
          writer.WriteValue((ulong)value);
          break;
        case PrimitiveTypeCode.UInt64Nullable:
          writer.WriteValue((value == null) ? (ulong?)null : (ulong)value);
          break;
        case PrimitiveTypeCode.Single:
          writer.WriteValue((float)value);
          break;
        case PrimitiveTypeCode.SingleNullable:
          writer.WriteValue((value == null) ? (float?)null : (float)value);
          break;
        case PrimitiveTypeCode.Double:
          writer.WriteValue((double)value);
          break;
        case PrimitiveTypeCode.DoubleNullable:
          writer.WriteValue((value == null) ? (double?)null : (double)value);
          break;
        case PrimitiveTypeCode.DateTime:
          writer.WriteValue((DateTime)value);
          break;
        case PrimitiveTypeCode.DateTimeNullable:
          writer.WriteValue((value == null) ? (DateTime?)null : (DateTime)value);
          break;
#if !NET20
        case PrimitiveTypeCode.DateTimeOffset:
          writer.WriteValue((DateTimeOffset)value);
          break;
        case PrimitiveTypeCode.DateTimeOffsetNullable:
          writer.WriteValue((value == null) ? (DateTimeOffset?)null : (DateTimeOffset)value);
          break;
#endif
        case PrimitiveTypeCode.Decimal:
          writer.WriteValue((decimal)value);
          break;
        case PrimitiveTypeCode.DecimalNullable:
          writer.WriteValue((value == null) ? (decimal?)null : (decimal)value);
          break;
        case PrimitiveTypeCode.Guid:
          writer.WriteValue((Guid)value);
          break;
        case PrimitiveTypeCode.GuidNullable:
          writer.WriteValue((value == null) ? (Guid?)null : (Guid)value);
          break;
        case PrimitiveTypeCode.TimeSpan:
          writer.WriteValue((TimeSpan)value);
          break;
        case PrimitiveTypeCode.TimeSpanNullable:
          writer.WriteValue((value == null) ? (TimeSpan?)null : (TimeSpan)value);
          break;
#if !(PORTABLE || PORTABLE40 || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT)
        case PrimitiveTypeCode.BigInteger:
          writer.WriteValue((BigInteger)value);
          break;
        case PrimitiveTypeCode.BigIntegerNullable:
          writer.WriteValue((value == null) ? (BigInteger?)null : (BigInteger)value);
          break;
#endif
        case PrimitiveTypeCode.Uri:
          writer.WriteValue((Uri)value);
          break;
        case PrimitiveTypeCode.String:
          writer.WriteValue((string)value);
          break;
        case PrimitiveTypeCode.Bytes:
          writer.WriteValue((byte[])value);
          break;
#if !(PORTABLE || NETFX_CORE)
        case PrimitiveTypeCode.DBNull:
          writer.WriteNull();
          break;
#endif
        default:
#if !(PORTABLE || NETFX_CORE)
          if (value is IConvertible)
          {
            // the value is a non-standard IConvertible
            // convert to the underlying value and retry
            IConvertible convertable = (IConvertible)value;
            TypeInformation typeInformation = ConvertUtils.GetTypeInformation(convertable);
            object convertedValue = convertable.ToType(typeInformation.Type, CultureInfo.InvariantCulture);

            WriteValue(writer, typeInformation.TypeCode, convertedValue);
          }
          else
#endif
          {
            throw JsonWriterException.Create(writer, "Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null);
          }
          break;
      }
    }
Пример #41
0
        internal static void WritePrimitiveType(BlobBuilder builder, PrimitiveTypeCode type)
        {
            switch (type)
            {
                case PrimitiveTypeCode.Boolean:
                case PrimitiveTypeCode.Byte:
                case PrimitiveTypeCode.SByte:
                case PrimitiveTypeCode.Char:
                case PrimitiveTypeCode.Int16:
                case PrimitiveTypeCode.UInt16:
                case PrimitiveTypeCode.Int32:
                case PrimitiveTypeCode.UInt32:
                case PrimitiveTypeCode.Int64:
                case PrimitiveTypeCode.UInt64:
                case PrimitiveTypeCode.Single:
                case PrimitiveTypeCode.Double:
                case PrimitiveTypeCode.IntPtr:
                case PrimitiveTypeCode.UIntPtr:
                case PrimitiveTypeCode.String:
                case PrimitiveTypeCode.Object:
                    builder.WriteByte((byte)type);
                    return;

                // TODO: should we allow these?
                case PrimitiveTypeCode.TypedReference:
                case PrimitiveTypeCode.Void:
                default:
                    Throw.ArgumentOutOfRange(nameof(type));
                    return;
            }
        }
 public INamespaceTypeReference GetTypeFor(PrimitiveTypeCode typeCode) {
   return Dummy.NamespaceTypeReference;
 }
        internal static void WriteValue(JsonWriter writer, PrimitiveTypeCode typeCode, object value)
        {
            switch (typeCode)
            {
                case PrimitiveTypeCode.Char:
                    writer.WriteValue((char)value);
                    break;
                case PrimitiveTypeCode.CharNullable:
                    writer.WriteValue((char?)value);
                    break;
                case PrimitiveTypeCode.Boolean:
                    writer.WriteValue((bool)value);
                    break;
                case PrimitiveTypeCode.BooleanNullable:
                    writer.WriteValue((bool?)value);
                    break;
                case PrimitiveTypeCode.SByte:
                    writer.WriteValue((sbyte)value);
                    break;
                case PrimitiveTypeCode.SByteNullable:
                    writer.WriteValue((sbyte?)value);
                    break;
                case PrimitiveTypeCode.Int16:
                    writer.WriteValue((short)value);
                    break;
                case PrimitiveTypeCode.Int16Nullable:
                    writer.WriteValue((short?)value);
                    break;
                case PrimitiveTypeCode.UInt16:
                    writer.WriteValue((ushort)value);
                    break;
                case PrimitiveTypeCode.UInt16Nullable:
                    writer.WriteValue((ushort?)value);
                    break;
                case PrimitiveTypeCode.Int32:
                    writer.WriteValue((int)value);
                    break;
                case PrimitiveTypeCode.Int32Nullable:
                    writer.WriteValue((int?)value);
                    break;
                case PrimitiveTypeCode.Byte:
                    writer.WriteValue((byte)value);
                    break;
                case PrimitiveTypeCode.ByteNullable:
                    writer.WriteValue((byte?)value);
                    break;
                case PrimitiveTypeCode.UInt32:
                    writer.WriteValue((uint)value);
                    break;
                case PrimitiveTypeCode.UInt32Nullable:
                    writer.WriteValue((uint?)value);
                    break;
                case PrimitiveTypeCode.Int64:
                    writer.WriteValue((long)value);
                    break;
                case PrimitiveTypeCode.Int64Nullable:
                    writer.WriteValue((long?)value);
                    break;
                case PrimitiveTypeCode.UInt64:
                    writer.WriteValue((ulong)value);
                    break;
                case PrimitiveTypeCode.UInt64Nullable:
                    writer.WriteValue((ulong?)value);
                    break;
                case PrimitiveTypeCode.Single:
                    writer.WriteValue((float)value);
                    break;
                case PrimitiveTypeCode.SingleNullable:
                    writer.WriteValue((float?)value);
                    break;
                case PrimitiveTypeCode.Double:
                    writer.WriteValue((double)value);
                    break;
                case PrimitiveTypeCode.DoubleNullable:
                    writer.WriteValue((double?)value);
                    break;
                case PrimitiveTypeCode.DateTime:
                    writer.WriteValue((DateTime)value);
                    break;
                case PrimitiveTypeCode.DateTimeNullable:
                    writer.WriteValue((DateTime?)value);
                    break;
                case PrimitiveTypeCode.Decimal:
                    writer.WriteValue((decimal)value);
                    break;
                case PrimitiveTypeCode.DecimalNullable:
                    writer.WriteValue((decimal?)value);
                    break;
                case PrimitiveTypeCode.Guid:
                    writer.WriteValue((Guid)value);
                    break;
                case PrimitiveTypeCode.GuidNullable:
                    writer.WriteValue((Guid?)value);
                    break;
                case PrimitiveTypeCode.TimeSpan:
                    writer.WriteValue((TimeSpan)value);
                    break;
                case PrimitiveTypeCode.TimeSpanNullable:
                    writer.WriteValue((TimeSpan?)value);
                    break;
                case PrimitiveTypeCode.Uri:
                    writer.WriteValue((Uri)value);
                    break;
                case PrimitiveTypeCode.String:
                    writer.WriteValue((string)value);
                    break;
                case PrimitiveTypeCode.Bytes:
                    writer.WriteValue((byte[])value);
                    break;

                default:
                    if (value is IConvertible)
                    {
                        // the value is a non-standard IConvertible
                        // convert to the underlying value and retry
                        IConvertible convertable = (IConvertible)value;

                        TypeInformation typeInformation = ConvertUtils.GetTypeInformation(convertable);

                        // if convertable has an underlying typecode of Object then attempt to convert it to a string
                        PrimitiveTypeCode resolvedTypeCode = (typeInformation.TypeCode == PrimitiveTypeCode.Object) ? PrimitiveTypeCode.String : typeInformation.TypeCode;
                        Type resolvedType = (typeInformation.TypeCode == PrimitiveTypeCode.Object) ? typeof(string) : typeInformation.Type;

                        object convertedValue = convertable.ToType(resolvedType, CultureInfo.InvariantCulture);

                        WriteValue(writer, resolvedTypeCode, convertedValue);
                        break;
                    }
                    else
                    {
                        throw CreateUnsupportedTypeException(writer, value);
                    }
            }
        }
Пример #44
0
 private static object ReadValue(PrimitiveTypeCode primitiveTypeCode, BinaryReader reader) {
   Contract.Requires(reader != null);
   switch (primitiveTypeCode) {
     case PrimitiveTypeCode.Boolean: return reader.ReadBoolean();
     case PrimitiveTypeCode.Char: return (char)reader.ReadUInt16();
     case PrimitiveTypeCode.Float32: return reader.ReadSingle();
     case PrimitiveTypeCode.Float64: return reader.ReadDouble();
     case PrimitiveTypeCode.Int16: return reader.ReadInt16();
     case PrimitiveTypeCode.Int32: return reader.ReadInt32();
     case PrimitiveTypeCode.Int64: return reader.ReadInt64();
     case PrimitiveTypeCode.Int8: return reader.ReadSByte();
     case PrimitiveTypeCode.UInt16: return reader.ReadUInt16();
     case PrimitiveTypeCode.UInt32: return reader.ReadUInt32();
     case PrimitiveTypeCode.UInt64: return reader.ReadUInt64();
     case PrimitiveTypeCode.UInt8: return reader.ReadByte();
     default:
       Contract.Assume(false);
       break;
   }
   return null;
 }
Пример #45
0
        public void DefineLocalConstant(string name, object value, PrimitiveTypeCode typeCode, uint constantSignatureToken)
        {
            if (value == null)
            {
                // ISymUnmanagedWriter2.DefineConstant2 throws an ArgumentException
                // if you pass in null - Dev10 appears to use 0 instead.
                // (See EMITTER::VariantFromConstVal)
                value = 0;
                typeCode = PrimitiveTypeCode.Int32;
            }

            if (typeCode == PrimitiveTypeCode.String)
            {
                DefineLocalStringConstant(name, (string)value, constantSignatureToken);
            }
            else
            {
                try
                {
                    this.symWriter.DefineConstant2(name, value, constantSignatureToken);
                }
                catch (Exception ex)
                {
                    throw new PdbWritingException(ex);
                }
            }
        }
Пример #46
0
        /// <summary>
        /// Writes primitive type code.
        /// </summary>
        /// <param name="type">Any primitive type code except for <see cref="PrimitiveTypeCode.TypedReference"/> and <see cref="PrimitiveTypeCode.Void"/>.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="type"/> is not valid in this context.</exception>
        public void PrimitiveType(PrimitiveTypeCode type)
        {
            switch (type)
            {
                case PrimitiveTypeCode.Boolean:
                case PrimitiveTypeCode.Byte:
                case PrimitiveTypeCode.SByte:
                case PrimitiveTypeCode.Char:
                case PrimitiveTypeCode.Int16:
                case PrimitiveTypeCode.UInt16:
                case PrimitiveTypeCode.Int32:
                case PrimitiveTypeCode.UInt32:
                case PrimitiveTypeCode.Int64:
                case PrimitiveTypeCode.UInt64:
                case PrimitiveTypeCode.Single:
                case PrimitiveTypeCode.Double:
                case PrimitiveTypeCode.IntPtr:
                case PrimitiveTypeCode.UIntPtr:
                case PrimitiveTypeCode.String:
                case PrimitiveTypeCode.Object:
                    Builder.WriteByte((byte)type);
                    return;

                case PrimitiveTypeCode.TypedReference:
                case PrimitiveTypeCode.Void:
                default:
                    Throw.ArgumentOutOfRange(nameof(type));
                    return;
            }
        }
Пример #47
0
 internal static void WriteValue(JsonWriter writer, PrimitiveTypeCode typeCode, object value)
 {
   switch (typeCode)
   {
     case PrimitiveTypeCode.Char:
       writer.WriteValue((char)value);
       break;
     case PrimitiveTypeCode.CharNullable:
       writer.WriteValue((value == null) ? (char?)null : (char)value);
       break;
     case PrimitiveTypeCode.Boolean:
       writer.WriteValue((bool)value);
       break;
     case PrimitiveTypeCode.BooleanNullable:
       writer.WriteValue((value == null) ? (bool?)null : (bool)value);
       break;
     case PrimitiveTypeCode.SByte:
       writer.WriteValue((sbyte)value);
       break;
     case PrimitiveTypeCode.SByteNullable:
       writer.WriteValue((value == null) ? (sbyte?)null : (sbyte)value);
       break;
     case PrimitiveTypeCode.Int16:
       writer.WriteValue((short)value);
       break;
     case PrimitiveTypeCode.Int16Nullable:
       writer.WriteValue((value == null) ? (short?)null : (short)value);
       break;
     case PrimitiveTypeCode.UInt16:
       writer.WriteValue((ushort)value);
       break;
     case PrimitiveTypeCode.UInt16Nullable:
       writer.WriteValue((value == null) ? (ushort?)null : (ushort)value);
       break;
     case PrimitiveTypeCode.Int32:
       writer.WriteValue((int)value);
       break;
     case PrimitiveTypeCode.Int32Nullable:
       writer.WriteValue((value == null) ? (int?)null : (int)value);
       break;
     case PrimitiveTypeCode.Byte:
       writer.WriteValue((byte)value);
       break;
     case PrimitiveTypeCode.ByteNullable:
       writer.WriteValue((value == null) ? (byte?)null : (byte)value);
       break;
     case PrimitiveTypeCode.UInt32:
       writer.WriteValue((uint)value);
       break;
     case PrimitiveTypeCode.UInt32Nullable:
       writer.WriteValue((value == null) ? (uint?)null : (uint)value);
       break;
     case PrimitiveTypeCode.Int64:
       writer.WriteValue((long)value);
       break;
     case PrimitiveTypeCode.Int64Nullable:
       writer.WriteValue((value == null) ? (long?)null : (long)value);
       break;
     case PrimitiveTypeCode.UInt64:
       writer.WriteValue((ulong)value);
       break;
     case PrimitiveTypeCode.UInt64Nullable:
       writer.WriteValue((value == null) ? (ulong?)null : (ulong)value);
       break;
     case PrimitiveTypeCode.Single:
       writer.WriteValue((float)value);
       break;
     case PrimitiveTypeCode.SingleNullable:
       writer.WriteValue((value == null) ? (float?)null : (float)value);
       break;
     case PrimitiveTypeCode.Double:
       writer.WriteValue((double)value);
       break;
     case PrimitiveTypeCode.DoubleNullable:
       writer.WriteValue((value == null) ? (double?)null : (double)value);
       break;
     case PrimitiveTypeCode.DateTime:
       writer.WriteValue((DateTime)value);
       break;
     case PrimitiveTypeCode.DateTimeNullable:
       writer.WriteValue((value == null) ? (DateTime?)null : (DateTime)value);
       break;
     case PrimitiveTypeCode.DateTimeOffset:
       writer.WriteValue((DateTimeOffset)value);
       break;
     case PrimitiveTypeCode.DateTimeOffsetNullable:
       writer.WriteValue((value == null) ? (DateTimeOffset?)null : (DateTimeOffset)value);
       break;
     case PrimitiveTypeCode.Decimal:
       writer.WriteValue((decimal)value);
       break;
     case PrimitiveTypeCode.DecimalNullable:
       writer.WriteValue((value == null) ? (decimal?)null : (decimal)value);
       break;
     case PrimitiveTypeCode.Guid:
       writer.WriteValue((Guid)value);
       break;
     case PrimitiveTypeCode.GuidNullable:
       writer.WriteValue((value == null) ? (Guid?)null : (Guid)value);
       break;
     case PrimitiveTypeCode.TimeSpan:
       writer.WriteValue((TimeSpan)value);
       break;
     case PrimitiveTypeCode.TimeSpanNullable:
       writer.WriteValue((value == null) ? (TimeSpan?)null : (TimeSpan)value);
       break;
     case PrimitiveTypeCode.BigInteger:
       // this will call to WriteValue(object)
       writer.WriteValue((BigInteger)value);
       break;
     case PrimitiveTypeCode.BigIntegerNullable:
       // this will call to WriteValue(object)
       writer.WriteValue((value == null) ? (BigInteger?)null : (BigInteger)value);
       break;
     case PrimitiveTypeCode.Uri:
       writer.WriteValue((Uri)value);
       break;
     case PrimitiveTypeCode.String:
       writer.WriteValue((string)value);
       break;
     case PrimitiveTypeCode.Bytes:
       writer.WriteValue((byte[])value);
       break;
     default:
       {
         throw CreateUnsupportedTypeException(writer, value);
       }
   }
 }
Пример #48
0
 private static object ReadValue(PrimitiveTypeCode primitiveTypeCode, BinaryReader reader)
 {
     switch (primitiveTypeCode) {
     case PrimitiveTypeCode.Boolean: return reader.ReadBoolean();
     case PrimitiveTypeCode.Char: return reader.ReadChar();
     case PrimitiveTypeCode.Float32: return reader.ReadSingle();
     case PrimitiveTypeCode.Float64: return reader.ReadDouble();
     case PrimitiveTypeCode.Int16: return reader.ReadInt16();
     case PrimitiveTypeCode.Int32: return reader.ReadInt32();
     case PrimitiveTypeCode.Int64: return reader.ReadInt64();
     case PrimitiveTypeCode.Int8: return reader.ReadSByte();
     case PrimitiveTypeCode.UInt16: return reader.ReadUInt16();
     case PrimitiveTypeCode.UInt32: return reader.ReadUInt32();
     case PrimitiveTypeCode.UInt64: return reader.ReadUInt64();
     case PrimitiveTypeCode.UInt8: return reader.ReadByte();
     default:
       Debug.Assert(false);
       break;
       }
       return null;
 }
Пример #49
0
        internal static void WriteValue(JsonWriter writer, PrimitiveTypeCode typeCode, object value)
        {
            switch (typeCode)
            {
                case PrimitiveTypeCode.Char:
                    writer.WriteValue((char)value);
                    break;
                case PrimitiveTypeCode.CharNullable:
                    writer.WriteValue((value == null) ? (char?)null : (char)value);
                    break;
                case PrimitiveTypeCode.Boolean:
                    writer.WriteValue((bool)value);
                    break;
                case PrimitiveTypeCode.BooleanNullable:
                    writer.WriteValue((value == null) ? (bool?)null : (bool)value);
                    break;
                case PrimitiveTypeCode.SByte:
                    writer.WriteValue((sbyte)value);
                    break;
                case PrimitiveTypeCode.SByteNullable:
                    writer.WriteValue((value == null) ? (sbyte?)null : (sbyte)value);
                    break;
                case PrimitiveTypeCode.Int16:
                    writer.WriteValue((short)value);
                    break;
                case PrimitiveTypeCode.Int16Nullable:
                    writer.WriteValue((value == null) ? (short?)null : (short)value);
                    break;
                case PrimitiveTypeCode.UInt16:
                    writer.WriteValue((ushort)value);
                    break;
                case PrimitiveTypeCode.UInt16Nullable:
                    writer.WriteValue((value == null) ? (ushort?)null : (ushort)value);
                    break;
                case PrimitiveTypeCode.Int32:
                    writer.WriteValue((int)value);
                    break;
                case PrimitiveTypeCode.Int32Nullable:
                    writer.WriteValue((value == null) ? (int?)null : (int)value);
                    break;
                case PrimitiveTypeCode.Byte:
                    writer.WriteValue((byte)value);
                    break;
                case PrimitiveTypeCode.ByteNullable:
                    writer.WriteValue((value == null) ? (byte?)null : (byte)value);
                    break;
                case PrimitiveTypeCode.UInt32:
                    writer.WriteValue((uint)value);
                    break;
                case PrimitiveTypeCode.UInt32Nullable:
                    writer.WriteValue((value == null) ? (uint?)null : (uint)value);
                    break;
                case PrimitiveTypeCode.Int64:
                    writer.WriteValue((long)value);
                    break;
                case PrimitiveTypeCode.Int64Nullable:
                    writer.WriteValue((value == null) ? (long?)null : (long)value);
                    break;
                case PrimitiveTypeCode.UInt64:
                    writer.WriteValue((ulong)value);
                    break;
                case PrimitiveTypeCode.UInt64Nullable:
                    writer.WriteValue((value == null) ? (ulong?)null : (ulong)value);
                    break;
                case PrimitiveTypeCode.Single:
                    writer.WriteValue((float)value);
                    break;
                case PrimitiveTypeCode.SingleNullable:
                    writer.WriteValue((value == null) ? (float?)null : (float)value);
                    break;
                case PrimitiveTypeCode.Double:
                    writer.WriteValue((double)value);
                    break;
                case PrimitiveTypeCode.DoubleNullable:
                    writer.WriteValue((value == null) ? (double?)null : (double)value);
                    break;
                case PrimitiveTypeCode.DateTime:
                    writer.WriteValue((DateTime)value);
                    break;
                case PrimitiveTypeCode.DateTimeNullable:
                    writer.WriteValue((value == null) ? (DateTime?)null : (DateTime)value);
                    break;
#if !NET20
                case PrimitiveTypeCode.DateTimeOffset:
                    writer.WriteValue((DateTimeOffset)value);
                    break;
                case PrimitiveTypeCode.DateTimeOffsetNullable:
                    writer.WriteValue((value == null) ? (DateTimeOffset?)null : (DateTimeOffset)value);
                    break;
#endif
                case PrimitiveTypeCode.Decimal:
                    writer.WriteValue((decimal)value);
                    break;
                case PrimitiveTypeCode.DecimalNullable:
                    writer.WriteValue((value == null) ? (decimal?)null : (decimal)value);
                    break;
                case PrimitiveTypeCode.Guid:
                    writer.WriteValue((Guid)value);
                    break;
                case PrimitiveTypeCode.GuidNullable:
                    writer.WriteValue((value == null) ? (Guid?)null : (Guid)value);
                    break;
                case PrimitiveTypeCode.TimeSpan:
                    writer.WriteValue((TimeSpan)value);
                    break;
                case PrimitiveTypeCode.TimeSpanNullable:
                    writer.WriteValue((value == null) ? (TimeSpan?)null : (TimeSpan)value);
                    break;
#if !(SILVERLIGHT || NET35 || NET20)
                case PrimitiveTypeCode.BigInteger:
                    // this will call to WriteValue(object)
                    writer.WriteValue((BigInteger)value);
                    break;
                case PrimitiveTypeCode.BigIntegerNullable:
                    // this will call to WriteValue(object)
                    writer.WriteValue((value == null) ? (BigInteger?)null : (BigInteger)value);
                    break;
#endif
                case PrimitiveTypeCode.Uri:
                    writer.WriteValue((Uri)value);
                    break;
                case PrimitiveTypeCode.String:
                    writer.WriteValue((string)value);
                    break;
                case PrimitiveTypeCode.Bytes:
                    writer.WriteValue((byte[])value);
                    break;
#if !(NETFX_CORE)
                case PrimitiveTypeCode.DBNull:
                    writer.WriteNull();
                    break;
#endif
                default:
#if !(NETFX_CORE)
                    if (value is IConvertible)
                    {
                        // the value is a non-standard IConvertible
                        // convert to the underlying value and retry
                        IConvertible convertable = (IConvertible)value;

                        TypeInformation typeInformation = ConvertUtils.GetTypeInformation(convertable);

                        // if convertable has an underlying typecode of Object then attempt to convert it to a string
                        PrimitiveTypeCode resolvedTypeCode = (typeInformation.TypeCode == PrimitiveTypeCode.Object) ? PrimitiveTypeCode.String : typeInformation.TypeCode;
                        Type resolvedType = (typeInformation.TypeCode == PrimitiveTypeCode.Object) ? typeof(string) : typeInformation.Type;

                        object convertedValue = convertable.ToType(resolvedType, CultureInfo.InvariantCulture);

                        WriteValue(writer, resolvedTypeCode, convertedValue);
                        break;
                    }
                    else
#endif
                    {
                        throw CreateUnsupportedTypeException(writer, value);
                    }
            }
        }
Пример #50
0
 // Token: 0x06000144 RID: 324
 // RVA: 0x0002AE50 File Offset: 0x00029050
 internal static void WriteValue(JsonWriter writer, PrimitiveTypeCode typeCode, object value)
 {
     switch (typeCode)
     {
     case PrimitiveTypeCode.Char:
         writer.WriteValue((char)value);
         return;
     case PrimitiveTypeCode.CharNullable:
         writer.WriteValue((value == null) ? null : new char?((char)value));
         return;
     case PrimitiveTypeCode.Boolean:
         writer.WriteValue((bool)value);
         return;
     case PrimitiveTypeCode.BooleanNullable:
         writer.WriteValue((value == null) ? null : new bool?((bool)value));
         return;
     case PrimitiveTypeCode.SByte:
         writer.WriteValue((sbyte)value);
         return;
     case PrimitiveTypeCode.SByteNullable:
         writer.WriteValue((value == null) ? null : new sbyte?((sbyte)value));
         return;
     case PrimitiveTypeCode.Int16:
         writer.WriteValue((short)value);
         return;
     case PrimitiveTypeCode.Int16Nullable:
         writer.WriteValue((value == null) ? null : new short?((short)value));
         return;
     case PrimitiveTypeCode.UInt16:
         writer.WriteValue((ushort)value);
         return;
     case PrimitiveTypeCode.UInt16Nullable:
         writer.WriteValue((value == null) ? null : new ushort?((ushort)value));
         return;
     case PrimitiveTypeCode.Int32:
         writer.WriteValue((int)value);
         return;
     case PrimitiveTypeCode.Int32Nullable:
         writer.WriteValue((value == null) ? null : new int?((int)value));
         return;
     case PrimitiveTypeCode.Byte:
         writer.WriteValue((byte)value);
         return;
     case PrimitiveTypeCode.ByteNullable:
         writer.WriteValue((value == null) ? null : new byte?((byte)value));
         return;
     case PrimitiveTypeCode.UInt32:
         writer.WriteValue((uint)value);
         return;
     case PrimitiveTypeCode.UInt32Nullable:
         writer.WriteValue((value == null) ? null : new uint?((uint)value));
         return;
     case PrimitiveTypeCode.Int64:
         writer.WriteValue((long)value);
         return;
     case PrimitiveTypeCode.Int64Nullable:
         writer.WriteValue((value == null) ? null : new long?((long)value));
         return;
     case PrimitiveTypeCode.UInt64:
         writer.WriteValue((ulong)value);
         return;
     case PrimitiveTypeCode.UInt64Nullable:
         writer.WriteValue((value == null) ? null : new ulong?((ulong)value));
         return;
     case PrimitiveTypeCode.Single:
         writer.WriteValue((float)value);
         return;
     case PrimitiveTypeCode.SingleNullable:
         writer.WriteValue((value == null) ? null : new float?((float)value));
         return;
     case PrimitiveTypeCode.Double:
         writer.WriteValue((double)value);
         return;
     case PrimitiveTypeCode.DoubleNullable:
         writer.WriteValue((value == null) ? null : new double?((double)value));
         return;
     case PrimitiveTypeCode.DateTime:
         writer.WriteValue((DateTime)value);
         return;
     case PrimitiveTypeCode.DateTimeNullable:
         writer.WriteValue((value == null) ? null : new DateTime?((DateTime)value));
         return;
     case PrimitiveTypeCode.Decimal:
         writer.WriteValue((decimal)value);
         return;
     case PrimitiveTypeCode.DecimalNullable:
         writer.WriteValue((value == null) ? null : new decimal?((decimal)value));
         return;
     case PrimitiveTypeCode.Guid:
         writer.WriteValue((Guid)value);
         return;
     case PrimitiveTypeCode.GuidNullable:
         writer.WriteValue((value == null) ? null : new Guid?((Guid)value));
         return;
     case PrimitiveTypeCode.TimeSpan:
         writer.WriteValue((TimeSpan)value);
         return;
     case PrimitiveTypeCode.TimeSpanNullable:
         writer.WriteValue((value == null) ? null : new TimeSpan?((TimeSpan)value));
         return;
     case PrimitiveTypeCode.Uri:
         writer.WriteValue((Uri)value);
         return;
     case PrimitiveTypeCode.String:
         writer.WriteValue((string)value);
         return;
     case PrimitiveTypeCode.Bytes:
         writer.WriteValue((byte[])value);
         return;
     case PrimitiveTypeCode.DBNull:
         writer.WriteNull();
         return;
     }
     if (value is IConvertible)
     {
         IConvertible convertible = (IConvertible)value;
         TypeInformation typeInformation = ConvertUtils.GetTypeInformation(convertible);
         PrimitiveTypeCode typeCode2 = (typeInformation.TypeCode == PrimitiveTypeCode.Object) ? PrimitiveTypeCode.String : typeInformation.TypeCode;
         Type conversionType = (typeInformation.TypeCode == PrimitiveTypeCode.Object) ? typeof(string) : typeInformation.Type;
         object value2 = convertible.ToType(conversionType, CultureInfo.InvariantCulture);
         JsonWriter.WriteValue(writer, typeCode2, value2);
         return;
     }
     throw JsonWriter.CreateUnsupportedTypeException(writer, value);
 }