Inheritance: XmlSerializationCodeGen
コード例 #1
0
 internal Member(XmlSerializationReaderCodeGen outerClass, string source, string arrayName, int i, MemberMapping mapping, bool multiRef)
     : this(outerClass, source, null, arrayName, i, mapping, multiRef, null) {
 }
コード例 #2
0
            internal Member(XmlSerializationReaderCodeGen outerClass, string source, string arraySource, string arrayName, int i, MemberMapping mapping, bool multiRef, string choiceSource) {
                this.source = source;
                this.arrayName = arrayName + "_" + i.ToString(CultureInfo.InvariantCulture);
                this.choiceArrayName = "choice_" + this.arrayName;
                this.choiceSource = choiceSource;
                ElementAccessor[] elements = mapping.Elements;

                if (mapping.TypeDesc.IsArrayLike) {
                    if (arraySource != null)
                        this.arraySource = arraySource;
                    else
                        this.arraySource = outerClass.GetArraySource(mapping.TypeDesc, this.arrayName, multiRef);
                    isArray = mapping.TypeDesc.IsArray;
                    isList = !isArray;
                    if (mapping.ChoiceIdentifier != null) {
                        this.choiceArraySource = outerClass.GetArraySource(mapping.TypeDesc, this.choiceArrayName, multiRef);

                        string a = choiceArrayName;
                        string c = "c" + a;
                        bool choiceUseReflection = mapping.ChoiceIdentifier.Mapping.TypeDesc.UseReflection;
                        string choiceTypeFullName = mapping.ChoiceIdentifier.Mapping.TypeDesc.CSharpName;
                        string castString = choiceUseReflection?"":"(" + choiceTypeFullName + "[])";
                        
                        string init = a + " = " + castString +
                            "EnsureArrayIndex(" + a + ", " + c + ", " + outerClass.RaCodeGen.GetStringForTypeof(choiceTypeFullName, choiceUseReflection) + ");";
                        this.choiceArraySource = init + outerClass.RaCodeGen.GetStringForArrayMember(a,  c + "++", mapping.ChoiceIdentifier.Mapping.TypeDesc);
                    }
                    else {
                        this.choiceArraySource = this.choiceSource;
                    }
                }
                else {
                    this.arraySource = arraySource == null ? source : arraySource;
                    this.choiceArraySource = this.choiceSource;
                }
                this.mapping = mapping;
            }
コード例 #3
0
 internal Member(XmlSerializationReaderCodeGen outerClass, string source, string arraySource, string arrayName, int i, MemberMapping mapping, string choiceSource) 
     : this (outerClass, source, arraySource, arrayName, i, mapping, false, choiceSource) { 
 }
