简体   繁体   English

具有XmlSerializer的DefaultParameterValue属性?

[英]DefaultParameterValue attribute with XmlSerializer?

We're writing a .NET 3.5 application, which uses Xml serialization of various objects. 我们正在编写一个.NET 3.5应用程序,该应用程序使用各种对象的Xml序列化。

We are basically creating an xml document from an object, and i'd like to be able to use this attribute (DefaultParameterValue) which is what .NET 4.0 is using to implement optional arguments. 我们基本上是从一个对象创建一个xml文档,我希望能够使用此属性(DefaultParameterValue),这是.NET 4.0用于实现可选参数的属性。

I'd like the Xml generated document to contain the default values for the parameters that have this attribute. 我希望Xml生成的文档包含具有此属性的参数的默认值。

Is it possible? 可能吗?

XmlSerializer operates at the (public) field/property level. XmlSerializer在(公共)字段/属性级别运行。 In doing so, one of the things it looks at is the similar [DefaultValue(...)] . 这样做的目的之一就是类似的[DefaultValue(...)] It does not look at methods at all , except for a few assistance patterns like ShouldSerialize*() . 它不看方法可言 ,除了少数的援助模式,如ShouldSerialize*() As such, there would seen to be no crossover at all with parameters, ad no need to look at [DefaultParameterValue(...)] . 这样,就可以看到根本没有参数交叉,也不需要查看[DefaultParameterValue(...)]

This is not how attributes work. 属性不是这样工作的。 The ones that xml de/serializers recognize are well documented, the list is not extensible. xml de / serializers可以识别的文档有据可查,该列表不可扩展。 Adding more requires changing code. 添加更多内容需要更改代码。 Code that you cannot change, it is locked up inside a framework assembly. 您无法更改的代码被锁定在框架程序集中。

Implement the equivalent by assigning the default value you want in the class constructor. 通过在类构造函数中分配所需的默认值来实现等效项。

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

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