首页 新闻 赞助 找找看

在ASP.NET里面是否可以用Attribute属性

0
悬赏园豆:5 [已解决问题] 解决于 2010-09-01 13:20

比如

 

[CustomAttribute()]
protected void Page_Load(object sender, EventArgs e)
{
//TO DO

}

 

试过好像不行。

 

@GB的主页 @GB | 初学一级 | 园豆:190
提问于:2010-08-31 22:08
< >
分享
最佳答案
0

attibute实例,是在编译期进行初始化,而不是运行期。所以只能在第一次能看到。

[Custom()]
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
}
[AttributeUsage(AttributeTargets.All)]
public class CustomAttribute : Attribute
{
public CustomAttribute() { HttpContext.Current.Response.Write(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff")); }
}

 

收获园豆:5
慧☆星 | 大侠五级 |园豆:5640 | 2010-09-01 09:07
yes, 怪不得我第一次运行的时候看到,再刷新就没了。。那如何解决?
@GB | 园豆:190 (初学一级) | 2010-09-01 09:16
其他回答(3)
0

可以,

[AttributeUsageAttribute(AttributeTargets.Method)]

public class CustomAttribute:Attribute

{

 

}

通过给 类型 添加 AttributeUsageAttribute 特性来标记自定义的 Attribute 可以应用在哪些类型上面(类、方法、属性、字段、构造函数、事件、接口、参数、返回值、委托等)

HUHU慈悲 | 园豆:9973 (大侠五级) | 2010-09-01 00:26
佛祖你有试过吗? [AttributeUsage(AttributeTargets.All)] public class CustomAttribute : Attribute { public CustomAttribute() { HttpContext.Current.Response.Write(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff")); } } 如下使用根本就没有输出: [Custom()] public partial class _Default : System.Web.UI.Page { [Custom()] protected void Page_Load(object sender, EventArgs e) { } }
支持(0) 反对(0) @GB | 园豆:190 (初学一级) | 2010-09-01 08:42
0

为何要在Page_Load上使用呢?什么目的。

Astar | 园豆:40805 (高人七级) | 2010-09-01 08:36
我不单想在Page_Load事件上使用,还想着Button的Click事件上使用,目的很简单-判断他的权限。 [Custom()] protected void btnUpdate_Click(object sender, ImageClickEventArgs e) { }
支持(0) 反对(0) @GB | 园豆:190 (初学一级) | 2010-09-01 08:44
@gb2312:如果是MVC里,你可以在Controller里给Action做,那还比较现实。
支持(0) 反对(0) Astar | 园豆:40805 (高人七级) | 2010-09-01 08:52
我就是想在ASP.NET里面搞搞MVC上面的这个东东,哈哈,怎知道他不行。
支持(0) 反对(0) @GB | 园豆:190 (初学一级) | 2010-09-01 08:54
attibute实例,是在编译期进行初始化,而不是运行期。所以我想MVC是在调用方法之前做的判断。检查是否有特性,然后再处理吧。所以得在 Httphandler 里面来 拦截处理吧。
支持(0) 反对(0) 慧☆星 | 园豆:5640 (大侠五级) | 2010-09-01 09:30
或者在条用这个方法的第一行用个类封装关于反射属性的功能,做个检查
支持(0) 反对(0) 慧☆星 | 园豆:5640 (大侠五级) | 2010-09-01 09:40
@gb2312:如果做权限,你可以在BLL里做,aspx.cs里的事件模型不行,如果想控制一些操作,可以写一个基类,重写一些事件做操作。
支持(0) 反对(0) Astar | 园豆:40805 (高人七级) | 2010-09-01 09:44
0

找个AOP库试试看呢,比如企业库的PolicyInjection,Spring.net

kyo-yo | 园豆:5587 (大侠五级) | 2010-09-01 09:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册