public XmlSerializer CreateSerializer(Type type, string defaultNamespace)
 {
     if (type == null)
     {
         throw new ArgumentNullException("type");
     }
     TempAssembly assembly = cache[defaultNamespace, type];
     XmlTypeMapping xmlMapping = null;
     if (assembly == null)
     {
         lock (cache)
         {
             assembly = cache[defaultNamespace, type];
             if (assembly == null)
             {
                 XmlSerializerImplementation implementation;
                 if (TempAssembly.LoadGeneratedAssembly(type, defaultNamespace, out implementation) == null)
                 {
                     xmlMapping = new XmlReflectionImporter(defaultNamespace).ImportTypeMapping(type, null, defaultNamespace);
                     assembly = XmlSerializer.GenerateTempAssembly(xmlMapping, type, defaultNamespace);
                 }
                 else
                 {
                     assembly = new TempAssembly(implementation);
                 }
                 cache.Add(defaultNamespace, type, assembly);
             }
         }
     }
     if (xmlMapping == null)
     {
         xmlMapping = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
     }
     return assembly.Contract.GetSerializer(type);
 }
Example #2
0
 /// <include file='doc\XmlSerializer.uex' path='docs/doc[@for="XmlSerializer.XmlSerializer"]/*' />
 /// <devdoc>
 ///    <para>[To be supplied.]</para>
 /// </devdoc>
 public XmlSerializer(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace) {
     XmlReflectionImporter importer = new XmlReflectionImporter(overrides, defaultNamespace);
     for (int i = 0; i < extraTypes.Length; i++)
         importer.IncludeType(extraTypes[i]);
     tempAssembly = GenerateTempAssembly(importer.ImportTypeMapping(type, root));
     this.events.sender = this;
 }
 // this method must be called before any generated serialization methods are called
 internal void Init(XmlWriter w, XmlSerializerNamespaces namespaces, string encodingStyle, string idBase, TempAssembly tempAssembly) {
     this.w = w;
     this.namespaces = namespaces;
     this.soap12 = (encodingStyle == Soap12.Encoding);
     this.idBase = idBase;
     Init(tempAssembly);
 }
 internal void Init(TempAssembly tempAssembly)
 {
     this.tempAssembly = tempAssembly;
     if ((tempAssembly != null) && tempAssembly.NeedAssembyResolve)
     {
         this.threadCode = Thread.CurrentThread.GetHashCode();
         this.assemblyResolver = new ResolveEventHandler(this.OnAssemblyResolve);
         AppDomain.CurrentDomain.AssemblyResolve += this.assemblyResolver;
     }
 }
 internal void Init(TempAssembly tempAssembly) {
     this.tempAssembly = tempAssembly;
     // only hook the assembly resolver if we have something to help us do the resolution
     if (tempAssembly != null && tempAssembly.NeedAssembyResolve) {
         // we save the threadcode to make sure we don't handle any resolve events for any other threads
         threadCode = Thread.CurrentThread.GetHashCode();
         assemblyResolver = new ResolveEventHandler(OnAssemblyResolve);
         AppDomain.CurrentDomain.AssemblyResolve += assemblyResolver;
     }
 }
 internal void Add(string ns, object o, TempAssembly assembly)
 {
     TempAssemblyCacheKey key = new TempAssemblyCacheKey(ns, o);
     lock (this)
     {
         if (this.cache[key] != assembly)
         {
             Hashtable hashtable = new Hashtable();
             foreach (object obj2 in this.cache.Keys)
             {
                 hashtable.Add(obj2, this.cache[obj2]);
             }
             this.cache = hashtable;
             this.cache[key] = assembly;
         }
     }
 }
