Exemplo n.º 1
0
        /// <include file='doc\SoapReflectionImporter.uex' path='docs/doc[@for="XmlReflectionImporter.ImportTypeMapping1"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public XmlTypeMapping ImportTypeMapping(Type type, string defaultNamespace)
        {
            ElementAccessor element = new ElementAccessor();

            element.IsSoap    = true;
            element.Mapping   = ImportTypeMapping(_modelScope.GetTypeModel(type), new RecursionLimiter());
            element.Name      = element.Mapping.DefaultElementName;
            element.Namespace = element.Mapping.Namespace == null ? defaultNamespace : element.Mapping.Namespace;
            element.Form      = XmlSchemaForm.Qualified;
            XmlTypeMapping xmlMapping = new XmlTypeMapping(_typeScope, element);

            xmlMapping.SetKeyInternal(XmlMapping.GenerateKey(type, null, defaultNamespace));
            xmlMapping.IsSoap             = true;
            xmlMapping.GenerateSerializer = true;
            return(xmlMapping);
        }
Exemplo n.º 2
0
 /// <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");
     }
     _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)
                 {
                     // 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);
     }
 }
Exemplo n.º 3
0
        private static XmlTypeMapping GetKnownMapping(Type type, string ns)
        {
            if (ns != null && ns != string.Empty)
            {
                return(null);
            }
            TypeDesc typeDesc = (TypeDesc)TypeScope.PrimtiveTypes[type];

            if (typeDesc == null)
            {
                return(null);
            }
            ElementAccessor element = new ElementAccessor();

            element.Name = typeDesc.DataType.Name;
            XmlTypeMapping mapping = new XmlTypeMapping(null, element);

            mapping.SetKeyInternal(XmlMapping.GenerateKey(type, null, null));
            return(mapping);
        }
Exemplo n.º 4
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, string location)
        {
            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);
            if (location != null)
            {
                DemandForUserLocationOrEvidence();
            }
            _tempAssembly = GenerateTempAssembly(_mapping, type, defaultNamespace, location);
        }
Exemplo n.º 5
0
        internal void InvokeWriter(XmlMapping mapping, XmlWriter xmlWriter, object o, XmlSerializerNamespaces namespaces, string encodingStyle, string id)
        {
            XmlSerializationWriter writer = null;

            try
            {
                encodingStyle = ValidateEncodingStyle(encodingStyle, mapping.Key);
                writer        = Contract.Writer;
                writer.Init(xmlWriter, namespaces, encodingStyle, id, this);
                if (_methods[mapping.Key].writeMethod == null)
                {
                    if (_writerMethods == null)
                    {
                        _writerMethods = Contract.WriteMethods;
                    }
                    string methodName = (string)_writerMethods[mapping.Key];
                    if (methodName == null)
                    {
                        throw new InvalidOperationException(string.Format(ResXml.XmlNotSerializable, mapping.Accessor.Name));
                    }
                    _methods[mapping.Key].writeMethod = GetMethodFromType(writer.GetType(), methodName, _pregeneratedAssmbly ? _assembly : null);
                }
                _methods[mapping.Key].writeMethod.Invoke(writer, new object[] { o });
            }
            catch (SecurityException e)
            {
                throw new InvalidOperationException(ResXml.XmlNoPartialTrust, e);
            }
            finally
            {
                if (writer != null)
                {
                    writer.Dispose();
                }
            }
        }
Exemplo n.º 6
0
        internal object InvokeReader(XmlMapping mapping, XmlReader xmlReader, XmlDeserializationEvents events, string encodingStyle)
        {
            XmlSerializationReader reader = null;

            try
            {
                encodingStyle = ValidateEncodingStyle(encodingStyle, mapping.Key);
                reader        = Contract.Reader;
                reader.Init(xmlReader, events, encodingStyle, this);
                if (_methods[mapping.Key].readMethod == null)
                {
                    if (_readerMethods == null)
                    {
                        _readerMethods = Contract.ReadMethods;
                    }
                    string methodName = (string)_readerMethods[mapping.Key];
                    if (methodName == null)
                    {
                        throw new InvalidOperationException(string.Format(ResXml.XmlNotSerializable, mapping.Accessor.Name));
                    }
                    _methods[mapping.Key].readMethod = GetMethodFromType(reader.GetType(), methodName, _pregeneratedAssmbly ? _assembly : null);
                }
                return(_methods[mapping.Key].readMethod.Invoke(reader, s_emptyObjectArray));
            }
            catch (SecurityException e)
            {
                throw new InvalidOperationException(ResXml.XmlNoPartialTrust, e);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
            }
        }
Exemplo n.º 7
0
 internal void SetTempAssembly(TempAssembly tempAssembly, XmlMapping mapping)
 {
     _tempAssembly    = tempAssembly;
     _mapping         = mapping;
     _typedSerializer = true;
 }
