加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何使用XmlAttributeOverrides以编程方式生成序列化程序

发布时间:2020-12-15 21:23:26 所属栏目:百科 来源:网络整理
导读:我想生成一个序列化程序集或.cs文件来使用XmlAttributeOverrides序列化我的类型,然后直接在我的项目中引用此程序集/ .cs文件,而不是使用XmlSerializer来执行xml序列化/反序列化.这是因为序列化使用XmlAttributeOverrides,当您使用覆盖创建XmlSerializer时,它
我想生成一个序列化程序集或.cs文件来使用XmlAttributeOverrides序列化我的类型,然后直接在我的项目中引用此程序集/ .cs文件,而不是使用XmlSerializer来执行xml序列化/反序列化.这是因为序列化使用XmlAttributeOverrides,当您使用覆盖创建XmlSerializer时,它不会查找现有程序集,但会始终重新生成一个( reference).我的程序在无法运行csc.exe的环境中运行,因此我无法在运行时生成序列化程序集.

要清楚,我不能只使用sgen.exe,因为它只生成执行默认xml序列化/反序列化的程序集.如果你创建一个XmlSerializer和pass it XmlAttributeOverrides in the constructor然后Serialize()和Deserialize()不使用sgen.exe生成的程序集,因此sgen.exe似乎对我没用.使用覆盖时,XmlSerializer将始终生成新的程序集.

那么,有没有办法可以调用XmlSerializer或其他类并让它创建一个.cs文件或dll,我可以包含在我的项目中?如果可能的话,我想自动执行此过程,因此每当我更改正在序列化的类型时,我都不需要手动更改.我不能使用sgen.exe / k,因为它只生成一个类型的默认XmlSerializer而不是我需要使用覆盖的类型.是否有另一种方法来生成或捕获由XmlSerializer创建的.cs文件?

(我有一个相关的问题here,这是这个问题的根源)

解决方法

您可以在编译时生成程序集.在Visual Studio打开的项目属性中,单击“构建”选项卡并向下滚动到“生成序列化程序集”.

将此设置为on将在本地编译它们,以便它们可以与您的解决方案一起部署.它们也可以使用Sgen.exe工具进行编译.有关详细信息,请参阅here.

Generate serialization assembly

Specifies whether the compiler will use the XML Serializer Generator
Tool (Sgen.exe) to create XML serialization assemblies. Serialization
assemblies can improve the startup performance of XmlSerializer if you
have used that class to serialize types in your code. By default,this
option is set to Auto,which specifies that serialization assemblies
be generated only if you have used XmlSerializer to encode types in
your code to XML. Off specifies that serialization assemblies never be
generated,regardless of whether your code uses XmlSerializer. On
specifies that serialization assemblies always be generated.
Serialization assemblies are named TypeName.XmlSerializers.dll. For
more information,see 07001.

从上次评论中我看到了你的问题.请参阅此question,我相信如果您使用XmlAttributeOverrides将始终生成程序集.

因此,如果您无法启动csc.exe,请不要使用XmlAttributeOverrides.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读