简体   繁体   English

使用XSD.exe自定义类名生成的类

[英]Classes Generated with XSD.exe Custom Class Names

是否可以对使用.Net XSD.exe工具生成的类名进行任何控制?

Basically, no. 基本上没有。 If you were writing the classes manually, you could have: 如果您手动编写类,则可以:

[XmlType("bar")]
class Foo {}

however, you can't do this with the xsd-generated classes. 但是,您不能使用xsd生成的类来执行此操作。 Unfortunately, one of the things you can't do with a partial class is rename it. 不幸的是,你不能partial class做的其中一件事就是重命名它。 Of course, you could use xsd to generate it, change the .cs file and don't generate it again , but that is not ideal for maintenance. 当然,您可以使用xsd生成它,更改.cs文件并且不再生成它 ,但这不是维护的理想选择。

As far as I'm aware I don't think this is possible, the class names match almost exactly to whats in the schema. 据我所知,我不认为这是可能的,类名几乎完全匹配模式中的什么。

Personally I would change the class names after XSD has generated the code, but to be honest I usually just stick with what XSD generates. 我个人会在XSD生成代码后更改类名,但说实话我通常只是坚持使用XSD生成的内容。 Its then easier for someone else reading the code to understand what classes map to what parts of the XML. 然后,阅读代码的其他人更容易理解哪些类映射到XML的哪些部分。

Alternatively, if you have control over the schema you could update that? 或者,如果您可以控制架构,则可以更新它?

Any schema with somewhat deep nesting then ends up with utterly useless names. 任何具有深度嵌套的模式都会以完全没用的名称结束。

I don't know of a way to work around the problem, but my tip to at least reduce the negative impact is this: Define a list of aliases for the awfully-named types. 我不知道如何解决这个问题,但我的建议至少减少负面影响是这样的:定义一个非常命名类型的别名列表。 This way you can write code that isn't completely unreadable without losing the ability to regenerate. 这样,您可以编写不完全不可读的代码,而不会失去重新生成的能力。

using AgentAddress = Example.Namespace.DataContract.RootElementNestedElementAgentAddress;
...

It's a pity this list itself has to be copy-pasted to all code files needing it, but I think this at least constitutes an improvement. 遗憾的是,这个列表本身必须被复制粘贴到需要它的所有代码文件中,但我认为这至少构成了一种改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM