相关疑难解决方法(0)

如何在C#中序列化Exception对象?

我试图在C#中序列化Exception对象.但是,似乎不可能,因为Exception类没有标记为[Serializable].有办法解决这个问题吗?

如果在执行应用程序期间出现问题,我希望被告知发生的异常.

我的第一反应是序列化它.

.net c# serialization exception

71
推荐指数
7
解决办法
7万
查看次数

在C#中实现自定义异常的行业标准最佳实践是什么?

在C#中实现自定义异常的行业标准最佳实践是什么?

我检查了谷歌,并提出了大量建议,但我不知道哪些建议具有更高的可信度.

如果任何人有权威文章的链接,那也会有所帮助.

c# custom-exceptions

67
推荐指数
3
解决办法
3万
查看次数

55
推荐指数
4
解决办法
4万
查看次数

如果列表/集合为空或为null且无法迭代(不是参数),则抛出什么异常类型?

假设一个简单的示例,其中方法检索集合(例如包含一些配置字符串的列表)并尝试以某种方式检查它:

void Init()
{
    XmlDocument config = new XmlDocument();
    config.Load(someXml);
    var list = config.SelectNodes("/root/strings/key"); // Normally, list should not be null or empty

    if (list == null || list.Count == 0)
        throw new SomeExceptionType(message);   // What kind of exception to throw?

    // Iterate list and process/examine its elements
    foreach (var e in list) ...
}
Run Code Online (Sandbox Code Playgroud)

在此特定实例中,如果未检索到任何内容,则该方法无法正常继续.我不确定在这种情况下抛出什么异常类型.据我所知,我的选择是:

  • 手动抛出任何东西,让它NullReferenceException自动抛出(不处理空列表情况),

  • 抛出自定义异常类型(可能不是一个好主意,因为我不期望调用者会尝试对异常做任何事情,即他不会寻找一个特殊的异常类型来处理),

  • 做点什么?

c# exception-handling

13
推荐指数
2
解决办法
1万
查看次数

在Custom Exception中添加额外的属性以返回AJAX功能

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

<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
Run Code Online (Sandbox Code Playgroud)

我在AJAX响应的返回中使用它来处理某些情况.

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

.... 
ajax code....
.error = function (xhr, text, message) { …
Run Code Online (Sandbox Code Playgroud)

vb.net exception-handling exception custom-errors custom-error-handling

12
推荐指数
1
解决办法
132
查看次数

带属性的自定义异常

经过一些研究后,我发现自定义异常应如下所示:

using System;
using System.Runtime.Serialization;

namespace YourNamespaceHere
{
    [Serializable()]
    public class YourCustomException : Exception, ISerializable
    {
        public YourCustomException() : base() { }
        public YourCustomException(string message) : base(message) { }
        public YourCustomException(string message, System.Exception inner) : base(message, inner) { }
        public YourCustomException(SerializationInfo info, StreamingContext context) : base(info, context) { }
    }
}
Run Code Online (Sandbox Code Playgroud)

但我有小问题.

我希望上面的例外有两个额外的字段,比如说int IDint ErrorCode.如何添加这两个字段并初始化它们 - 我应该添加一个新的构造函数,这两个参数和消息参数?

你也可以帮助我并展示如何为这个具有两个新属性的新类编写序列化方法吗?

谢谢.

.net c#

7
推荐指数
1
解决办法
7337
查看次数

错误S3925:更新"ISerializable"的此实现以符合推荐的序列化模式

我有以下用编写的代码.

自从我将NuGet SonarAnalyzer.CSharp软件包升级到版本6.0.0.2033后,我遇到了这个错误:

错误S3925更新"ISerializable"的此实现以符合推荐的序列化模式.

public class GenericException : Exception
{
    public GenericException(string message) : base(message)
    {
        // ...
    }

    public GenericException(string message, Exception innerException) : base(message, innerException)
    {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

有人有想法吗?

c# sonarlint visual-studio-2017

7
推荐指数
0
解决办法
3259
查看次数

可ISerializable和向后兼容性

我必须使用一个旧的应用程序,它使用binaryFormatter将应用程序数据序列化到文件流中(例如在一个名为"data.oldformat"的文件中),而没有任何优化主类已经标记了属性

<serializable()>public MainClass
....... 
end class
Run Code Online (Sandbox Code Playgroud)

和序列化代码

dim b as new binaryformatter
b.serialize(mystream,mymainclass)
Run Code Online (Sandbox Code Playgroud)

为了优化序列化/反序列化过程,我简单地使类实现了ISerializable接口并编写了一些优化的序列化例程

<serializable()>public MainClass
       implements ISerializable
....... 
end class
Run Code Online (Sandbox Code Playgroud)

优化工作非常好但我必须找到一种方法来恢复旧文件中的数据以实现向后兼容.

我怎样才能做到这一点??

皮耶路易吉

.net backwards-compatibility binaryformatter iserializable

6
推荐指数
1
解决办法
1798
查看次数

使用Server.TransferRequest()传递视图模型

我正在尝试微调我的MVC应用程序中的错误处理.

我在web.config中启用了自定义错误,并添加了以下代码Application_Error.

Global.asax中

protected void Application_Error(object sender, EventArgs e)
{
    Exception exception = Server.GetLastError() as Exception;
    if (exception != null)
    {
        Context.ClearError();
        Context.Response.TrySkipIisCustomErrors = true;

        string path = (exception is HttpException && (exception as HttpException).GetHttpCode() == 404) ?
            "~/Error/NotFound" :
            "~/Error/Index";
        Context.Server.TransferRequest(path, false);
    }
}
Run Code Online (Sandbox Code Playgroud)

ErrorController.cs

[AllowAnonymous]
public ActionResult Index()
{
    Response.Clear();
    Response.StatusCode = 503;
    Response.TrySkipIisCustomErrors = true;
    return View();
}

[AllowAnonymous]
public ActionResult NotFound()
{
    Response.Clear();
    Response.StatusCode = 404;
    Response.TrySkipIisCustomErrors = true;
    return View();
} …
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net error-handling asp.net-mvc

6
推荐指数
1
解决办法
1312
查看次数