XmlObjectSerializerContext.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / Serialization / System / Runtime / Serialization / XmlObjectSerializerContext.cs / 3 / XmlObjectSerializerContext.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------

namespace System.Runtime.Serialization 
{
    using System; 
    using System.Collections; 
    using System.Diagnostics;
    using System.Globalization; 
    using System.IO;
    using System.Reflection;
    using System.Text;
    using System.Xml; 
    using System.Collections.Generic;
    using DataContractDictionary = System.Collections.Generic.Dictionary; 
    using System.Security; 

#if USE_REFEMIT 
    public class XmlObjectSerializerContext
#else
    internal class XmlObjectSerializerContext
#endif 
    {
        protected XmlObjectSerializer serializer; 
        protected DataContract rootTypeDataContract; 
        internal ScopedKnownTypes scopedKnownTypes = new ScopedKnownTypes();
        protected DataContractDictionary serializerKnownDataContracts; 
        protected IList serializerKnownTypeList;
        /// 
        /// Critical - We base the decision whether to Demand SerializationFormatterPermission on this value
        ///  
        [SecurityCritical]
        bool demandedSerializationFormatterPermission; 
        ///  
        /// Critical - We base the decision whether to Demand MemberAccess permission on this value
        ///  
        [SecurityCritical]
        bool demandedMemberAccessPermission;
        int itemCount;
        int maxItemsInObjectGraph; 
        StreamingContext streamingContext;
        bool ignoreExtensionDataObject; 
 
        internal XmlObjectSerializerContext(XmlObjectSerializer serializer, int maxItemsInObjectGraph, StreamingContext streamingContext, bool ignoreExtensionDataObject)
        { 
            this.serializer = serializer;
            this.itemCount = 1;
            this.maxItemsInObjectGraph = maxItemsInObjectGraph;
            this.streamingContext = streamingContext; 
            this.ignoreExtensionDataObject = ignoreExtensionDataObject;
        } 
 
        internal XmlObjectSerializerContext(DataContractSerializer serializer, DataContract rootTypeDataContract)
            : this(serializer, 
            serializer.MaxItemsInObjectGraph,
            new StreamingContext(StreamingContextStates.All),
            serializer.IgnoreExtensionDataObject)
        { 
            this.rootTypeDataContract = rootTypeDataContract;
            this.serializerKnownTypeList = serializer.knownTypeList; 
            this.serializerKnownDataContracts = serializer.KnownDataContracts; 
        }
 
        internal XmlObjectSerializerContext(NetDataContractSerializer serializer)
            : this(serializer,
            serializer.MaxItemsInObjectGraph,
            serializer.Context, 
            serializer.IgnoreExtensionDataObject)
        { 
        } 

        internal virtual SerializationMode Mode 
        {
            get { return SerializationMode.SharedContract; }
        }
 
        internal virtual bool IsGetOnlyCollection
        { 
            get { return false; } 
            set {  }
        } 

        /// 
        /// Critical - Demands SerializationFormatter permission. demanding the right permission is critical
        /// Safe - no data or control leaks in or out, must be callable from transparent generated IL 
        /// 
        [SecurityCritical, SecurityTreatAsSafe] 
        public void DemandSerializationFormatterPermission() 
        {
            if (!demandedSerializationFormatterPermission) 
            {
                Globals.SerializationFormatterPermission.Demand();
                demandedSerializationFormatterPermission = true;
            } 
        }
 
        ///  
        /// Critical - Demands MemberAccess permission. demanding the right permission is critical
        /// Safe - no data or control leaks in or out, must be callable from transparent generated IL 
        /// 
        [SecurityCritical, SecurityTreatAsSafe]
        public void DemandMemberAccessPermission()
        { 
            if (!demandedMemberAccessPermission)
            { 
                Globals.MemberAccessPermission.Demand(); 
                demandedMemberAccessPermission = true;
            } 
        }

#if USE_REFEMIT
        public StreamingContext GetStreamingContext() 
#else
        internal StreamingContext GetStreamingContext() 
#endif 
        {
            return streamingContext; 
        }

#if USE_REFEMIT
        public void IncrementItemCount(int count) 
#else
        internal void IncrementItemCount(int count) 
#endif 
        {
            if (count > maxItemsInObjectGraph - itemCount) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.ExceededMaxItemsQuota, maxItemsInObjectGraph)));
            itemCount += count;
        }
 
        internal int RemainingItemCount
        { 
            get { return maxItemsInObjectGraph - itemCount; } 
        }
 
        internal bool IgnoreExtensionDataObject
        {
            get { return ignoreExtensionDataObject; }
        } 

        internal DataContract GetDataContract(Type type) 
        { 
            return GetDataContract(type.TypeHandle, type);
        } 

        internal virtual DataContract GetDataContract(RuntimeTypeHandle typeHandle, Type type)
        {
            if (IsGetOnlyCollection) 
            {
                return DataContract.GetGetOnlyCollectionDataContract(DataContract.GetId(typeHandle), typeHandle, type, Mode); 
            } 
            else
            { 
                return DataContract.GetDataContract(typeHandle, type, Mode);
            }
        }
 
        internal virtual DataContract GetDataContractSkipValidation(int typeId, RuntimeTypeHandle typeHandle, Type type)
        { 
            if (IsGetOnlyCollection) 
            {
                return DataContract.GetGetOnlyCollectionDataContractSkipValidation(typeId, typeHandle, type); 
            }
            else
            {
                return DataContract.GetDataContractSkipValidation(typeId, typeHandle, type); 
            }
        } 
 

        internal virtual DataContract GetDataContract(int id, RuntimeTypeHandle typeHandle) 
        {
            if (IsGetOnlyCollection)
            {
                return DataContract.GetGetOnlyCollectionDataContract(id, typeHandle, null /*type*/, Mode); 
            }
            else 
            { 
                return DataContract.GetDataContract(id, typeHandle, Mode);
            } 
        }

        internal virtual void CheckIfTypeSerializable(Type memberType, bool isMemberTypeSerializable)
        { 
            if (!isMemberTypeSerializable)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.TypeNotSerializable, memberType))); 
        } 

        internal virtual Type GetSurrogatedType(Type type) 
        {
            return type;
        }
 
        DataContractDictionary SerializerKnownDataContracts
        { 
            get 
            {
                // This field must be initialized during construction by serializers using data contracts. 
                return this.serializerKnownDataContracts;
            }
        }
 
        DataContract GetDataContractFromSerializerKnownTypes(XmlQualifiedName qname)
        { 
            DataContractDictionary serializerKnownDataContracts = this.SerializerKnownDataContracts; 
            if (serializerKnownDataContracts == null)
                return null; 
            DataContract outDataContract;
            return serializerKnownDataContracts.TryGetValue(qname, out outDataContract) ? outDataContract : null;
        }
 
        internal static DataContractDictionary GetDataContractsForKnownTypes(IList knownTypeList)
        { 
            if (knownTypeList == null) return null; 
            DataContractDictionary dataContracts = new DataContractDictionary();
            Dictionary typesChecked = new Dictionary(); 
            for (int i = 0; i < knownTypeList.Count; i++)
            {
                Type knownType = knownTypeList[i];
                if (knownType == null) 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.NullKnownType, "knownTypes")));
 
                DataContract.CheckAndAdd(knownType, typesChecked, ref dataContracts); 
            }
            return dataContracts; 
        }

        protected DataContract ResolveDataContractFromKnownTypes(string typeName, string typeNs, DataContract memberTypeContract)
        { 
            DataContract dataContract = PrimitiveDataContract.GetPrimitiveDataContract(typeName, typeNs);
            if (dataContract == null) 
            { 
                XmlQualifiedName qname = new XmlQualifiedName(typeName, typeNs);
                dataContract = scopedKnownTypes.GetDataContract(qname); 
                if (dataContract == null)
                {
                    dataContract = GetDataContractFromSerializerKnownTypes(qname);
                } 
                if (dataContract == null && memberTypeContract != null
                    && !memberTypeContract.UnderlyingType.IsInterface 
                    && memberTypeContract.StableName == qname) 
                {
                    dataContract = memberTypeContract; 
                }
                if (dataContract == null && rootTypeDataContract != null)
                {
                    if (rootTypeDataContract.StableName == qname) 
                        dataContract = rootTypeDataContract;
                    else 
                    { 
                        CollectionDataContract collectionContract = rootTypeDataContract as CollectionDataContract;
                        while (collectionContract != null) 
                        {
                            DataContract itemContract = GetDataContract(GetSurrogatedType(collectionContract.ItemType));
                            if (itemContract.StableName == qname)
                            { 
                                dataContract = itemContract;
                                break; 
                            } 
                            collectionContract = itemContract as CollectionDataContract;
                        } 
                    }
                }
            }
            return dataContract; 
        }
 
    } 

} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK