Exemplo n.º 1
0
        public object ReflectionReadClass(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString[] memberNames, XmlDictionaryString[] memberNamespaces, ClassDataContract classContract)
        {
            object obj = CreateObject(classContract);

            context.AddNewObject(obj);
            InvokeOnDeserializing(context, classContract, obj);

            if (classContract.IsISerializable)
            {
                obj = ReadISerializable(xmlReader, context, classContract);
            }
            else
            {
                ReflectionReadMembers(xmlReader, context, memberNames, memberNamespaces, classContract, ref obj);
            }

            if (obj is IObjectReference objectReference)
            {
                obj = context.GetRealObject(objectReference, context.GetObjectId());
            }

            obj = ResolveAdapterObject(obj, classContract);
            InvokeDeserializationCallback(obj);
            InvokeOnDeserialized(context, classContract, obj);

            return(obj);
        }
Exemplo n.º 2
0
        private object ReflectionReadCollectionCore(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString collectionItemName, XmlDictionaryString collectionItemNamespace, CollectionDataContract collectionContract)
        {
            bool isArray = (collectionContract.Kind == CollectionKind.Array);

            int    arraySize   = context.GetArraySize();
            string objectId    = context.GetObjectId();
            object resultArray = null;

            if (isArray && ReflectionTryReadPrimitiveArray(xmlReader, context, collectionItemName, collectionItemNamespace, collectionContract.UnderlyingType, collectionContract.ItemType, arraySize, out resultArray))
            {
                return(resultArray);
            }

            object resultCollection = ReflectionCreateCollection(collectionContract);

            context.AddNewObject(resultCollection);
            context.IncrementItemCount(1);

            if (!ReflectionReadSpecialCollection(xmlReader, context, collectionContract, resultCollection))
            {
                bool isReadOnlyCollection = false;
                resultCollection = ReadCollectionItems(xmlReader, context, collectionItemName, collectionItemNamespace, collectionContract, resultCollection, isReadOnlyCollection);
            }

            return(resultCollection);
        }
Exemplo n.º 3
0
        public override object ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
        {
            xmlReader.Read();
            Type   objType = UnderlyingType;
            object obj     = objType.IsArray ? Array.CreateInstance(objType.GetElementType(), 0) : GetUninitializedObject(objType);

            context.AddNewObject(obj);
            string            objectId = context.GetObjectId();
            SerializationInfo serInfo  = context.ReadSerializationInfo(xmlReader, objType);
            object            newObj   = SerializationSurrogateSetObjectData(obj, serInfo, context.GetStreamingContext());

            if (newObj == null)
            {
                newObj = obj;
            }
            if (newObj is IDeserializationCallback)
            {
                ((IDeserializationCallback)newObj).OnDeserialization(null);
            }
            if (newObj is IObjectReference)
            {
                newObj = GetRealObject((IObjectReference)newObj, context.GetStreamingContext());
            }
            context.ReplaceDeserializedObject(objectId, obj, newObj);
            xmlReader.ReadEndElement();
            return(newObj);
        }
Exemplo n.º 4
0
        public override object ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
        {
            XmlReaderDelegator xmlDelegator = ParseReaderString(xmlReader);

            ClassDataContract cdc = new ClassDataContract(this.UnderlyingType);

            // The Class Data Contract created from the underlying exception type uses custom imported members that are
            // created in this classes constructor. Here we clear out the Class Data Contract's default members and insert our own.
            cdc.Members.Clear();
            foreach (DataMember dm in this.Members)
            {
                cdc.Members.Add(dm);
            }

            cdc.MemberNames        = _memberNames;
            cdc.ContractNamespaces = _contractNamespaces;
            cdc.MemberNamespaces   = _memberNamespaces;

            object obj = cdc.ReadXmlValue(xmlDelegator, context);

            if (context != null)
            {
                context.AddNewObject(obj);
            }
            return(obj);
        }
Exemplo n.º 5
0
        public override object ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
        {
            object obj = ReadEnumValue(xmlReader);

            if (context != null)
            {
                context.AddNewObject(obj);
            }
            return(obj);
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
0
        public object ReflectionReadClass(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString[] memberNames, XmlDictionaryString[] memberNamespaces, ClassDataContract classContract)
        {
            object obj = CreateObject(classContract);

            context.AddNewObject(obj);
            InvokeOnDeserializing(context, classContract, obj);

            ReflectionReadMembers(xmlReader, context, memberNames, memberNamespaces, classContract, ref obj);
            obj = ResolveAdapterObject(obj, classContract);
            InvokeDeserializationCallback(obj);
            InvokeOnDeserialized(context, classContract, obj);

            return(obj);
        }
        public object ReadFromXml(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString[] memberNames, XmlDictionaryString[] memberNamespaces)
        {
            // InitArgs()
            this.xmlReader        = xmlReader;
            this.context          = context;
            this.memberNames      = memberNames;
            this.memberNamespaces = memberNamespaces;

            //DemandSerializationFormatterPermission(classContract);
            //DemandMemberAccessPermission(memberAccessFlag);
            CreateObject(classContract);

            context.AddNewObject(objectLocal);
            InvokeOnDeserializing(classContract);

            string objectId = null;

            if (HasFactoryMethod(classContract))
            {
                objectId = context.GetObjectId();
            }
            if (classContract.IsISerializable)
            {
                ReadISerializable(classContract);
            }
            else
            {
                ReadClass(classContract);
            }
            bool isFactoryType = InvokeFactoryMethod(classContract, objectId);

            if (Globals.TypeOfIDeserializationCallback.IsAssignableFrom(classContract.UnderlyingType))
            {
                ((IDeserializationCallback)objectLocal).OnDeserialization(null);
            }
            InvokeOnDeserialized(classContract);
            if (objectId == null || !isFactoryType)
            {
                // Do a conversion back from DateTimeOffsetAdapter to DateTimeOffset after deserialization.
                // DateTimeOffsetAdapter is used here for deserialization purposes to bypass the ISerializable implementation
                // on DateTimeOffset; which does not work in partial trust.

                if (classContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter)
                {
                    objectLocal = DateTimeOffsetAdapter.GetDateTimeOffset((DateTimeOffsetAdapter)objectLocal);
                }
                // else - do we have to call CodeInterpreter.ConvertValue()? I guess not...
            }
            return(objectLocal);
        }
Exemplo n.º 9
0
        public override object ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
        {
            object o;

            if (context == null)
            {
                o = XmlObjectSerializerReadContext.ReadRootIXmlSerializable(xmlReader, this, true /*isMemberType*/);
            }
            else
            {
                o = context.ReadIXmlSerializable(xmlReader, this, true /*isMemberType*/);
                context.AddNewObject(o);
            }
            xmlReader.ReadEndElement();
            return(o);
        }
Exemplo n.º 10
0
 protected static object HandleReadValue(object obj, XmlObjectSerializerReadContext context)
 {
     context.AddNewObject(obj);
     return(obj);
 }
        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);
        }