コード例 #4
0
        internal static Assembly GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, string defaultNamespace, Evidence evidence, CompilerParameters parameters, Assembly assembly, Hashtable assemblies) {
            FileIOPermission.Assert();
            for (int i = 0; i < xmlMappings.Length; i++) {
                xmlMappings[i].CheckShallow();
            }
            Compiler compiler = new Compiler();
            try {
                Hashtable scopeTable = new Hashtable();
                foreach (XmlMapping mapping in xmlMappings)
                    scopeTable[mapping.Scope] = mapping;
                TypeScope[] scopes = new TypeScope[scopeTable.Keys.Count];
                scopeTable.Keys.CopyTo(scopes, 0);
                
                assemblies.Clear();
                Hashtable importedTypes = new Hashtable();
                foreach (TypeScope scope in scopes) {
                    foreach (Type t in scope.Types) {
                        compiler.AddImport(t, importedTypes);
                        Assembly a = t.Assembly;
                        string name = a.FullName;
                        if (assemblies[name] != null)
                            continue;
                        if (!a.GlobalAssemblyCache) {
                            assemblies[name] = a;
                        }
                    }
                }
                for (int i = 0; i < types.Length; i++) {
                    compiler.AddImport(types[i], importedTypes);
                }
                compiler.AddImport(typeof(object).Assembly);
                compiler.AddImport(typeof(XmlSerializer).Assembly);

                IndentedWriter writer = new IndentedWriter(compiler.Source, false);
                
                writer.WriteLine("#if _DYNAMIC_XMLSERIALIZER_COMPILATION");
                writer.WriteLine("[assembly:System.Security.AllowPartiallyTrustedCallers()]");
                writer.WriteLine("[assembly:System.Security.SecurityTransparent()]");
                writer.WriteLine("#endif");
                // Add AssemblyVersion attribute to match parent accembly version
                if (types != null && types.Length > 0 && types[0] != null) {
                    writer.WriteLine("[assembly:System.Reflection.AssemblyVersionAttribute(\"" + types[0].Assembly.GetName().Version.ToString() + "\")]");
                }
                if (assembly != null && types.Length > 0) {
                    for (int i = 0; i < types.Length; i++) {
                        Type type = types[i];
                        if (type == null)
                            continue;
                        if (DynamicAssemblies.IsTypeDynamic(type)) {
                            throw new InvalidOperationException(Res.GetString(Res.XmlPregenTypeDynamic, types[i].FullName));
                        }
                    }
                    writer.Write("[assembly:");
                    writer.Write(typeof(XmlSerializerVersionAttribute).FullName);
                    writer.Write("(");
                    writer.Write("ParentAssemblyId=");
                    ReflectionAwareCodeGen.WriteQuotedCSharpString(writer, GenerateAssemblyId(types[0]));
                    writer.Write(", Version=");
                    ReflectionAwareCodeGen.WriteQuotedCSharpString(writer, ThisAssembly.Version);
                    if (defaultNamespace != null) {
                        writer.Write(", Namespace=");
                        ReflectionAwareCodeGen.WriteQuotedCSharpString(writer, defaultNamespace);
                    }
                    writer.WriteLine(")]");
                }
                CodeIdentifiers classes = new CodeIdentifiers();
                classes.AddUnique("XmlSerializationWriter", "XmlSerializationWriter");
                classes.AddUnique("XmlSerializationReader", "XmlSerializationReader");
                string suffix = null;
                if (types != null && types.Length == 1 && types[0] != null) {
                    suffix = CodeIdentifier.MakeValid(types[0].Name);
                    if (types[0].IsArray) {
                        suffix += "Array";
                    }
                }

                writer.WriteLine("namespace " + GeneratedAssemblyNamespace + " {");
                writer.Indent++;

                writer.WriteLine();

                string writerClass = "XmlSerializationWriter" + suffix;
                writerClass = classes.AddUnique(writerClass, writerClass);
                XmlSerializationWriterCodeGen writerCodeGen = new XmlSerializationWriterCodeGen(writer, scopes, "public", writerClass);

                writerCodeGen.GenerateBegin();
                string[] writeMethodNames = new string[xmlMappings.Length];

                for (int i = 0; i < xmlMappings.Length; i++) {
                    writeMethodNames[i] = writerCodeGen.GenerateElement(xmlMappings[i]);
                }
                writerCodeGen.GenerateEnd();
                    
                writer.WriteLine();

                string readerClass = "XmlSerializationReader" + suffix;
                readerClass = classes.AddUnique(readerClass, readerClass);
                XmlSerializationReaderCodeGen readerCodeGen = new XmlSerializationReaderCodeGen(writer, scopes, "public", readerClass);

                readerCodeGen.GenerateBegin();
                string[] readMethodNames = new string[xmlMappings.Length];
                for (int i = 0; i < xmlMappings.Length; i++) {
                    readMethodNames[i] = readerCodeGen.GenerateElement(xmlMappings[i]);
                }
                readerCodeGen.GenerateEnd(readMethodNames, xmlMappings, types);

                string baseSerializer = readerCodeGen.GenerateBaseSerializer("XmlSerializer1", readerClass, writerClass, classes);
                Hashtable serializers = new Hashtable();
                for (int i = 0; i < xmlMappings.Length; i++) {
                    if (serializers[xmlMappings[i].Key] == null) {
                        serializers[xmlMappings[i].Key] = readerCodeGen.GenerateTypedSerializer(readMethodNames[i], writeMethodNames[i], xmlMappings[i], classes, baseSerializer, readerClass, writerClass);
                    }
                }
                readerCodeGen.GenerateSerializerContract("XmlSerializerContract", xmlMappings, types, readerClass, readMethodNames, writerClass, writeMethodNames, serializers);
                writer.Indent--;
                writer.WriteLine("}");

                return compiler.Compile(assembly, defaultNamespace, parameters, evidence);
            }
            finally {
                compiler.Close();
            }
        }
