.Net里的Attribute 学习
.Net里的Attribute 学习
前两天看到书里边讲Attribute定制,结合了网上的资料,自己做了简单的登录功能,并结合了某些设计模式,有兴趣的朋友可以看下。由于时间原因,没有做过多的说明,直接上代码,希望能帮助哪些不会的初学者,同时也希望得到高人的指点,本人将虚心接受批评,谢谢!
用户登录操作类:
using System;
using LemonFreamworkAOP.User;
using LemonFreamworkAOP.AOP; namespace LemonFreamworkAOP.User
{
[LoginAOP]
public class UserDAL : ContextBoundObject, IUser
{
private UserInfo userInfo; public UserDAL(UserInfo userInfo)
{
this.userInfo = userInfo;
}
public Boolean IsLogin(UserInfo user)
{
return this.userInfo == user;
}
}
}
下边是几个核心抽象基类类:
using System;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging; namespace LemonFreamworkAOP.AOP
{
public abstract class AOPProperty : IContextProperty, IContributeObjectSink
{
protected virtual String GetName
{
get { return "AOP"; }
} protected virtual Boolean IsOK(Context newCtx)
{
return true;
} protected abstract IMessageSink GetSink(IMessageSink nextSink); #region IContextProperty IContributeObjectSink
public void Freeze(Context newContext)
{ } public Boolean IsNewContextOK(Context newCtx)
{
return IsOK(newCtx);
} public String Name
{
get { return GetName; }
} public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink nextSink)
{
return GetSink(nextSink);
}
#endregion
}
}
using System;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging; namespace LemonFreamworkAOP.AOP
{
public abstract class AOPAttribute : ContextAttribute
{
public AOPAttribute() : base("AOP") { } public sealed override void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg)
{
ctorMsg.ContextProperties.Add(CreateAOPProperty());
} protected abstract AOPProperty CreateAOPProperty();
}
}
using System;
using System.Collections.Generic;
using LemonFreamworkAOP.User;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging; namespace LemonFreamworkAOP.AOP
{
public abstract class AOPSink : IMessageSink
{
private Dictionary<String, Object> beforeHandles;
private Dictionary<String, Object> afterHandler;
private IMessageSink messageSink;
protected const String errorDesc = "Not Find MethodName !"; public AOPSink(IMessageSink messageSink)
{
this.beforeHandles = new Dictionary<String, Object>();
this.afterHandler = new Dictionary<String, Object>();
this.messageSink = messageSink;
AddAllBeforeAOPHandle();
AddAllAfterAOPHandle();
} public virtual void AddBeforeAOPHandle(String methodName, BeforeAOPHandle beforeHandle)
{
if (String.IsNullOrEmpty(methodName)) return;
if (!beforeHandles.ContainsKey(methodName))
beforeHandles.Add(methodName, beforeHandle);
} public virtual void AddAfterAOPHandle(String methodName, AfterAOPHandle aftereHandle)
{
if (String.IsNullOrEmpty(methodName)) return;
if (!afterHandler.ContainsKey(methodName))
afterHandler.Add(methodName, aftereHandle);
} public virtual BeforeAOPHandle FindBeforeAOPHandle(String methodName)
{
if (beforeHandles.ContainsKey(methodName))
return beforeHandles[methodName] as BeforeAOPHandle;
else
throw new Exception(errorDesc);
} public virtual AfterAOPHandle FindAfterAOPHandle(String methodName)
{
if (afterHandler.ContainsKey(methodName))
return beforeHandles[methodName] as AfterAOPHandle;
else
throw new Exception(errorDesc);
} public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
{
return null;
} public IMessageSink NextSink
{
get { return this.messageSink; }
} public IMessage SyncProcessMessage(IMessage msg)
{
IMethodCallMessage callMsg = msg as IMethodCallMessage;
BeforeProcess(callMsg); IMessage imsg = messageSink.SyncProcessMessage(msg); //IMethodReturnMessage retMsg = imsg as IMethodReturnMessage;
//AfterProcess(callMsg.MethodName, retMsg); return imsg;
} protected abstract void AddAllBeforeAOPHandle();
protected abstract void AddAllAfterAOPHandle(); protected virtual void BeforeProcess(IMethodCallMessage callMsg)
{
String methodName = callMsg.MethodName;
BeforeAOPHandle beforeHandle = FindBeforeAOPHandle(methodName);
if (beforeHandle != null)
beforeHandle(callMsg);
} protected virtual void AfterProcess(String methodName, IMethodReturnMessage retMsg)
{
AfterAOPHandle afterHandle = FindAfterAOPHandle(methodName);
if (afterHandle != null)
afterHandle(retMsg);
}
}
}
以下是登录功能的派生子类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace LemonFreamworkAOP.AOP
{
[AttributeUsage(AttributeTargets.Class)]
public class LoginAOPAttribute : AOPAttribute
{
protected override AOPProperty CreateAOPProperty()
{
return new LoginAOPProperty();
}
}
}
using System;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging; namespace LemonFreamworkAOP.AOP
{
public class LoginAOPProperty : AOPProperty
{
protected override IMessageSink GetSink(IMessageSink nextSink)
{
return new LoginAOPSink(nextSink);
}
}
}
using System;
using LemonFreamworkAOP.User;
using LemonFreamworkAOP.Untity;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging; namespace LemonFreamworkAOP.AOP
{
public class LoginAOPSink : AOPSink
{
public LoginAOPSink(IMessageSink messageSink) : base(messageSink) { } public void BeforeUserLogin(IMethodCallMessage callMsg)
{
UserInfo loginUser = callMsg.GetArg(0) as UserInfo;
UserInfo configUser = ConfigHelper.GetUser;
if (loginUser.UserName != configUser.UserName || loginUser.Password != configUser.Password)
throw new Exception("用户名或密码不正确!");
} protected override void AddAllBeforeAOPHandle()
{
AddBeforeAOPHandle("IsLogin", BeforeUserLogin);
} protected override void AddAllAfterAOPHandle()
{ }
}
}
.Net里的Attribute 学习的更多相关文章
- 代码走查25条疑问 C# 跳转新的标签页 C#线程处理 .Net 特性 attribute 学习 ----自定义特性 看懂 ,学会 .NET 事件的正确姿势-简单版
代码走查25条疑问 代码走查(Code Review) 是一个开发人员与架构师集中讨论代码的过程.通过代码走查可以提高代码的 质量,同时减少Bug出现的几率.但是在小公司中并没有代码走查的过程在这 ...
- C# Attribute学习
由于项目中需要使用到序列化相关的技术,从而想到是否可以使用C#中的特性,特此花了近两小时学习了一下. 对于特性的学习,主要参考了两篇博文,特此感谢.以下附链接: http://www.cnblogs. ...
- .Net 特性 attribute 学习 ----自定义特性
什么是特性? [Obsolete("不要用无参构造函数",true)] 放在方式上, 该方法就不能使用了 [Serializable]放在类上面.该类就是可以序列化和反序列化使用 ...
- Qt 的线程与事件循环——可打印threadid进行观察槽函数到底是在哪个线程里执行,学习moveToThread的使用)
周末天冷,索性把电脑抱到床上上网,这几天看了 dbzhang800 博客关于 Qt 事件循环的几篇 Blog,发现自己对 Qt 的事件循环有不少误解.从来只看到现象,这次借 dbzhang800 的博 ...
- C#特性Attribute学习
起初一直纠结于如何调用特性附着在下面那个成员的值,后来发现不需要调用,通过反射加载的时候是自动绑定上去的,即 获得成员对象之后,有一个方法可以获得特性标签. 其实从类库提供者,和类库使用者的角度,分开 ...
- laravel里的队列学习
首先,我们要搞明白几个概念,从小到大依次有:队列任务,队列,连接. 他们属于依次被包含的关系,一个队列里有许多的队列任务,一个连接中可以有许多队列. 队列任务:对每个用户都会进行的操作,理解为队列任务 ...
- 用python制作文件搜索工具,深挖电脑里的【学习大全】
咳咳~懂得都懂啊 点击此处找管理员小姐姐领取正经资料~ 开发环境 解释器: Python 3.8.8 | Anaconda, Inc. 编辑器: pycharm 专业版 先演示效果 开始代码,先导入模 ...
- odoo里的javascript学习---自定义插件
插件效果图 定义js odoo.define('auto_widget',function(require){ "use strict"//通过扩展AbstractField来扩展 ...
- [0406]学习一个——Unit 1 Html、CSS与版本控制
前言 最近发现了Github的Student认证,本来想用来注册Digital Ocean搭个梯子,结果注册验证不能用VISA借记卡=~=. 那么在这漫长的清明节假期里,只有学习能满足空虚的内心(划掉 ...
随机推荐
- MVC生成CheckBoxList并对其验证
原文:MVC生成CheckBoxList并对其验证 通过扩展方法,可以让CheckBox水平排列,生成CheckBoxList,正如"MVC扩展生成CheckBoxList并水平排列&quo ...
- mysql更改数据文件夹步骤与错误(ERROR 2002 (HY000))处理方法
1,关闭mysql服务: service mysqld stop 2,创建新建的文件夹 mkdir -p data 3,把曾经的文件夹转移到新的数据文件夹 mv /var/lib/mysql/ /da ...
- 企业办公即时通信软件TeamTalk
TeamTalk 一键部署方案:TTAutoDeploy TeamTalk 整套服务提供模块部署脚本和一键部署方案,主要模块有JDK,NGINX,PHP,PERCONA(MYSQL), REDIS ...
- A在SP.NET跨页多选
在ASP.NET跨页多选 本文介绍怎样在ASP.NET中实现多页面选择的问题.其详细思路非常easy:用隐藏的INPUT记住每次选择的项目,在进行数据绑定时.检查保存的值,再在DataGrid中进行选 ...
- lua迭代器和仿制药for
不管是什么样的结构,你只需要同意遍历集合可以称为迭代器的所有元素.lua常用来形容叙事功能迭代器.个元素.每个迭代器都须要保存一些状态来知道当前处于什么位置和怎样进行下一次迭代. 对于这种任务.闭包提 ...
- Hadoop -YARN 应用程序设计概述
一概述 应用程序是用户编写的处理数据的统称,它从YARN中申请资源完毕自己的计算任务.YARN自身相应用程序类型没有不论什么限制,它能够是处理短类型任务的MapReduce作业,也能够是 ...
- Scala从零开始:使用Intellij IDEA写hello world
Scala从零开始:使用Intellij IDEA写hello world 分类: Scala |2014-05-23 00:39 |860人阅读 引言 在之前的文章中,我们介绍了如何使用Scal ...
- 1951: [Sdoi2010]古文字猪
1951: [Sdoi2010]古代猪文 链接:Click Here~ 题目: 一道非常好的组合数学题.!!.题目非常长.只是就以下几段话实用. iPig认为仅仅要符合文献,每一种能整除N的k都是有可 ...
- w5cValidator【AngularJS】 2.0 版本发布
w5cValidator 插件基于angular原有的表单验证,在原有的基础上扩展了一些错误提示的功能,让大家不用在每个表单上写一些提示信息的模板,专心的去实现业务逻辑. 代码地址:https://g ...
- Javascript多线程引擎(四)
Javascript多线程引擎(四)--之C语言单继承 因为使用C语言做为开发语言, 而C语言在类的支持方面几乎为零, 而Javascript语言的Object类型是一个非常明显的类支持对象,所以这里 ...