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); }
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); }
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); }
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); }
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); }
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); }
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); }