title: Pre-generating XmlSerializers using Sgen and ILMerge. Trouble with arrays
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>How can I successfully pre-generate serialization types for arrays?</p>
  
  <p>Consider the following 2 assemblies and their simplified contents:</p>
  
  <p>Assembly: MyApp.exe</p>
  
  <pre><code>public class MyApp
  {
      public static int Main(string[] args)
      {
          new XmlSerializer(typeof(Foo));
          new XmlSerializer(typeof(Foo[]));
      }
  }
  </code></pre>
  
  <p>Assembly: Foo.dll</p>
  
  <pre><code>public class Foo
  {
  
  }
  </code></pre>
  
  <p>Further Info:</p>
  
  <p>The following app.config causes XmlSerialization related events to be added to the Event Log</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;system.diagnostics&gt; 
      &lt;switches&gt; 
        &lt;add name="XmlSerialization.PregenEventLog" value="1" /&gt;
        &lt;add name="XmlSerialization.Compilation" value="1" /&gt;
      &lt;/switches&gt; 
    &lt;/system.diagnostics&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>I see no events for the Foo type. I see the following message for Foo[]:</p>
  
  <blockquote>
    <p>Pre-generated serializer
    'Foo.XmlSerializers' has expired. You
    need to re-generate serializer for
    'Foo[]'.</p>
  </blockquote>