Exemplo n.º 8
0
 internal static TempAssembly GenerateTempAssembly(XmlMapping xmlMapping, Type type, string defaultNamespace, string location)
 {
     return(new TempAssembly(new XmlMapping[] { xmlMapping }, new Type[] { type }, defaultNamespace, location));
 }
Exemplo n.º 9
0
 internal static TempAssembly GenerateTempAssembly(XmlMapping xmlMapping)
 {
     return(GenerateTempAssembly(xmlMapping, null, null));
 }
Exemplo n.º 10
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;
 }
Exemplo n.º 11
0
 public XmlSerializerMappingKey(XmlMapping mapping)
 {
     this.Mapping = mapping;
 }
Exemplo n.º 12
0
        internal string GenerateTypedSerializer(string readMethod, string writeMethod, XmlMapping mapping, CodeIdentifiers classes, string baseSerializer, string readerClass, string writerClass)
        {
            string serializerName = CodeIdentifier.MakeValid(Accessor.UnescapeName(mapping.Accessor.Mapping.TypeDesc.Name));

            serializerName = classes.AddUnique(serializerName + "Serializer", mapping);

            TypeBuilder typedSerializerTypeBuilder = CodeGenerator.CreateTypeBuilder(
                _moduleBuilder,
                CodeIdentifier.GetCSharpName(serializerName),
                TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit,
                CreatedTypes[baseSerializer],
                CodeGenerator.EmptyTypeArray
                );

            ilg = new CodeGenerator(typedSerializerTypeBuilder);
            ilg.BeginMethod(
                typeof(Boolean),
                "CanDeserialize",
                new Type[] { typeof(XmlReader) },
                new string[] { "xmlReader" },
                CodeGenerator.PublicOverrideMethodAttributes
                );

            if (mapping.Accessor.Any)
            {
                ilg.Ldc(true);
                ilg.Stloc(ilg.ReturnLocal);
                ilg.Br(ilg.ReturnLabel);
            }
            else
            {
                MethodInfo XmlReader_IsStartElement = typeof(XmlReader).GetMethod(
                    "IsStartElement",
                    CodeGenerator.InstanceBindingFlags,
                    null,
                    new Type[] { typeof(String), typeof(String) },
                    null
                    );
                ilg.Ldarg(ilg.GetArg("xmlReader"));
                ilg.Ldstr(mapping.Accessor.Name);
                ilg.Ldstr(mapping.Accessor.Namespace);
                ilg.Call(XmlReader_IsStartElement);
                ilg.Stloc(ilg.ReturnLocal);
                ilg.Br(ilg.ReturnLabel);
            }
            ilg.MarkLabel(ilg.ReturnLabel);
            ilg.Ldloc(ilg.ReturnLocal);
            ilg.EndMethod();

            if (writeMethod != null)
            {
                ilg = new CodeGenerator(typedSerializerTypeBuilder);
                ilg.BeginMethod(
                    typeof(void),
                    "Serialize",
                    new Type[] { typeof(object), typeof(XmlSerializationWriter) },
                    new string[] { "objectToSerialize", "writer" },
                    CodeGenerator.ProtectedOverrideMethodAttributes);
                MethodInfo writerType_writeMethod = CreatedTypes[writerClass].GetMethod(
                    writeMethod,
                    CodeGenerator.InstanceBindingFlags,
                    null,
                    new Type[] { (mapping is XmlMembersMapping) ? typeof(object[]) : typeof(object) },
                    null
                    );
                ilg.Ldarg("writer");
                ilg.Castclass(CreatedTypes[writerClass]);
                ilg.Ldarg("objectToSerialize");
                if (mapping is XmlMembersMapping)
                {
                    ilg.ConvertValue(typeof(object), typeof(object[]));
                }
                ilg.Call(writerType_writeMethod);
                ilg.EndMethod();
            }
            if (readMethod != null)
            {
                ilg = new CodeGenerator(typedSerializerTypeBuilder);
                ilg.BeginMethod(
                    typeof(object),
                    "Deserialize",
                    new Type[] { typeof(XmlSerializationReader) },
                    new string[] { "reader" },
                    CodeGenerator.ProtectedOverrideMethodAttributes);
                MethodInfo readerType_readMethod = CreatedTypes[readerClass].GetMethod(
                    readMethod,
                    CodeGenerator.InstanceBindingFlags,
                    null,
                    CodeGenerator.EmptyTypeArray,
                    null
                    );
                ilg.Ldarg("reader");
                ilg.Castclass(CreatedTypes[readerClass]);
                ilg.Call(readerType_readMethod);
                ilg.EndMethod();
            }
            typedSerializerTypeBuilder.DefineDefaultConstructor(CodeGenerator.PublicMethodAttributes);
            Type typedSerializerType = typedSerializerTypeBuilder.CreateTypeInfo().AsType();

            CreatedTypes.Add(typedSerializerType.Name, typedSerializerType);

            return(typedSerializerType.Name);
        }