Example #7
0
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public static XmlSerializer[] FromMappings(XmlMapping[] mappings, Type type)
        {
            if (mappings == null || mappings.Length == 0) return Array.Empty<XmlSerializer>();

#if NET_NATIVE
            var serializers = new XmlSerializer[mappings.Length];
            for(int i=0;i<mappings.Length;i++)
            {
                serializers[i] = new XmlSerializer();
                serializers[i].rootType = type;
                serializers[i]._mapping = mappings[i];
            }

            return serializers;
#else
            XmlSerializerImplementation contract = null;
            TempAssembly tempAssembly = null;
            {
                if (XmlMapping.IsShallow(mappings))
                {
                    return Array.Empty<XmlSerializer>();
                }
                else
                {
                    if (type == null)
                    {
                        tempAssembly = new TempAssembly(mappings, new Type[] { type }, null, null, null);
                        XmlSerializer[] serializers = new XmlSerializer[mappings.Length];

                        contract = tempAssembly.Contract;

                        for (int i = 0; i < serializers.Length; i++)
                        {
                            serializers[i] = (XmlSerializer)contract.TypedSerializers[mappings[i].Key];
                            serializers[i].SetTempAssembly(tempAssembly, mappings[i]);
                        }

                        return serializers;
                    }
                    else
                    {
                        // Use XmlSerializer cache when the type is not null.
                        return GetSerializersFromCache(mappings, type);
                    }
                }
            }
#endif
        }
Example #8
0
        private void Init(Type type, string defaultNamespace)
        {
            if (type == null)
                throw new ArgumentNullException(nameof(type));

            DefaultNamespace = defaultNamespace;
            rootType = type;

            _mapping = GetKnownMapping(type, defaultNamespace);
            if (_mapping != null)
            {
                _primitiveType = type;
                return;
            }
#if !NET_NATIVE
            _tempAssembly = s_cache[defaultNamespace, type];
            if (_tempAssembly == null)
            {
                lock (s_cache)
                {
                    _tempAssembly = s_cache[defaultNamespace, type];
                    if (_tempAssembly == null)
                    {
                        {
                            // need to reflect and generate new serialization assembly
                            XmlReflectionImporter importer = new XmlReflectionImporter(defaultNamespace);
                            _mapping = importer.ImportTypeMapping(type, null, defaultNamespace);
                            _tempAssembly = GenerateTempAssembly(_mapping, type, defaultNamespace);
                        }
                    }
                    s_cache.Add(defaultNamespace, type, _tempAssembly);
                }
            }
            if (_mapping == null)
            {
                _mapping = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
            }
#else
            XmlSerializerImplementation contract = GetXmlSerializerContractFromGeneratedAssembly();

            if (contract != null)
            {
                this.innerSerializer = contract.GetSerializer(type);
            }
            else if (ReflectionMethodEnabled)
            {
                var importer = new XmlReflectionImporter(defaultNamespace);
                _mapping = importer.ImportTypeMapping(type, null, defaultNamespace);

                if (_mapping == null)
                {
                    _mapping = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
                }
            }
#endif
        }
 internal void Add(string ns, object o, TempAssembly assembly) {
     TempAssemblyCacheKey key = new TempAssemblyCacheKey(ns, o);
     lock(this) {
         if (cache[key] == assembly) return;
         Hashtable clone = new Hashtable();
         foreach (object k in cache.Keys) {
             clone.Add(k, cache[k]);
         }
         cache = clone;
         cache[key] = assembly;
     }
 }
Example #10
0
        /// <include file='doc\XmlSerializer.uex' path='docs/doc[@for="XmlSerializer.XmlSerializer7"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        internal XmlSerializer(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace, object location, object evidence)
        {
#if NET_NATIVE
            throw new PlatformNotSupportedException();
#else
            if (type == null)
                throw new ArgumentNullException("type");

            XmlReflectionImporter importer = new XmlReflectionImporter(overrides, defaultNamespace);
            if (extraTypes != null)
            {
                for (int i = 0; i < extraTypes.Length; i++)
                    importer.IncludeType(extraTypes[i]);
            }
            _mapping = importer.ImportTypeMapping(type, root, defaultNamespace);
            _tempAssembly = GenerateTempAssembly(_mapping, type, defaultNamespace);
#endif
        }
