LoadGeneratedAssembly() static private method

static private LoadGeneratedAssembly ( Type type, string defaultNamespace, XmlSerializerImplementation &contract ) : Assembly
type System.Type
defaultNamespace string
contract XmlSerializerImplementation
return System.Reflection.Assembly
Esempio n. 1
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;
            }
            _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);
            }
        }
Esempio n. 2
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 !] !;
        public static XmlSerializer[] FromMappings(XmlMapping[] mappings, Type type)
        {
            if (mappings == null || mappings.Length == 0)
            {
                return(new XmlSerializer[0]);
            }
            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(new XmlSerializer[0]);
                }
                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);
            }
        }
 /// <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);
     }
 }
 public XmlSerializer(Type type, string defaultNamespace)
 {
     this.events = new XmlDeserializationEvents();
     if (type == null)
     {
         throw new ArgumentNullException("type");
     }
     this.mapping = GetKnownMapping(type, defaultNamespace);
     if (this.mapping != null)
     {
         this.primitiveType = type;
     }
     else
     {
         this.tempAssembly = cache[defaultNamespace, type];
         if (this.tempAssembly == null)
         {
             lock (cache)
             {
                 this.tempAssembly = cache[defaultNamespace, type];
                 if (this.tempAssembly == null)
                 {
                     XmlSerializerImplementation implementation;
                     Assembly assembly = TempAssembly.LoadGeneratedAssembly(type, defaultNamespace, out implementation);
                     if (assembly == null)
                     {
                         this.mapping      = new XmlReflectionImporter(defaultNamespace).ImportTypeMapping(type, null, defaultNamespace);
                         this.tempAssembly = GenerateTempAssembly(this.mapping, type, defaultNamespace);
                     }
                     else
                     {
                         this.mapping      = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
                         this.tempAssembly = new TempAssembly(new XmlMapping[] { this.mapping }, assembly, implementation);
                     }
                 }
                 cache.Add(defaultNamespace, type, this.tempAssembly);
             }
         }
         if (this.mapping == null)
         {
             this.mapping = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
         }
     }
 }
Esempio n. 6
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));
        }
        public static XmlSerializer[] FromMappings(XmlMapping[] mappings, Type type)
        {
            if ((mappings == null) || (mappings.Length == 0))
            {
                return(new XmlSerializer[0]);
            }
            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(new XmlSerializer[0]);
                }
                if (type != null)
                {
                    return(GetSerializersFromCache(mappings, type));
                }
                tempAssembly = new TempAssembly(mappings, new Type[] { type }, null, null, null);
                XmlSerializer[] serializerArray = new XmlSerializer[mappings.Length];
                contract = tempAssembly.Contract;
                for (int j = 0; j < serializerArray.Length; j++)
                {
                    serializerArray[j] = (XmlSerializer)contract.TypedSerializers[mappings[j].Key];
                    serializerArray[j].SetTempAssembly(tempAssembly, mappings[j]);
                }
                return(serializerArray);
            }
            XmlSerializer[] serializerArray2 = new XmlSerializer[mappings.Length];
            for (int i = 0; i < serializerArray2.Length; i++)
            {
                serializerArray2[i] = (XmlSerializer)contract.TypedSerializers[mappings[i].Key];
            }
            return(serializerArray2);
        }
        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));
        }