How to use XmlSerializer with classes produced from CompileAssemblyFromSource ?
I dynamicaly generate code and use theCodeDomProviderandCompileAssemblyFromSourceto produce an in memory Assembly. The generated code uses List<T> as one of the Properties.
I then need to stream XML into and out of a class in the in memory Assembly.
I first use theActivator to create an instance of the class:
Assembly assembly = results.CompiledAssembly;Type[] exportedTypes = assembly.GetExportedTypes();object rc =
Activator.CreateInstance(exportedTypes[0])The next step is to put the object in a Property Grid and fill it with data.
So far so good.
The problem occurs when i try to use theXmlSerializer to write it out as XML:
StringWriter sw1 = new StringWriter(); }
Type type = rc.GetType();
XmlSerializer xs = new XmlSerializer(type, "Common.Generated");
try
{
using (XmlTextWriter writer = new XmlTextWriter(sw1))
{
writer.Formatting = Formatting.Indented;
xs.Serialize(writer, rc);
}
Log(sw1.ToString());
catch (Exception ex)
{
Log(ex.ToString());
}
I get the following exception:
System.InvalidOperationException: There was an error generating the XML document. > System.TypeInitializationException: The type initializer for 'Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterXXX' threw an exception. > System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterXXX..cctor()
End of inner exception stack trace
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterXXX..ctor()
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializerContract.get_Writer()
at System.Xml.Serialization.TempAssembly.InvokeWriter(XmlMapping mapping, XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
End of inner exception stack trace
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o)
If I cut and past the generated code into a simple Test Program it compiles and Serializes fine, using the same code from above.
Here is a sample of the Generated Code:
namespace Common.Generated
{
[Serializable()]
[XmlRoot("CONFIGURATION")]
public class XXX
{
[XmlInclude(typeof(XXX.ITEM))]
[XmlInclude(typeof(List<XXX.ITEM>))]
[Serializable()]
public class ITEM
{
public ITEM() { }
private string NameField = String.Empty;
[XmlElement("NAME")]
public string Name
{
get { return (this.NameField); }
set { this.NameField = value; }
}
}
public XXX() { }
private List<XXX.ITEM> items_ = new List<XXX.ITEM>();
[XmlArray("ITEMS")]
[XmlArrayItem("ITEM", Type=typeof(XXX.ITEM))]
public List<XXX.ITEM> Items
{
get { return ( this.items_ ); }
set { this.items_ = value; }
}
}
}
Here is a sample of how I am compiling the code:
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters compilerParameters = new CompilerParameters();
compilerParameters.ReferencedAssemblies.Add("System.dll");
compilerParameters.ReferencedAssemblies.Add("mscorlib.dll");
compilerParameters.ReferencedAssemblies.Add("system.xml.dll");
compilerParameters.ReferencedAssemblies.Add("system.data.dll");
compilerParameters.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
compilerParameters.IncludeDebugInformation = false;
compilerParameters.GenerateExecutable = false;
compilerParameters.GenerateInMemory = true;
CompilerResults results = provider.CompileAssemblyFromSource(compilerParameters, _code);
if (results.Errors.HasErrors)
{
Log("Error compiling assembly:");
foreach (CompilerError error in results.Errors)
{
Log(error.ErrorText);
}
}
else
{
Assembly assembly = results.CompiledAssembly;
Type[] exportedTypes = assembly.GetExportedTypes();
rc = Activator.CreateInstance(exportedTypes[0]);
Log("OK");
}
}
Any help would be apprecieated...

