Ejemplo n.º 1
0
        public object GetRealObject(IObjectReference obj, string id)
        {
            object realObject = SurrogateDataContract.GetRealObject(obj, base.GetStreamingContext());

            if (realObject == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(System.Runtime.Serialization.SR.GetString("GetRealObjectReturnedNull", new object[] { DataContract.GetClrTypeFullName(obj.GetType()) })));
            }
            this.ReplaceDeserializedObject(id, obj, realObject);
            return(realObject);
        }
        internal static DataContract GetDataContractFromSurrogateSelector(ISurrogateSelector surrogateSelector, StreamingContext context, RuntimeTypeHandle typeHandle, Type type, ref Hashtable surrogateDataContracts)
        {
            if (surrogateSelector == null)
            {
                return(null);
            }

            if (type == null)
            {
                type = Type.GetTypeFromHandle(typeHandle);
            }
            DataContract builtInDataContract = DataContract.GetBuiltInDataContract(type);

            if (builtInDataContract != null)
            {
                return(builtInDataContract);
            }
            if (surrogateDataContracts != null)
            {
                DataContract cachedSurrogateContract = (DataContract)surrogateDataContracts[type];
                if (cachedSurrogateContract != null)
                {
                    return(cachedSurrogateContract);
                }
            }
            DataContract            surrogateContract = null;
            ISerializationSurrogate surrogate         = GetSurrogate(type, surrogateSelector, context);

            if (surrogate != null)
            {
                surrogateContract = new SurrogateDataContract(type, surrogate);
            }
            else if (type.IsArray)
            {
                Type         elementType  = type.GetElementType();
                DataContract itemContract = GetDataContractFromSurrogateSelector(surrogateSelector, context, elementType.TypeHandle, elementType, ref surrogateDataContracts);
                if (itemContract == null)
                {
                    itemContract = DataContract.GetDataContract(elementType.TypeHandle, elementType, SerializationMode.SharedType);
                }
                surrogateContract = new CollectionDataContract(type, itemContract);
            }
            if (surrogateContract != null)
            {
                if (surrogateDataContracts == null)
                {
                    surrogateDataContracts = new Hashtable();
                }
                surrogateDataContracts.Add(type, surrogateContract);
                return(surrogateContract);
            }
            return(null);
        }
        public object GetRealObject(IObjectReference obj, string id)
        {
            object?realObj = SurrogateDataContract.GetRealObject(obj, this.GetStreamingContext());

            // If GetRealObject returns null, it indicates that the object could not resolve itself because
            // it is missing information. This may occur in a case where multiple IObjectReference instances
            // depend on each other. BinaryFormatter supports this by fixing up the references later. These
            // XmlObjectSerializer implementations do not support fix-ups since the format does not contain
            // forward references. However, we throw for this case since it allows us to add fix-up support
            // in the future if we need to.
            if (realObj == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException("error"));
            }
            ReplaceDeserializedObject(id, obj, realObj);
            return(realObj);
        }
Ejemplo n.º 4
0
        internal static DataContract GetDataContractFromSurrogateSelector(ISurrogateSelector surrogateSelector, StreamingContext context, RuntimeTypeHandle typeHandle, Type type, ref Hashtable surrogateDataContracts)
        {
            if (surrogateSelector == null)
                return null;

            if (type == null)
                type = Type.GetTypeFromHandle(typeHandle);
            DataContract builtInDataContract = DataContract.GetBuiltInDataContract(type);
            if (builtInDataContract != null)
                return builtInDataContract;
            if (surrogateDataContracts != null)
            {
                DataContract cachedSurrogateContract = (DataContract)surrogateDataContracts[type];
                if (cachedSurrogateContract != null)
                    return cachedSurrogateContract;
            }
            DataContract surrogateContract = null;
            ISerializationSurrogate surrogate = GetSurrogate(type, surrogateSelector, context);
            if (surrogate != null)
                surrogateContract = new SurrogateDataContract(type, surrogate);
            else if (type.IsArray)
            {
                Type elementType = type.GetElementType();
                DataContract itemContract = GetDataContractFromSurrogateSelector(surrogateSelector, context, elementType.TypeHandle, elementType, ref surrogateDataContracts);
                if (itemContract == null)
                    itemContract = DataContract.GetDataContract(elementType.TypeHandle, elementType, SerializationMode.SharedType);
                surrogateContract = new CollectionDataContract(type, itemContract);
            }
            if (surrogateContract != null)
            {
                if (surrogateDataContracts == null)
                    surrogateDataContracts = new Hashtable();
                surrogateDataContracts.Add(type, surrogateContract);
                return surrogateContract;
            }
            return null;
        }