internal override void CheckIfTypeSerializable(Type memberType, bool isMemberTypeSerializable)
        {
            if (mode == SerializationMode.SharedType && surrogateSelector != null &&
                CheckIfTypeSerializableForSharedTypeMode(memberType))
            {
                return;
            }
            else
            {
                if (dataContractSurrogate != null)
                {
                    while (memberType.IsArray)
                    {
                        memberType = memberType.GetElementType();
                    }

                    memberType = DataContractSurrogateCaller.GetDataContractType(dataContractSurrogate, memberType);
                    if (!DataContract.IsTypeSerializable(memberType))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new System.Runtime.Serialization.InvalidDataContractException(SR.Format(SR.TypeNotSerializable, memberType)));
                    }

                    return;
                }
            }

            base.CheckIfTypeSerializable(memberType, isMemberTypeSerializable);
        }
        private XmlElement ExportSurrogateData(object key)
        {
            object surrogateData = _dataContractSet.GetSurrogateData(key);

            if (surrogateData == null)
            {
                return(null);
            }

            StringWriter      stringWriter   = new StringWriter(CultureInfo.InvariantCulture);
            XmlWriterSettings writerSettings = new XmlWriterSettings
            {
                OmitXmlDeclaration = true
            };
            XmlWriter         xmlWriter  = XmlWriter.Create(stringWriter, writerSettings);
            Collection <Type> knownTypes = new Collection <Type>();

            DataContractSurrogateCaller.GetKnownCustomDataTypes(_dataContractSet.DataContractSurrogate, knownTypes);
            DataContractSerializer serializer = new DataContractSerializer(Globals.TypeOfObject,
                                                                           SurrogateDataAnnotationName.Name, SurrogateDataAnnotationName.Namespace, knownTypes, int.MaxValue,
                                                                           false /*ignoreExtensionDataObject*/, true /*preserveObjectReferences*/, null /*dataContractSurrogate*/);

            serializer.WriteObject(xmlWriter, surrogateData);
            xmlWriter.Flush();
            return((XmlElement)XmlDoc.ReadNode(XmlReader.Create(new StringReader(stringWriter.ToString()))));
        }
예제 #3
0
        internal static object SurrogateToDataContractType(IDataContractSurrogate dataContractSurrogate, object oldObj, Type surrogatedDeclaredType, ref Type objType)
        {
            object obj = DataContractSurrogateCaller.GetObjectToSerialize(dataContractSurrogate, oldObj, objType, surrogatedDeclaredType);

            if (obj != oldObj)
            {
                if (obj == null)
                {
                    objType = Globals.TypeOfObject;
                }
                else
                {
                    objType = obj.GetType();
                }
            }
            return(obj);
        }
        private object InternalDeserializeWithSurrogate(XmlReaderDelegator xmlReader, Type declaredType, DataContract surrogateDataContract, string name, string ns)
        {
            DataContract dataContract = surrogateDataContract ??
                                        GetDataContract(DataContractSurrogateCaller.GetDataContractType(dataContractSurrogate, declaredType));

            if (IsGetOnlyCollection && dataContract.UnderlyingType != declaredType)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new System.Runtime.Serialization.InvalidDataContractException(SR.Format(SR.SurrogatesWithGetOnlyCollectionsNotSupportedSerDeser,
                                                                                                                                                  DataContract.GetClrTypeFullName(declaredType))));
            }
            ReadAttributes(xmlReader);
            string objectId = GetObjectId();
            object oldObj   = InternalDeserialize(xmlReader, name, ns, declaredType, ref dataContract);
            object obj      = DataContractSurrogateCaller.GetDeserializedObject(dataContractSurrogate, oldObj, dataContract.UnderlyingType, declaredType);

            ReplaceDeserializedObject(objectId, oldObj, obj);

            return(obj);
        }
예제 #5
0
 internal static Type GetSurrogatedType(IDataContractSurrogate dataContractSurrogate, Type type)
 {
     return(DataContractSurrogateCaller.GetDataContractType(dataContractSurrogate, DataContract.UnwrapNullableType(type)));
 }