C#枚举属性null与0

时间:2009-01-17 00:46:57

标签: c# web-services serialization enums null

我正在使用IIS / asmx来支持Flash客户端。我的一些服务层数据传输对象具有枚举值属性。在某些情况下,这些属性应为null。

当这样的枚举属性具有null值的对象呈现为soap时,我收到此错误:

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Instance validation error: '0' is not a valid value for NameSpaceX.Model.NodeType.
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_NodeType(NodeType v)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write4_PackageDTO(String n, String ns, PackageDTO o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write15_ArrayOfPackageDTO(Object o)
   at Microsoft.Xml.Serialization.GeneratedAssembly.ListOfPackageDTOSerializer1.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
   at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
   at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
   at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
   at System.Web.Services.Protocols.WebServiceHandler.Invoke()

有没有更好的解决方案来创建值为0的枚举成员(例如[0,“null”]?我怀疑我在这里缺乏基本知识。

感谢。

1 个答案:

答案 0 :(得分:4)

我的第一个倾向是指向Nullable<T>类型。但是,在查看有关Nullable<T>和SOAP的文档后,似乎可能不支持它。您可能希望进一步探讨这一点。

也就是说,Enums宣布NotSet = 0成员是很常见的。