Example #11
0
        /// <include file='doc\XmlSerializer.uex' path='docs/doc[@for="XmlSerializer.FromMappings1"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public static XmlSerializer[] FromMappings(XmlMapping[] mappings, Type type)
        {
            if (mappings == null || mappings.Length == 0) return Array.Empty<XmlSerializer>();
            XmlSerializerImplementation contract = null;
            Assembly assembly = type == null ? null : TempAssembly.LoadGeneratedAssembly(type, null, out contract);
            TempAssembly tempAssembly = null;
            if (assembly == null)
            {
                if (XmlMapping.IsShallow(mappings))
                {
                    return Array.Empty<XmlSerializer>();
                }
                else
                {
                    if (type == null)
                    {
                        tempAssembly = new TempAssembly(mappings, new Type[] { type }, null, null, null);
                        XmlSerializer[] serializers = new XmlSerializer[mappings.Length];

                        contract = tempAssembly.Contract;

                        for (int i = 0; i < serializers.Length; i++)
                        {
                            serializers[i] = (XmlSerializer)contract.TypedSerializers[mappings[i].Key];
                            serializers[i].SetTempAssembly(tempAssembly, mappings[i]);
                        }

                        return serializers;
                    }
                    else
                    {
                        // Use XmlSerializer cache when the type is not null.
                        return GetSerializersFromCache(mappings, type);
                    }
                }
            }
            else
            {
                XmlSerializer[] serializers = new XmlSerializer[mappings.Length];
                for (int i = 0; i < serializers.Length; i++)
                    serializers[i] = (XmlSerializer)contract.TypedSerializers[mappings[i].Key];
                return serializers;
            }
        }
Example #12
0
 /// <include file='doc\XmlSerializer.uex' path='docs/doc[@for="XmlSerializer.XmlSerializer5"]/*' />
 /// <devdoc>
 ///    <para>[To be supplied.]</para>
 /// </devdoc>
 public XmlSerializer(XmlTypeMapping xmlTypeMapping)
 {
     _tempAssembly = GenerateTempAssembly(xmlTypeMapping);
     _mapping      = xmlTypeMapping;
 }
 // this method must be called before any generated deserialization methods are called
 internal void Init(XmlReader r, XmlDeserializationEvents events, string encodingStyle, TempAssembly tempAssembly)
 {
     _events = events;
     Init(tempAssembly);
     Init(r, encodingStyle);
 }
 internal void SetTempAssembly(TempAssembly tempAssembly, XmlMapping mapping)
 {
     this.tempAssembly    = tempAssembly;
     this.mapping         = mapping;
     this.typedSerializer = true;
 }
Example #15
0
 internal void SetTempAssembly(TempAssembly tempAssembly, XmlMapping mapping)
 {
     _tempAssembly    = tempAssembly;
     _mapping         = mapping;
     _typedSerializer = true;
 }
Example #16
0
        public static XmlSerializer[] FromMappings(XmlMapping[] mappings, Type type)
        {
            if (mappings == null || mappings.Length == 0)
            {
                return(Array.Empty <XmlSerializer>());
            }
            bool anySoapMapping = false;

            foreach (var mapping in mappings)
            {
                if (mapping.IsSoap)
                {
                    anySoapMapping = true;
                }
            }

            if ((anySoapMapping && ReflectionMethodEnabled) || Mode == SerializationMode.ReflectionOnly)
            {
                XmlSerializer[] serializers = GetReflectionBasedSerializers(mappings, type);
                return(serializers);
            }

            XmlSerializerImplementation contract = null;
            Assembly     assembly     = type == null ? null : TempAssembly.LoadGeneratedAssembly(type, null, out contract);
            TempAssembly tempAssembly = null;

            if (assembly == null)
            {
                if (Mode == SerializationMode.PreGenOnly)
                {
                    AssemblyName name           = type.Assembly.GetName();
                    string       serializerName = Compiler.GetTempAssemblyName(name, null);
                    throw new FileLoadException(SR.Format(SR.FailLoadAssemblyUnderPregenMode, serializerName));
                }

                if (XmlMapping.IsShallow(mappings))
                {
                    return(Array.Empty <XmlSerializer>());
                }
                else
                {
                    if (type == null)
                    {
                        tempAssembly = new TempAssembly(mappings, new Type[] { type }, null, null);
                        XmlSerializer[] serializers = new XmlSerializer[mappings.Length];

                        contract = tempAssembly.Contract;

                        for (int i = 0; i < serializers.Length; i++)
                        {
                            serializers[i] = (XmlSerializer)contract.TypedSerializers[mappings[i].Key];
                            serializers[i].SetTempAssembly(tempAssembly, mappings[i]);
                        }

                        return(serializers);
                    }
                    else
                    {
                        // Use XmlSerializer cache when the type is not null.
                        return(GetSerializersFromCache(mappings, type));
                    }
                }
            }
            else
            {
                XmlSerializer[] serializers = new XmlSerializer[mappings.Length];
                for (int i = 0; i < serializers.Length; i++)
                {
                    serializers[i] = (XmlSerializer)contract.TypedSerializers[mappings[i].Key];
                }
                return(serializers);
            }
        }
 internal void Init(TempAssembly tempAssembly)
 {
 }
