Пример #1
0
        private object ReadItemOfPrimitiveType(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, Type type, string name, string ns, PrimitiveDataContract primitiveContract, int nullables)
        {
            object value;

            context.ReadAttributes(xmlReader);
            string objectId        = context.ReadIfNullOrRef(xmlReader, type, DataContract.IsTypeSerializable(type));
            bool   typeIsValueType = type.IsValueType;

            if (objectId != null)
            {
                if (objectId.Length == 0)
                {
                    objectId = context.GetObjectId();

                    if (!string.IsNullOrEmpty(objectId) && typeIsValueType)
                    {
                        throw new SerializationException(SR.Format(SR.ValueTypeCannotHaveId, DataContract.GetClrTypeFullName(type)));
                    }

                    if (primitiveContract != null && primitiveContract.UnderlyingType != Globals.TypeOfObject)
                    {
                        value = primitiveContract.ReadXmlValue(xmlReader, context);
                    }
                    else
                    {
                        value = ReflectionInternalDeserialize(xmlReader, context, null /*collectionContract*/, type, name, ns);
                    }
                }
                else
                {
                    if (typeIsValueType)
                    {
                        throw new SerializationException(SR.Format(SR.ValueTypeCannotHaveRef, DataContract.GetClrTypeFullName(type)));
                    }
                    else
                    {
                        value = context.GetExistingObject(objectId, type, name, ns);
                    }
                }
            }
            else
            {
                if (typeIsValueType && nullables == 0)
                {
                    throw new SerializationException(SR.Format(SR.ValueTypeCannotBeNull, DataContract.GetClrTypeFullName(type)));
                }
                else
                {
                    value = null;
                }
            }

            return(value);
        }
Пример #2
0
        protected object ReflectionReadValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, Type type, string name, string ns, PrimitiveDataContract primitiveContractForOriginalType = null)
        {
            object value     = null;
            int    nullables = 0;

            while (type.GetTypeInfo().IsGenericType&& type.GetGenericTypeDefinition() == Globals.TypeOfNullable)
            {
                nullables++;
                type = type.GetGenericArguments()[0];
            }

            PrimitiveDataContract primitiveContract = nullables != 0 ?
                                                      PrimitiveDataContract.GetPrimitiveDataContract(type)
                : (primitiveContractForOriginalType ?? PrimitiveDataContract.GetPrimitiveDataContract(type));

            if ((primitiveContract != null && primitiveContract.UnderlyingType != Globals.TypeOfObject) || nullables != 0 || type.GetTypeInfo().IsValueType)
            {
                context.ReadAttributes(xmlReader);
                string objectId = context.ReadIfNullOrRef(xmlReader, Globals.TypeOfString, true);
                if (objectId != null)
                {
                    if (objectId.Length == 0)
                    {
                        objectId = context.GetObjectId();
                        if (primitiveContract != null && primitiveContract.UnderlyingType != Globals.TypeOfObject)
                        {
                            value = primitiveContract.ReadXmlValue(xmlReader, context);
                            context.AddNewObject(value);
                        }
                        else
                        {
                            value = ReflectionInternalDeserialize(xmlReader, context, null /*collectionContract*/, type, name, ns);
                        }
                    }
                    else if (type.GetTypeInfo().IsValueType)
                    {
                        throw new SerializationException(SR.Format(SR.ValueTypeCannotHaveId, DataContract.GetClrTypeFullName(type)));
                    }
                }
                else
                {
                    value = null;
                }
            }
            else
            {
                value = ReflectionInternalDeserialize(xmlReader, context, null /*collectionContract*/, type, name, ns);
            }

            return(value);
        }
        object ReadValue(Type type, string name, string ns)
        {
            var    valueType = type;
            object value     = null;
            bool   shouldAssignNullableValue = false;
            int    nullables = 0;

            while (type.IsGenericType && type.GetGenericTypeDefinition() == Globals.TypeOfNullable)
            {
                nullables++;
                type = type.GetGenericArguments() [0];
            }

            PrimitiveDataContract primitiveContract = PrimitiveDataContract.GetPrimitiveDataContract(type);

            if ((primitiveContract != null && primitiveContract.UnderlyingType != Globals.TypeOfObject) || nullables != 0 || type.IsValueType)
            {
                context.ReadAttributes(xmlReader);
                string objectId = context.ReadIfNullOrRef(xmlReader, type, DataContract.IsTypeSerializable(type));
                // Deserialize null
                if (objectId == Globals.NullObjectId)
                {
                    if (nullables != 0)
                    {
                        value = Activator.CreateInstance(valueType);
                    }
                    else if (type.IsValueType)
                    {
                        throw new SerializationException(SR.GetString(SR.ValueTypeCannotBeNull, DataContract.GetClrTypeFullName(type)));
                    }
                    else
                    {
                        value = null;
                    }
                }
                else if (objectId == string.Empty)
                {
                    // Deserialize value

                    // Compare against Globals.NewObjectId, which is set to string.Empty

                    objectId = context.GetObjectId();

                    if (type.IsValueType)
                    {
                        if (!string.IsNullOrEmpty(objectId))
                        {
                            throw new SerializationException(SR.GetString(SR.ValueTypeCannotHaveId, DataContract.GetClrTypeFullName(type)));
                        }
                    }
                    object innerValueRead = null;
                    if (nullables != 0)
                    {
                        shouldAssignNullableValue = true;
                    }

                    if (primitiveContract != null && primitiveContract.UnderlyingType != Globals.TypeOfObject)
                    {
                        value = primitiveContract.XmlFormatReaderMethod.Invoke(xmlReader, new object [0]);
                        if (!type.IsValueType)
                        {
                            context.AddNewObject(value);
                        }
                    }
                    else
                    {
                        value = InternalDeserialize(type, name, ns);
                    }
                }
                else
                {
                    // Deserialize ref
                    if (type.IsValueType)
                    {
                        throw new SerializationException(SR.GetString(SR.ValueTypeCannotHaveRef, DataContract.GetClrTypeFullName(type)));
                    }
                    else
                    {
                        value = CodeInterpreter.ConvertValue(context.GetExistingObject(objectId, type, name, ns), Globals.TypeOfObject, type);
                    }
                }

                if (shouldAssignNullableValue)
                {
                    if (objectId != Globals.NullObjectId)
                    {
                        value = WrapNullableObject(type, value, valueType, nullables);
                    }
                }
            }
            else
            {
                value = InternalDeserialize(type, name, ns);
            }

            return(value);
        }