使用SGEN和ILMerge pre产生XmlSerializers。麻烦数组数组、麻烦、ILMerge、SGEN

2023-09-03 13:37:13 作者:得不到的ミ就毁灭

我使用XmlSerializer的广泛,而不是让.NET来生成所需的序列化程序集在运行时,我想提前生成这些组件,并与我的应用程序捆绑他们。我能够使用SGEN生成这些组件在构建时。此外,我必须单独运行SGEN每个阵列的类型,我将序列化(使用SGEN /吨:富[])。最后,我用ILMerge合并数组类型的序列化类到Foo.XmlSerializers.dll组装。我曾与ILDASM证实Foo.XmlSErializers.dll事实上确实包含所有的合并类。

在运行时,.NET成功加载FooSerializer从Foo.XmlSerializers.dll而无需调用CSC,并产生一个临时议会。然而,.NET无法从同一DLL加载ArrayOfFooSerializer,和事实上确实调用CSC

我怎样才能成功地pre-生成序列类型数组?

考虑以下2组件及其简化内容:

大会:MyApp.exe的

 公共类的MyApp
{
    公共静态INT主要(字串[] args)
    {
        新的XmlSerializer(typeof运算(富));
        新的XmlSerializer(typeof运算(富[]));
    }
}
 

大会:Foo.dll

 公共类Foo
{

}
 

进一步信息:

下面的app.config导致XmlSerialization相关的事件被添加到事件日志

 <结构>
  < System.Diagnostics程序>
    <开关>
      <添加的name = value =1/&GTXmlSerialization pregenEventLog。
      <添加名称=XmlSerialization.Compilation值=1/>
    < /开关>
  < /system.diagnostics>
< /结构>
 
治疗强直性脊柱炎,缓解附着点炎是根本

我看不出有什么事件的Foo类型不同。我看到下面的消息美孚[]:

  

pre-生成序列   'Foo.XmlSerializers'已过期。您   需要重新生成串行器为   '富[]'。

解决方案

您的问题实际上可能是由于您运行ILMerge的事实。序列化程序集生成轨道的确切的版本,它从产生的汇编,并会说,它已过期,如果有什么区别(即使只是在code,而不是接口)。事实证明,ILMerge改变了大会的ID,这可能是导致此。 有关问题的更多信息,请参阅这篇文章。

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[]'.

解决方案

Your problem may actually be due to the fact that you ran ILMerge. The serialization assembly you generate tracks the exact version of the assembly it was generated from, and will say it has expired if there is any difference (even if only in the code, and not the interface). As it turns out, ILMerge changes that Assembly ID, which could be causing this. See this post for more information about the problem.