Example #18
0
        public XmlSerializer(Type type, string defaultNamespace)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            DefaultNamespace = defaultNamespace;
            _rootType        = type;

            _mapping = GetKnownMapping(type, defaultNamespace);
            if (_mapping != null)
            {
                _primitiveType = type;
                return;
            }
#if !FEATURE_SERIALIZATION_UAPAOT
            _tempAssembly = s_cache[defaultNamespace, type];
            if (_tempAssembly == null)
            {
                lock (s_cache)
                {
                    _tempAssembly = s_cache[defaultNamespace, type];
                    if (_tempAssembly == null)
                    {
                        {
                            XmlSerializerImplementation contract = null;
                            Assembly assembly = TempAssembly.LoadGeneratedAssembly(type, defaultNamespace, out contract);
                            if (assembly == null)
                            {
                                if (Mode == SerializationMode.PreGenOnly)
                                {
                                    AssemblyName name           = type.Assembly.GetName();
                                    var          serializerName = Compiler.GetTempAssemblyName(name, defaultNamespace);
                                    throw new FileLoadException(SR.Format(SR.FailLoadAssemblyUnderPregenMode, serializerName));
                                }

                                // need to reflect and generate new serialization assembly
                                XmlReflectionImporter importer = new XmlReflectionImporter(defaultNamespace);
                                _mapping      = importer.ImportTypeMapping(type, null, defaultNamespace);
                                _tempAssembly = GenerateTempAssembly(_mapping, type, defaultNamespace);
                            }
                            else
                            {
                                // we found the pre-generated assembly, now make sure that the assembly has the right serializer
                                // try to avoid the reflection step, need to get ElementName, namespace and the Key form the type
                                _mapping      = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
                                _tempAssembly = new TempAssembly(new XmlMapping[] { _mapping }, assembly, contract);
                            }
                        }
                    }
                    s_cache.Add(defaultNamespace, type, _tempAssembly);
                }
            }
            if (_mapping == null)
            {
                _mapping = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
            }
#else
            XmlSerializerImplementation contract = GetXmlSerializerContractFromGeneratedAssembly();

            if (contract != null)
            {
                this.innerSerializer = contract.GetSerializer(type);
            }
#endif
        }
