UnwrappedTypesXmlSerializerManager.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx35 / System.ServiceModel.Web / System / ServiceModel / Dispatcher / UnwrappedTypesXmlSerializerManager.cs / 1305376 / UnwrappedTypesXmlSerializerManager.cs

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

namespace System.ServiceModel.Dispatcher 
{
    using System; 
    using System.Collections.Generic; 
    using System.Runtime;
    using System.Runtime.Serialization; 
    using System.ServiceModel;
    using System.Xml;
    using System.Xml.Serialization;
    using DiagnosticUtility = System.ServiceModel.DiagnosticUtility; 

    class UnwrappedTypesXmlSerializerManager 
    { 
        Dictionary allTypes;
        XmlReflectionImporter importer; 
        Dictionary> operationTypes;
        bool serializersCreated;
        Dictionary serializersMap;
        Object thisLock; 

        public UnwrappedTypesXmlSerializerManager() 
        { 
            this.allTypes = new Dictionary();
            this.serializersMap = new Dictionary(); 
            this.operationTypes = new Dictionary>();
            importer = new XmlReflectionImporter();
            this.thisLock = new Object();
        } 

        public TypeSerializerPair[] GetOperationSerializers(Object key) 
        { 
            if (key == null)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("key");
            }
            lock (thisLock)
            { 
                if (!this.serializersCreated)
                { 
                    BuildSerializers(); 
                    this.serializersCreated = true;
                } 
                List serializers = new List();
                IList operationTypes = this.operationTypes[key];
                for (int i = 0; i < operationTypes.Count; ++i)
                { 
                    TypeSerializerPair pair = new TypeSerializerPair();
                    pair.Type = operationTypes[i]; 
                    pair.Serializer = new XmlSerializerXmlObjectSerializer(serializersMap[operationTypes[i]]); 
                    serializers.Add(pair);
                } 
                return serializers.ToArray();
            }
        }
 
        public void RegisterType(Object key, IList types)
        { 
            if (key == null) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("key"); 
            }
            if (types == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("types"); 
            }
            lock (thisLock) 
            { 
                if (this.serializersCreated)
                { 
                    Fx.Assert("An xml serializer type was added after the serializers were created");
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.XmlSerializersCreatedBeforeRegistration)));
                }
                for (int i = 0; i < types.Count; ++i) 
                {
                    if (!allTypes.ContainsKey(types[i])) 
                    { 
                        allTypes.Add(types[i], importer.ImportTypeMapping(types[i]));
                    } 
                }
                operationTypes.Add(key, types);
            }
        } 

        void BuildSerializers() 
        { 
            List types = new List();
            List mappings = new List(); 
            foreach (Type type in allTypes.Keys)
            {
                XmlTypeMapping mapping = allTypes[type];
                types.Add(type); 
                mappings.Add(mapping);
            } 
            XmlSerializer[] serializers = XmlSerializer.FromMappings(mappings.ToArray()); 
            for (int i = 0; i < types.Count; ++i)
            { 
                serializersMap.Add(types[i], serializers[i]);
            }
        }
 
        public struct TypeSerializerPair
        { 
            public XmlObjectSerializer Serializer; 
            public Type Type;
        } 

        class XmlSerializerXmlObjectSerializer : XmlObjectSerializer
        {
            XmlSerializer serializer; 

            public XmlSerializerXmlObjectSerializer(XmlSerializer serializer) 
            { 
                if (serializer == null)
                { 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serializer");
                }
                this.serializer = serializer;
            } 

            public override bool IsStartObject(XmlDictionaryReader reader) 
            { 
                return this.serializer.CanDeserialize(reader);
            } 

            public override object ReadObject(XmlDictionaryReader reader, bool verifyObjectName)
            {
                return this.serializer.Deserialize(reader); 
            }
 
            public override void WriteEndObject(XmlDictionaryWriter writer) 
            {
                Fx.Assert("This method should never get hit"); 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
            }

            public override void WriteObject(XmlDictionaryWriter writer, object graph) 
            {
                this.serializer.Serialize(writer, graph); 
            } 

            public override void WriteObjectContent(XmlDictionaryWriter writer, object graph) 
            {
                Fx.Assert("This method should never get hit");
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
            } 

            public override void WriteStartObject(XmlDictionaryWriter writer, object graph) 
            { 
                Fx.Assert("This method should never get hit");
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException()); 
            }
        }
    }
} 


// File provided for Reference Use Only by Microsoft Corporation (c) 2007.


                        

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