.NET属性,处理异常 - 使用上的属性访问器 [英] .net Attributes that handle exceptions - usage on a property accessor

查看:121
本文介绍了.NET属性,处理异常 - 使用上的属性访问器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,我从我的asp.net mvc的经验知道,你可以有一个处理异常(HandleErrorAttribute)属性。据我可以告诉控制器类有可能被整合到这一行为的一些onException的事件。但是,我想要做类似的东西在自己的code:

well I know from my asp.net mvc experience that you can have attributes that handle exceptions (HandleErrorAttribute). As far as I can tell the Controller class has some OnException event which may be integral to this behaviour. However, I want to do something similar in my own code:

梦例如:

public String MyProperty
{
    [ExceptionBehaviour(typeof(FormatException), MyExEnum.ClearValue)]
    set
    {
        _thing.prop = Convert.ToThing(value);
    }
}
....

在code以上显然让人很没有意义,但靠近我希望做那种事情。我想对房地产set访问的属性来捕获一些类型的异常,然后处理这个在一些自定义的方式(甚至只是咽下去)。

The code above obviously makes very little sense, but is close to the kind of thing I wish to do. I want the attribute on the property set accessor to catch some type of exception and then deal with this in some custom way (or even just swallow it).

任何想法家伙?

推荐答案

没有,除非你使用PostSharp或类似这是不可能的。这不是一个标准的属性特征 - 而这是一些ASP.NET MVC是添加为您服务。你不明白它的所有属性。

No, that isn't possible unless you use PostSharp or similar. This isn't a standard attribute feature - rather it is something that ASP.NET MVC is adding for you. You don't get it for all attributes.

这篇关于.NET属性,处理异常 - 使用上的属性访问器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