Example #19
0
 internal void SetTempAssembly(TempAssembly tempAssembly, XmlMapping mapping)
 {
     _tempAssembly = tempAssembly;
     _mapping = mapping;
     _typedSerializer = true;
 }
        /// <include file='doc\XmlSerializer.uex' path='docs/doc[@for="XmlSerializer.XmlSerializer7"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        internal XmlSerializer(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace, string location, Evidence evidence)
        {
            if (type == null)
                throw new ArgumentNullException(nameof(type));

            DefaultNamespace = defaultNamespace;
            rootType = type;
            XmlReflectionImporter importer = new XmlReflectionImporter(overrides, defaultNamespace);
            if (extraTypes != null)
            {
                for (int i = 0; i < extraTypes.Length; i++)
                    importer.IncludeType(extraTypes[i]);
            }
            _mapping = importer.ImportTypeMapping(type, root, defaultNamespace);
            if (location != null || evidence != null)
            {
                DemandForUserLocationOrEvidence();
            }

#if !NET_NATIVE
            _tempAssembly = GenerateTempAssembly(_mapping, type, defaultNamespace, location, evidence);
#endif
        }
 internal void Init(TempAssembly tempAssembly)
 {
 }
 internal void SetTempAssembly(TempAssembly tempAssembly, XmlMapping mapping) {
     this.tempAssembly = tempAssembly;
     this.mapping = mapping;
     this.typedSerializer = true;
 }
Example #23
0
 /// <include file='doc\XmlSerializer.uex' path='docs/doc[@for="XmlSerializer.XmlSerializer5"]/*' />
 /// <devdoc>
 ///    <para>[To be supplied.]</para>
 /// </devdoc>
 public XmlSerializer(XmlTypeMapping xmlTypeMapping) {
     tempAssembly = GenerateTempAssembly(xmlTypeMapping);
     this.events.sender = this;
 }
Example #24
0
 /// <include file='doc\XmlSerializer.uex' path='docs/doc[@for="XmlSerializer.XmlSerializer5"]/*' />
 /// <devdoc>
 ///    <para>[To be supplied.]</para>
 /// </devdoc>
 public XmlSerializer(XmlTypeMapping xmlTypeMapping)
 {
     _tempAssembly = GenerateTempAssembly(xmlTypeMapping);
     _mapping = xmlTypeMapping;
 }
Example #25
0
 /// <include file='doc\XmlSerializer.uex' path='docs/doc[@for="XmlSerializer.XmlSerializer6"]/*' />
 /// <devdoc>
 ///    <para>[To be supplied.]</para>
 /// </devdoc>
 public XmlSerializer(Type type) {
     this.events.sender = this;
     tempAssembly = cache[type];
     if (tempAssembly == null) {
         lock (cache) {
             tempAssembly = cache[type];
             if (tempAssembly == null) {
                 XmlReflectionImporter importer = new XmlReflectionImporter();
                 tempAssembly = GenerateTempAssembly(importer.ImportTypeMapping(type));
                 cache.Add(type, tempAssembly);
             }
         }
     }
 }
Example #26
0
        private static XmlSerializer[] GetSerializersFromCache(XmlMapping[] mappings, Type type)
        {
            XmlSerializer[] serializers = new XmlSerializer[mappings.Length];

            InternalHashtable typedMappingTable = null;
            lock (s_xmlSerializerTable)
            {
                typedMappingTable = s_xmlSerializerTable[type] as InternalHashtable;
                if (typedMappingTable == null)
                {
                    typedMappingTable = new InternalHashtable();
                    s_xmlSerializerTable[type] = typedMappingTable;
                }
            }

            lock (typedMappingTable)
            {
                InternalHashtable pendingKeys = new InternalHashtable();
                for (int i = 0; i < mappings.Length; i++)
                {
                    XmlSerializerMappingKey mappingKey = new XmlSerializerMappingKey(mappings[i]);
                    serializers[i] = typedMappingTable[mappingKey] as XmlSerializer;
                    if (serializers[i] == null)
                    {
                        pendingKeys.Add(mappingKey, i);
                    }
                }

                if (pendingKeys.Count > 0)
                {
                    XmlMapping[] pendingMappings = new XmlMapping[pendingKeys.Count];
                    int index = 0;
                    foreach (XmlSerializerMappingKey mappingKey in pendingKeys.Keys)
                    {
                        pendingMappings[index++] = mappingKey.Mapping;
                    }

                    TempAssembly tempAssembly = new TempAssembly(pendingMappings, new Type[] { type }, null, null, null);
                    XmlSerializerImplementation contract = tempAssembly.Contract;

                    foreach (XmlSerializerMappingKey mappingKey in pendingKeys.Keys)
                    {
                        index = (int)pendingKeys[mappingKey];
                        serializers[index] = (XmlSerializer)contract.TypedSerializers[mappingKey.Mapping.Key];
                        serializers[index].SetTempAssembly(tempAssembly, mappingKey.Mapping);

                        typedMappingTable[mappingKey] = serializers[index];
                    }
                }
            }

            return serializers;
        }
