IsShallow() static private method

static private IsShallow ( XmlMapping mappings ) : bool
mappings XmlMapping
return bool
        public static Assembly GenerateSerializer(Type[] types, XmlMapping[] mappings, CompilerParameters parameters)
        {
            if ((types == null) || (types.Length == 0))
            {
                return(null);
            }
            if (mappings == null)
            {
                throw new ArgumentNullException("mappings");
            }
            if (XmlMapping.IsShallow(mappings))
            {
                throw new InvalidOperationException(Res.GetString("XmlMelformMapping"));
            }
            Assembly assembly = null;

            for (int i = 0; i < types.Length; i++)
            {
                Type type = types[i];
                if (DynamicAssemblies.IsTypeDynamic(type))
                {
                    throw new InvalidOperationException(Res.GetString("XmlPregenTypeDynamic", new object[] { type.FullName }));
                }
                if (assembly == null)
                {
                    assembly = type.Assembly;
                }
                else if (type.Assembly != assembly)
                {
                    throw new ArgumentException(Res.GetString("XmlPregenOrphanType", new object[] { type.FullName, assembly.Location }), "types");
                }
            }
            return(TempAssembly.GenerateAssembly(mappings, types, null, null, XmlSerializerCompilerParameters.Create(parameters, true), assembly, new Hashtable()));
        }
Beispiel #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);
            }
        }
        internal static bool GenerateSerializer(Type[] types, XmlMapping[] mappings, Stream stream)
        {
            if (types == null || types.Length == 0)
            {
                return(false);
            }

            if (mappings == null)
            {
                throw new ArgumentNullException(nameof(mappings));
            }

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (XmlMapping.IsShallow(mappings))
            {
                throw new InvalidOperationException(SR.XmlMelformMapping);
            }

            Assembly assembly = null;

            for (int i = 0; i < types.Length; i++)
            {
                Type type = types[i];
                if (DynamicAssemblies.IsTypeDynamic(type))
                {
                    throw new InvalidOperationException(SR.Format(SR.XmlPregenTypeDynamic, type.FullName));
                }

                if (assembly == null)
                {
                    assembly = type.Assembly;
                }
                else if (type.Assembly != assembly)
                {
                    throw new ArgumentException(SR.Format(SR.XmlPregenOrphanType, type.FullName, assembly.Location), nameof(types));
                }
            }

            return(TempAssembly.GenerateSerializerToStream(mappings, types, null, assembly, new Hashtable(), stream));
        }
        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);
        }
        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);
        }