使用 XmlAttributeOverrides 时,如何以编程方式生成序列化程序集或 cs 文件,如 XmlSerializer 那样?
我想生成一个序列化程序集或 .cs 文件来使用 XmlAttributeOverrides 序列化我的类型,然后直接在我的项目中引用此程序集/.cs 文件,而不是使用 XmlSerializer 执行 xml 序列化/反序列化。这是因为序列化使用 XmlAttributeOverrides,并且当您创建具有覆盖的 XmlSerializer 时,它不会查找现有程序集,但始终会重新生成一个程序集 (参考)。我的程序在无法运行 csc.exe 的环境中运行,因此我无法在运行时生成序列化程序集。
需要明确的是,我不能只使用 sgen.exe,因为它只生成执行默认 xml 序列化/反序列化的程序集。如果创建 XmlSerializer 并在构造函数中传递 XmlAttributeOverrides,则序列化() 和 Deserialize() 不使用 sgen.exe 生成的程序集,因此 sgen.exe 对我来说似乎没有用处。使用覆盖时,XmlSerializer 将始终生成新的程序集。
那么,有没有一种方法可以调用 XmlSerializer 或其他类并让它创建可以包含在项目中的 .cs 文件或 dll?如果可能的话,我想自动化这个过程,这样每当我更改正在序列化的类型时,我就不需要进行手动更改。我无法使用 sgen.exe /k,因为它只会生成某种类型的默认 XmlSerializer,而不是我需要的使用覆盖的类型。是否有另一种方法来生成或捕获由 XmlSerializer 创建的 .cs 文件?
(我有一个相关的问题这里这是根的这个)
I want to generate a serialization assembly or .cs file to serialize my types using XmlAttributeOverrides, and then reference this assembly/.cs file in my project directly rather than use XmlSerializer to perform xml serialization/deserialization. This is because the serialization uses XmlAttributeOverrides and when you create an XmlSerializer with overrides it doesn't look for an existing assembly but will always re-generate one (reference). My program runs in an environment where it's not possible to run csc.exe, therefore my I cannot generate a serialization assembly at runtime.
To be clear, I can't just use sgen.exe because that only generates assemblies that perform the default xml serialization/deserialization. If you create an XmlSerializer and pass it XmlAttributeOverrides in the constructor then Serialize() and Deserialize() do NOT use the assembly generated by sgen.exe, therefore sgen.exe seems of no use to me. When using overrides XmlSerializer will always generate a new assembly.
So, is there a way I can call XmlSerializer or other classes and get it to create a .cs file or dll that I can include in my project? I'd like to automate this process if possible so I don't need to make manual changes whenever I change my types that are being serialized. I can't use sgen.exe /k because that only generates the default XmlSerializer for a type instead of the one I need which uses overrides. Is there another way to generate or capture the .cs file that's created by XmlSerializer?
(I have a related question here that's the root of this one)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在编译时生成程序集。在 Visual Studio 中打开项目
属性
,单击构建
选项卡并向下滚动到生成序列化程序集
。将其设置为
on
将在本地编译它们,以便它们可以与您的解决方案一起部署。它们也可以使用Sgen.exe
工具进行编译。有关详细信息,请参阅此处。从最后的评论我看到了你的问题。请参阅此问题,我相信如果您使用
XmlAttributeOverrides
程序集总会产生。因此,如果您无法启动 csc.exe,则不要使用
XmlAttributeOverrides
。You can generate the assemblies at compile time. In Visual Studio open project
properties
, click thebuild
tab and scroll down toGenerate serialization assemblies
.Setting this to
on
will compile them locally so that they may be deployed with your solution. They can also be compiled using theSgen.exe
tool. See here for more info.From the last comment I see your issue. Refer to this question, I believe if your using
XmlAttributeOverrides
the assembly will always be generated.Hence if you can't launch csc.exe then don't use
XmlAttributeOverrides
.