Example #27
0
 /// <include file='doc\XmlSerializer.uex' path='docs/doc[@for="XmlSerializer.FromMappings"]/*' />
 /// <devdoc>
 ///    <para>[To be supplied.]</para>
 /// </devdoc>
 public static XmlSerializer[] FromMappings(XmlMapping[] mappings) {
     if (mappings.Length == 0) return new XmlSerializer[0];
     TempAssembly tempAssembly = new TempAssembly(mappings);
     XmlSerializer[] serializers = new XmlSerializer[mappings.Length];
     for (int i = 0; i < serializers.Length; i++)
         serializers[i] = new XmlSerializer(tempAssembly, i);
     return serializers;
 }
Example #28
0
 /// <include file='doc\XmlSerializerFactory.uex' path='docs/doc[@for="XmlSerializerFactory.CreateSerializer1"]/*' />
 /// <devdoc>
 ///    <para>[To be supplied.]</para>
 /// </devdoc>
 public XmlSerializer CreateSerializer(Type type, string defaultNamespace)
 {
     if (type == null)
         throw new ArgumentNullException(nameof(type));
     TempAssembly tempAssembly = s_cache[defaultNamespace, type];
     XmlTypeMapping mapping = null;
     if (tempAssembly == null)
     {
         lock (s_cache)
         {
             tempAssembly = s_cache[defaultNamespace, type];
             if (tempAssembly == null)
             {
                 XmlSerializerImplementation contract;
                 Assembly assembly = TempAssembly.LoadGeneratedAssembly(type, defaultNamespace, out contract);
                 if (assembly == null)
                 {
                     // need to reflect and generate new serialization assembly
                     XmlReflectionImporter importer = new XmlReflectionImporter(defaultNamespace);
                     mapping = importer.ImportTypeMapping(type, null, defaultNamespace);
                     tempAssembly = XmlSerializer.GenerateTempAssembly(mapping, type, defaultNamespace);
                 }
                 else
                 {
                     tempAssembly = new TempAssembly(contract);
                 }
                 s_cache.Add(defaultNamespace, type, tempAssembly);
             }
         }
     }
     if (mapping == null)
     {
         mapping = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
     }
     return (XmlSerializer)tempAssembly.Contract.GetSerializer(type);
 }
Example #29
0
 XmlSerializer(TempAssembly tempAssembly, int methodIndex) {
     this.tempAssembly = tempAssembly;
     this.methodIndex = methodIndex;
     this.events.sender = this;
 }
Example #30
0
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public XmlSerializer(XmlTypeMapping xmlTypeMapping)
        {
            if (xmlTypeMapping == null)
                throw new ArgumentNullException(nameof(xmlTypeMapping));

#if !NET_NATIVE
            _tempAssembly = GenerateTempAssembly(xmlTypeMapping);
#endif
            _mapping = xmlTypeMapping;
        }
Example #31
0
 /// <include file='doc\XmlSerializer.uex' path='docs/doc[@for="XmlSerializer.XmlSerializer5"]/*' />
 /// <devdoc>
 ///    <para>[To be supplied.]</para>
 /// </devdoc>
 public XmlSerializer(XmlTypeMapping xmlTypeMapping)
 {
     tempAssembly       = GenerateTempAssembly(xmlTypeMapping);
     this.events.sender = this;
 }
Example #32
0
        private void Init(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace, object location, object evidence)
        {
            if (type == null)
                throw new ArgumentNullException(nameof(type));

            DefaultNamespace = defaultNamespace;
            rootType = type;

            XmlReflectionImporter importer = new XmlReflectionImporter(overrides, defaultNamespace);
            if (extraTypes != null)
            {
                for (int i = 0; i < extraTypes.Length; i++)
                    importer.IncludeType(extraTypes[i]);
            }
            _mapping = importer.ImportTypeMapping(type, root, defaultNamespace);
#if !NET_NATIVE
            _tempAssembly = GenerateTempAssembly(_mapping, type, defaultNamespace);
#endif
        }
