代码之家  ›  专栏  ›  技术社区  ›  anthony

使用Sgen和ILMerge预生成XmlSerializer。阵列故障

  •  4
  • anthony  · 技术社区  · 16 年前

    我广泛使用XmlSerializer,不允许.NET在运行时生成必要的序列化程序集,而是希望提前生成这些程序集,并将它们与我的应用程序捆绑在一起。我能够在构建时使用Sgen生成这些程序集。此外,我必须为要序列化的每个数组类型分别运行Sgen(使用Sgen/t:Foo[])。最后,我使用ILMerge将数组类型序列化类合并到Foo.xmlsializers.dll程序集中。我已经用ildasm验证了Foo.XmlSErializers.dll实际上包含所有合并的类。

    如何成功预生成阵列的序列化类型?

    考虑以下2个程序集及其简化内容:

    程序集:MyApp.exe

    public class MyApp
    {
        public static int Main(string[] args)
        {
            new XmlSerializer(typeof(Foo));
            new XmlSerializer(typeof(Foo[]));
        }
    }
    

    程序集:Foo.dll

    public class Foo
    {
    
    }
    

    以下app.config导致将XmlSerialization相关事件添加到事件日志中

    <configuration>
      <system.diagnostics> 
        <switches> 
          <add name="XmlSerialization.PregenEventLog" value="1" />
          <add name="XmlSerialization.Compilation" value="1" />
        </switches> 
      </system.diagnostics>
    </configuration>
    

    预生成序列化程序 “Foo.XmlSerializers”已过期。你 “Foo[]”。

    2 回复  |  直到 16 年前
        1
  •  6
  •   Nick    16 年前

    您的问题实际上可能是由于您运行了ILMerge。生成的序列化程序集跟踪 准确的 See this post for more information about the problem .

        2
  •  3
  •   Juozas Kontvainis    12 年前

    您可以尝试从Mono查看sgen,它允许同时为多个类型生成序列化程序集。

    推荐文章