C# 枚举属性 null 与 0

发布于 2024-07-12 11:18:46 字数 1907 浏览 5 评论 0原文

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

当此类枚举属性具有空值的对象呈现给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”]?我怀疑我我这里缺乏基本知识

I'm using IIS/asmx to support a Flash client. Some of my service layer data transfer objects have properties that are enumeration values. There are cases where these properties should be null.

When an object with a null value for such an enumeration property is rendered to soap, I receive this error:

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()

Is there a better solution then to create an enumeration member with value 0 (e.g. [0, "null"]? I suspect that I'm lacking basic knowledge here.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑叹一世浮沉 2024-07-19 11:18:46

我的第一个想法是向您指出 Nullable 类型。 但是,在查看有关 Nullable 和 SOAP 的文档后,发现它可能不受支持。 您可能想进一步探索这一点。

也就是说,枚举声明 NotSet = 0 成员是很常见的。

My first inclination was to point you to the Nullable<T> type. However, after looking around for documentation on Nullable<T> and SOAP, it appears that it might not be supported. You might want to explore this further.

That said, it is very common for Enums to declare a NotSet = 0 member.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文