IContextAttribute特性 为什么会自动执行
时间:2011-12-14
来源:互联网
             C# code
在执行这行代码时,User 类标注了特性:
C# code
AspectOrientedAttribute 特性继承自 IContextAttribute
C# code
当我继承 IContextAttribute 时,AspectOrientedAttribute 会在 new User() 时执行自己的构造函数 和 IContextAttribute.IsContextOK 方法;
如果我取消了继承 IContextAttribute 时,在 new User() 时:AspectOrientedAttribute 却不会自己构造,更不会执行 IsContextOK 方法;
----------------------------------------------------------------------------------
换言之:当 User 的特性 是 IContextAttribute, 则特性将会自动构造执行;
否则却不会;
——凭什么 IContextAttribute 具有自动执行的权利??而其他特性就只能由我们人为的写 代码控制??
            User.CurrentUser = new User(name, tb_Psd.Text.Trim());
在执行这行代码时,User 类标注了特性:
C# code
[AspectOrientedAttribute] class User{ }
AspectOrientedAttribute 特性继承自 IContextAttribute
C# code
public sealed class AspectOrientedAttribute : Attribute, IDisposable//, IContextAttribute
当我继承 IContextAttribute 时,AspectOrientedAttribute 会在 new User() 时执行自己的构造函数 和 IContextAttribute.IsContextOK 方法;
如果我取消了继承 IContextAttribute 时,在 new User() 时:AspectOrientedAttribute 却不会自己构造,更不会执行 IsContextOK 方法;
----------------------------------------------------------------------------------
换言之:当 User 的特性 是 IContextAttribute, 则特性将会自动构造执行;
否则却不会;
——凭什么 IContextAttribute 具有自动执行的权利??而其他特性就只能由我们人为的写 代码控制??
作者: sxl514286339 发布时间: 2011-12-14
             看到了:原来是 User 继承了 : ContextBoundObject——是这个 父类在搞鬼
我是在研究一段AOP代码:刚才 构造 User 时,却先执行了 特性——这就是一种拦截机制的啦;
好了,精简源码也堪上来:
C# code
执行结果 :
特性 构造!
执行 IsContextOK!
执行 GetPropertiesForNewContext!
User 构造!
IContextAttribute自动执行.User
            
            我是在研究一段AOP代码:刚才 构造 User 时,却先执行了 特性——这就是一种拦截机制的啦;
好了,精简源码也堪上来:
C# code
class Program { static void Main(string[] args) { User user = new User(); Console.WriteLine(user.GetType()); } } public class MyAttribute : Attribute, IContextAttribute { public MyAttribute() { Console.WriteLine("特性 构造!"); } public bool IsContextOK(Context ctx, IConstructionCallMessage msg) { Console.WriteLine("执行 IsContextOK!"); return false; } public void GetPropertiesForNewContext(IConstructionCallMessage msg) { Console.WriteLine("执行 GetPropertiesForNewContext!"); } } [MyAttribute] public class User : ContextBoundObject { public User() { Console.WriteLine("User 构造!"); } }
执行结果 :
特性 构造!
执行 IsContextOK!
执行 GetPropertiesForNewContext!
User 构造!
IContextAttribute自动执行.User
作者: sxl514286339 发布时间: 2011-12-14
             各位——结不了帖子;
送分啦!!!!!!
要分的来领取咧......
            送分啦!!!!!!
要分的来领取咧......
作者: sxl514286339 发布时间: 2011-12-14
 相关阅读 更多  
      
    热门阅读
-   office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具 office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具阅读:74 
-   如何安装mysql8.0 如何安装mysql8.0阅读:31 
-   Word快速设置标题样式步骤详解 Word快速设置标题样式步骤详解阅读:28 
-   20+道必知必会的Vue面试题(附答案解析) 20+道必知必会的Vue面试题(附答案解析)阅读:37 
-   HTML如何制作表单 HTML如何制作表单阅读:22 
-   百词斩可以改天数吗?当然可以,4个步骤轻松修改天数! 百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!阅读:31 
-   ET文件格式和XLS格式文件之间如何转化? ET文件格式和XLS格式文件之间如何转化?阅读:24 
-   react和vue的区别及优缺点是什么 react和vue的区别及优缺点是什么阅读:121 
-   支付宝人脸识别如何关闭? 支付宝人脸识别如何关闭?阅读:21 
-   腾讯微云怎么修改照片或视频备份路径? 腾讯微云怎么修改照片或视频备份路径?阅读:28 















