带属性的自定义异常

时间:2015-10-09 06:41:02

标签: c# .net

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

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) { }
    }
}

但我有小问题。

我希望上面的例外有两个额外的字段,比如int IDint ErrorCode。如何添加这两个字段并对其进行初始化 - 我是否应该使用这两个参数和消息参数添加 new 构造函数?

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

谢谢。

1 个答案:

答案 0 :(得分:8)

它看起来像这样。 您可以在此处查找更多详细信息What is the correct way to make a custom .NET Exception serializable?

As of Spring Security 4.0, CSRF protection is enabled by default with XML 
configuration. If you would like to disable CSRF protection, the corresponding 
XML configuration can be seen below.

<http>
    <!-- ... -->
    <csrf disabled="true"/>
</http>

CSRF protection is enabled by default with Java configuration. If you would like to disable 
CSRF, the corresponding Java configuration can be seen below. Refer to the Javadoc of csrf() 
for additional customizations in how CSRF protection is configured.

@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable();
    }
}