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();
}
}