コード例 #5
0
ファイル: compilation.cs プロジェクト: ArildF/masters
        public TempAssembly(XmlMapping[] xmlMappings) {
            Compiler compiler = new Compiler();
            allAssembliesAllowPartialTrust = false;
            try {
                IndentedWriter writer = new IndentedWriter(compiler.Source, false);
                writer.WriteLine("[assembly:System.Security.AllowPartiallyTrustedCallers()]");
                writer.WriteLine("namespace " + GeneratedAssemblyNamespace + " {");
                writer.Indent++;
                writer.WriteLine();

                Hashtable scopeTable = new Hashtable();
                foreach (XmlMapping mapping in xmlMappings)
                    scopeTable[mapping.Scope] = mapping;
                TypeScope[] scopes = new TypeScope[scopeTable.Keys.Count];
                scopeTable.Keys.CopyTo(scopes, 0);
                
                XmlSerializationWriterCodeGen writerCodeGen = new XmlSerializationWriterCodeGen(writer, scopes);
                writerCodeGen.GenerateBegin();
                string[] writeMethodNames = new string[xmlMappings.Length];
                for (int i = 0; i < xmlMappings.Length; i++)
                    writeMethodNames[i] = writerCodeGen.GenerateElement(xmlMappings[i]);
                writerCodeGen.GenerateEnd();
                    
                writer.WriteLine();
                
                XmlSerializationReaderCodeGen readerCodeGen = new XmlSerializationReaderCodeGen(writer, scopes);
                readerCodeGen.GenerateBegin();
                string[] readMethodNames = new string[xmlMappings.Length];
                for (int i = 0; i < xmlMappings.Length; i++)
                    readMethodNames[i] = readerCodeGen.GenerateElement(xmlMappings[i]);
                readerCodeGen.GenerateEnd();
                    
                writer.Indent--;
                writer.WriteLine("}");
                allAssembliesAllowPartialTrust = true;
                assemblies = new Hashtable();
                foreach (TypeScope scope in scopes) {
                    foreach (Type t in scope.Types) {
                        compiler.AddImport(t);
                        Assembly a = t.Assembly;
                        if (allAssembliesAllowPartialTrust && !AssemblyAllowsPartialTrust(a))
                            allAssembliesAllowPartialTrust = false;
                        if (!a.GlobalAssemblyCache)
                            assemblies[a.FullName] = a;
                    }
                }
                compiler.AddImport(typeof(XmlWriter));
                compiler.AddImport(typeof(XmlSerializationWriter));
                compiler.AddImport(typeof(XmlReader));
                compiler.AddImport(typeof(XmlSerializationReader));

                assembly = compiler.Compile();

                methods = new TempMethod[xmlMappings.Length];
                
                readerType = GetTypeFromAssembly("XmlSerializationReader1");
                writerType = GetTypeFromAssembly("XmlSerializationWriter1");

                for (int i = 0; i < methods.Length; i++) {
                    TempMethod method = new TempMethod();
                    XmlTypeMapping xmlTypeMapping = xmlMappings[i] as XmlTypeMapping;
                    if (xmlTypeMapping != null) {
                        method.name = xmlTypeMapping.ElementName;
                        method.ns = xmlTypeMapping.Namespace;
                    }
                    method.readMethod = GetMethodFromType(readerType, readMethodNames[i]);
                    method.writeMethod = GetMethodFromType(writerType, writeMethodNames[i]);
                    methods[i] = method;
                }
            }
            finally {
                compiler.Close();
            }
        }
 internal Member(XmlSerializationReaderCodeGen outerClass, string source, string arraySource, string arrayName, int i, MemberMapping mapping, bool multiRef, string choiceSource)
 {
     this.fixupIndex = -1;
     this.source = source;
     this.arrayName = arrayName + "_" + i.ToString(CultureInfo.InvariantCulture);
     this.choiceArrayName = "choice_" + this.arrayName;
     this.choiceSource = choiceSource;
     ElementAccessor[] elements = mapping.Elements;
     if (mapping.TypeDesc.IsArrayLike)
     {
         if (arraySource != null)
         {
             this.arraySource = arraySource;
         }
         else
         {
             this.arraySource = outerClass.GetArraySource(mapping.TypeDesc, this.arrayName, multiRef);
         }
         this.isArray = mapping.TypeDesc.IsArray;
         this.isList = !this.isArray;
         if (mapping.ChoiceIdentifier != null)
         {
             this.choiceArraySource = outerClass.GetArraySource(mapping.TypeDesc, this.choiceArrayName, multiRef);
             string choiceArrayName = this.choiceArrayName;
             string str2 = "c" + choiceArrayName;
             bool useReflection = mapping.ChoiceIdentifier.Mapping.TypeDesc.UseReflection;
             string cSharpName = mapping.ChoiceIdentifier.Mapping.TypeDesc.CSharpName;
             string str4 = useReflection ? "" : ("(" + cSharpName + "[])");
             string str5 = choiceArrayName + " = " + str4 + "EnsureArrayIndex(" + choiceArrayName + ", " + str2 + ", " + outerClass.RaCodeGen.GetStringForTypeof(cSharpName, useReflection) + ");";
             this.choiceArraySource = str5 + outerClass.RaCodeGen.GetStringForArrayMember(choiceArrayName, str2 + "++", mapping.ChoiceIdentifier.Mapping.TypeDesc);
         }
         else
         {
             this.choiceArraySource = this.choiceSource;
         }
     }
     else
     {
         this.arraySource = (arraySource == null) ? source : arraySource;
         this.choiceArraySource = this.choiceSource;
     }
     this.mapping = mapping;
 }