Example #33
0
 XmlSerializer(TempAssembly tempAssembly, int methodIndex)
 {
     this.tempAssembly  = tempAssembly;
     this.methodIndex   = methodIndex;
     this.events.sender = this;
 }
Example #34
0
        private static XmlSerializer[] GetSerializersFromCache(XmlMapping[] mappings, Type type)
        {
            XmlSerializer[] serializers = new XmlSerializer[mappings.Length];

            Dictionary<XmlSerializerMappingKey, XmlSerializer> typedMappingTable = null;
            lock (s_xmlSerializerTable)
            {
                if (!s_xmlSerializerTable.TryGetValue(type, out typedMappingTable))
                {
                    typedMappingTable = new Dictionary<XmlSerializerMappingKey, XmlSerializer>();
                    s_xmlSerializerTable[type] = typedMappingTable;
                }
            }

            lock (typedMappingTable)
            {
                var pendingKeys = new Dictionary<XmlSerializerMappingKey, int>();
                for (int i = 0; i < mappings.Length; i++)
                {
                    XmlSerializerMappingKey mappingKey = new XmlSerializerMappingKey(mappings[i]);
                    if (!typedMappingTable.TryGetValue(mappingKey, out serializers[i]))
                    {
                        pendingKeys.Add(mappingKey, i);
                    }
                }

                if (pendingKeys.Count > 0)
                {
                    XmlMapping[] pendingMappings = new XmlMapping[pendingKeys.Count];
                    int index = 0;
                    foreach (XmlSerializerMappingKey mappingKey in pendingKeys.Keys)
                    {
                        pendingMappings[index++] = mappingKey.Mapping;
                    }

                    TempAssembly tempAssembly = new TempAssembly(pendingMappings, new Type[] { type }, null, null, null);
                    XmlSerializerImplementation contract = tempAssembly.Contract;

                    foreach (XmlSerializerMappingKey mappingKey in pendingKeys.Keys)
                    {
                        index = pendingKeys[mappingKey];
                        serializers[index] = (XmlSerializer)contract.TypedSerializers[mappingKey.Mapping.Key];
                        serializers[index].SetTempAssembly(tempAssembly, mappingKey.Mapping);

                        typedMappingTable[mappingKey] = serializers[index];
                    }
                }
            }

            return serializers;
        }
 public XmlSerializer(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace, string location, Evidence evidence) {
     if (type == null)
         throw new ArgumentNullException("type");
     XmlReflectionImporter importer = new XmlReflectionImporter(overrides, defaultNamespace);
     for (int i = 0; i < extraTypes.Length; i++)
         importer.IncludeType(extraTypes[i]);
     this.mapping = importer.ImportTypeMapping(type, root, defaultNamespace);
     tempAssembly = GenerateTempAssembly(this.mapping, type, defaultNamespace, location, evidence);
 }
        // this method must be called before any generated deserialization methods are called
        internal void Init(XmlReader r, XmlDeserializationEvents events, string encodingStyle, TempAssembly tempAssembly) {
            this.events = events;
            if (checkDeserializeAdvances)
            {
                this.countingReader = new XmlCountingReader(r);
                this.r = this.countingReader;
            }
            else 
                this.r = r;
            this.d = null;
            this.soap12 = (encodingStyle == Soap12.Encoding);
            Init(tempAssembly);

            schemaNsID = r.NameTable.Add(XmlSchema.Namespace);
            schemaNs2000ID = r.NameTable.Add("http://www.w3.org/2000/10/XMLSchema");
            schemaNs1999ID = r.NameTable.Add("http://www.w3.org/1999/XMLSchema");
            schemaNonXsdTypesNsID = r.NameTable.Add(UrtTypes.Namespace);
            instanceNsID = r.NameTable.Add(XmlSchema.InstanceNamespace);
            instanceNs2000ID = r.NameTable.Add("http://www.w3.org/2000/10/XMLSchema-instance");
            instanceNs1999ID = r.NameTable.Add("http://www.w3.org/1999/XMLSchema-instance");
            soapNsID = r.NameTable.Add(Soap.Encoding);
            soap12NsID = r.NameTable.Add(Soap12.Encoding);
            schemaID = r.NameTable.Add("schema");
            wsdlNsID = r.NameTable.Add(Wsdl.Namespace);
            wsdlArrayTypeID = r.NameTable.Add(Wsdl.ArrayType);
            nullID = r.NameTable.Add("null");
            nilID = r.NameTable.Add("nil");
            typeID = r.NameTable.Add("type");
            arrayTypeID = r.NameTable.Add("arrayType");
            itemTypeID = r.NameTable.Add("itemType");
            arraySizeID = r.NameTable.Add("arraySize");
            arrayID = r.NameTable.Add("Array");
            urTypeID = r.NameTable.Add(Soap.UrType);
            InitIDs();
        }
 public static XmlSerializer[] FromMappings(XmlMapping[] mappings, Evidence evidence) {
     if (mappings == null || mappings.Length == 0) return new XmlSerializer[0];
     if (XmlMapping.IsShallow(mappings)) {
         return new XmlSerializer[0];
     }
     TempAssembly tempAssembly = new TempAssembly(mappings, new Type[0], null, null, evidence);
     XmlSerializerImplementation contract = tempAssembly.Contract;
     XmlSerializer[] serializers = new XmlSerializer[mappings.Length];
     for (int i = 0; i < serializers.Length; i++) {
         serializers[i] = (XmlSerializer)contract.TypedSerializers[mappings[i].Key];
     }
     return serializers;
 }
 /// <include file='doc\XmlSerializer.uex' path='docs/doc[@for="XmlSerializer.XmlSerializer1"]/*' />
 /// <devdoc>
 ///    <para>[To be supplied.]</para>
 /// </devdoc>
 public XmlSerializer(Type type, string defaultNamespace) {
     if (type == null)
         throw new ArgumentNullException("type");
     this.mapping = GetKnownMapping(type, defaultNamespace);
     if (this.mapping != null) {
         this.primitiveType = type;
         return;
     }
     tempAssembly = cache[defaultNamespace, type];
     if (tempAssembly == null) {
         lock (cache) {
             tempAssembly = cache[defaultNamespace, type];
             if (tempAssembly == null) {
                 XmlSerializerImplementation contract;
                 Assembly assembly = TempAssembly.LoadGeneratedAssembly(type, defaultNamespace, out contract);
                 if (assembly == null) {
                     // need to reflect and generate new serialization assembly
                     XmlReflectionImporter importer = new XmlReflectionImporter(defaultNamespace);
                     this.mapping = importer.ImportTypeMapping(type, null, defaultNamespace);
                     tempAssembly = GenerateTempAssembly(this.mapping, type, defaultNamespace);
                 }
                 else {
                     // we found the pre-generated assembly, now make sure that the assembly has the right serializer
                     // try to avoid the reflection step, need to get ElementName, namespace and the Key form the type
                     this.mapping = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
                     tempAssembly = new TempAssembly(new XmlMapping[] { this.mapping }, assembly, contract);
                 }
             }
             cache.Add(defaultNamespace, type, tempAssembly);
         }
     }
     if (mapping == null) {
         mapping = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
     }
 }
Example #39
0
        /// <include file='doc\XmlSerializerFactory.uex' path='docs/doc[@for="XmlSerializerFactory.CreateSerializer5"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public XmlSerializer CreateSerializer(XmlTypeMapping xmlTypeMapping)
        {
            TempAssembly tempAssembly = XmlSerializer.GenerateTempAssembly(xmlTypeMapping);

            return((XmlSerializer)tempAssembly.Contract.TypedSerializers[xmlTypeMapping.Key]);
        }