gpt4 book ai didi

vb.net - 在自定义异常中添加额外属性以返回 AJAX 函数

转载 作者:行者123 更新时间:2023-12-01 23:12:49 24 4
gpt4 key购买 nike

我有一个自定义异常类,如下所示:

<Serializable>
Public Class SamException
Inherits Exception
Public Sub New()
' Add other code for custom properties here.
End Sub
Public Property OfferBugSend As Boolean = True

Public Sub New(ByVal message As String)
MyBase.New(message)
' Add other code for custom properties here.
End Sub

Public Sub New(ByVal message As String, ByVal inner As Exception)
MyBase.New(message, inner)
' Add other code for custom properties here.
End Sub

End Class

我在 AJAX 响应返回的某些情况下使用它。

在我的 AJAX 响应的错误函数中,我确定错误属于我的自定义类型,如下所示:

.... 
ajax code....
.error = function (xhr, text, message) {
var parsed = JSON.parse(xhr.responseText);
var isSamCustomError = (parsed.ExceptionType).toLowerCase().indexOf('samexception') >= 0;
.... etc....

如果错误属于我的自定义类型,这允许我向客户端发回特定响应。

但是...我似乎无法让它将额外的属性 OfferBugSend 发布到客户端,以便 AJAX 代码以不同的方式处理这种情况。

console.log("OfferBugSend: " + parsed.OfferBugSend) 

显示未定义,如果我检查响应,这是因为 xhr.responseText 仅包含属性:

ExceptionType
Message
StackTrace

这些属性来自基类Exception,但它没有传递我的自定义类属性.​​..

我怎样才能做到这一点?

最佳答案

Exception 类是可序列化的,但它包含一个 IDictionary 并实现 ISerializable,这需要做更多的工作来序列化自定义异常类。

处理此问题的更简单方法是利用 Data Exception 类的集合,如下所示:

Public Property OfferBugSend As Boolean
Get
Return Data("OfferBugSend")
End Get
Set(value As Boolean)
Data("OfferBugSend") = value
End Set
End Property

执行此操作的另一种方法是确保您的派生类也实现 ISerializable接口(interface),其中涉及提供序列化构造函数和重写 GetObjectData()。看这个other answer (在 C# 中)作为该方法的基线。

关于vb.net - 在自定义异常中添加额外属性以返回 AJAX 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46279505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com