I use XmlSerializer extensively and rather than allowing .NET to generate the necessary serialization assemblies at runtime, I'd like to generate these assemblies ahead of time and bundle them with my application. I am able to use Sgen to generate these assemblies at build time. Additionally, I must run Sgen separately for each array type that I will serialize (using sgen /t:Foo[]). Finally, I use ILMerge to merge the array type serialization classes into the Foo.XmlSerializers.dll assembly. I have verified with ildasm that Foo.XmlSErializers.dll does in fact contain all of the merged classes.
At runtime, .NET successfully loads FooSerializer from Foo.XmlSerializers.dll without invoking csc and generating a temporary assembly. However, .NET fails to load ArrayOfFooSerializer from the same dll, and does in fact invoke csc.
How can I successfully pre-generate serialization types for arrays?
Consider the following 2 assemblies and their simplified contents:
Assembly: MyApp.exe
public class MyApp
{
public static int Main(string[] args)
{
new XmlSerializer(typeof(Foo));
new XmlSerializer(typeof(Foo[]));
}
}
Assembly: Foo.dll
public class Foo
{
}
Further Info:
The following app.config causes XmlSerialization related events to be added to the Event Log
<configuration>
<system.diagnostics>
<switches>
<add name="XmlSerialization.PregenEventLog" value="1" />
<add name="XmlSerialization.Compilation" value="1" />
</switches>
</system.diagnostics>
</configuration>
I see no events for the Foo type. I see the following message for Foo[]:
Pre-generated serializer 'Foo.XmlSerializers' has expired. You need to re-generate serializer for 'Foo[]'.