コード例 #7
0
 internal static Assembly GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, string defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies)
 {
     Assembly assembly3;
     FileIOPermission.Assert();
     for (int i = 0; i < xmlMappings.Length; i++)
     {
         xmlMappings[i].CheckShallow();
     }
     Compiler compiler = new Compiler();
     try
     {
         Hashtable hashtable = new Hashtable();
         foreach (XmlMapping mapping in xmlMappings)
         {
             hashtable[mapping.Scope] = mapping;
         }
         TypeScope[] array = new TypeScope[hashtable.Keys.Count];
         hashtable.Keys.CopyTo(array, 0);
         assemblies.Clear();
         Hashtable hashtable2 = new Hashtable();
         foreach (TypeScope scope in array)
         {
             foreach (Type type in scope.Types)
             {
                 compiler.AddImport(type, hashtable2);
                 Assembly assembly2 = type.Assembly;
                 string fullName = assembly2.FullName;
                 if ((assemblies[fullName] == null) && !assembly2.GlobalAssemblyCache)
                 {
                     assemblies[fullName] = assembly2;
                 }
             }
         }
         for (int j = 0; j < types.Length; j++)
         {
             compiler.AddImport(types[j], hashtable2);
         }
         compiler.AddImport(typeof(object).Assembly);
         compiler.AddImport(typeof(XmlSerializer).Assembly);
         IndentedWriter writer = new IndentedWriter(compiler.Source, false);
         writer.WriteLine("#if _DYNAMIC_XMLSERIALIZER_COMPILATION");
         writer.WriteLine("[assembly:System.Security.AllowPartiallyTrustedCallers()]");
         writer.WriteLine("[assembly:System.Security.SecurityTransparent()]");
         writer.WriteLine("[assembly:System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)]");
         writer.WriteLine("#endif");
         if (((types != null) && (types.Length > 0)) && (types[0] != null))
         {
             writer.WriteLine("[assembly:System.Reflection.AssemblyVersionAttribute(\"" + types[0].Assembly.GetName().Version.ToString() + "\")]");
         }
         if ((assembly != null) && (types.Length > 0))
         {
             for (int num3 = 0; num3 < types.Length; num3++)
             {
                 Type type2 = types[num3];
                 if ((type2 != null) && DynamicAssemblies.IsTypeDynamic(type2))
                 {
                     throw new InvalidOperationException(Res.GetString("XmlPregenTypeDynamic", new object[] { types[num3].FullName }));
                 }
             }
             writer.Write("[assembly:");
             writer.Write(typeof(XmlSerializerVersionAttribute).FullName);
             writer.Write("(");
             writer.Write("ParentAssemblyId=");
             ReflectionAwareCodeGen.WriteQuotedCSharpString(writer, GenerateAssemblyId(types[0]));
             writer.Write(", Version=");
             ReflectionAwareCodeGen.WriteQuotedCSharpString(writer, "4.0.0.0");
             if (defaultNamespace != null)
             {
                 writer.Write(", Namespace=");
                 ReflectionAwareCodeGen.WriteQuotedCSharpString(writer, defaultNamespace);
             }
             writer.WriteLine(")]");
         }
         CodeIdentifiers classes = new CodeIdentifiers();
         classes.AddUnique("XmlSerializationWriter", "XmlSerializationWriter");
         classes.AddUnique("XmlSerializationReader", "XmlSerializationReader");
         string str2 = null;
         if (((types != null) && (types.Length == 1)) && (types[0] != null))
         {
             str2 = CodeIdentifier.MakeValid(types[0].Name);
             if (types[0].IsArray)
             {
                 str2 = str2 + "Array";
             }
         }
         writer.WriteLine("namespace Microsoft.Xml.Serialization.GeneratedAssembly {");
         writer.Indent++;
         writer.WriteLine();
         string identifier = "XmlSerializationWriter" + str2;
         identifier = classes.AddUnique(identifier, identifier);
         XmlSerializationWriterCodeGen gen = new XmlSerializationWriterCodeGen(writer, array, "public", identifier);
         gen.GenerateBegin();
         string[] writerMethods = new string[xmlMappings.Length];
         for (int k = 0; k < xmlMappings.Length; k++)
         {
             writerMethods[k] = gen.GenerateElement(xmlMappings[k]);
         }
         gen.GenerateEnd();
         writer.WriteLine();
         string str4 = "XmlSerializationReader" + str2;
         str4 = classes.AddUnique(str4, str4);
         XmlSerializationReaderCodeGen gen2 = new XmlSerializationReaderCodeGen(writer, array, "public", str4);
         gen2.GenerateBegin();
         string[] methods = new string[xmlMappings.Length];
         for (int m = 0; m < xmlMappings.Length; m++)
         {
             methods[m] = gen2.GenerateElement(xmlMappings[m]);
         }
         gen2.GenerateEnd(methods, xmlMappings, types);
         string baseSerializer = gen2.GenerateBaseSerializer("XmlSerializer1", str4, identifier, classes);
         Hashtable serializers = new Hashtable();
         for (int n = 0; n < xmlMappings.Length; n++)
         {
             if (serializers[xmlMappings[n].Key] == null)
             {
                 serializers[xmlMappings[n].Key] = gen2.GenerateTypedSerializer(methods[n], writerMethods[n], xmlMappings[n], classes, baseSerializer, str4, identifier);
             }
         }
         gen2.GenerateSerializerContract("XmlSerializerContract", xmlMappings, types, str4, methods, identifier, writerMethods, serializers);
         writer.Indent--;
         writer.WriteLine("}");
         assembly3 = compiler.Compile(assembly, defaultNamespace, parameters, evidence);
     }
     finally
     {
         compiler.Close();
     }
     return assembly3;
 }