使用 xsd.exe 创建虚拟方法
我正在使用通过 xsd.exe 工具从 XML 架构生成的类。它当前生成一个巨大的(32k 行).cs 文件。然后,我使用 XMLSerializer 序列化和反序列化模型的部分内容。我需要重写这些类中的属性,因此我在单独的文件中有部分类来重写其中一些生成的方法。然而,这意味着每次模式更改时都要进入并将数百个方法标记为虚拟方法。有没有办法让 xsd.exe 工具在生成方法时将其标记为虚拟方法?
I am using classes that were generated from an XML schema using the xsd.exe tool. It currently generates a huge (32k line) .cs file. I then serialize and deserialize parts of the of model using XMLSerializer. I need to override properties in these classes, so I have partial classes in separate files that override some of these generated methods. However, this means going in and marking hundreds of methods as virtual every time the schema changes. Is there a way to get the xsd.exe tool to mark methods as virtual when they are generated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
XSD.exe 的输出不太可定制。
我不久前遇到了类似的问题,并创建了一个简单的控制台应用程序来修改 XSD.exe 生成的输出。
The output from XSD.exe is not very customizable.
I had a similar problem a while ago and created a simple console application that modified the generated output from XSD.exe.
对我来说,您需要修改序列化代码似乎很奇怪。如果您需要覆盖属性,难道您不能简单地创建新属性来包装现有属性并添加新行为吗?或者我错过了重点。
It seems odd to me that you need to modify the serialization code. If you need to override properties, can't you simply create new properties that kind of wrap the existing ones yet add new behaviour? Or did I miss the point.
如果您必须持续执行此操作,则应该研究某种类型的代码生成。构建 XSD 生成的类型,然后编写应用程序来加载这些类型,并使用反射生成相同的类型(除了所有属性都是虚拟的)。然后,您将能够从新类型派生并根据需要覆盖属性。
这可以成为您构建过程的一部分。
If you have to do this on an ongoing basis, you should look into code generation of some kind. Build the XSD-generated types, then write an application to load those types, and use Reflection to generate types that are identical except that all the properties are virtual. You will then be able to derive from the new types and override the properties as necessary.
This can become part of your build process.