.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借记卡=~=. 那么在这漫长的清明节假期里,只有学习能满足空虚的内心(划掉 ...
随机推荐
- 文字超出DIV后,隐藏文字并显示...
<html> <head> <style type="text/css"> #cs{width:100px;height:50px;line-h ...
- jQuery邮箱验证正则表达式验证邮箱合法
if($.trim(email)==''||$.trim(email)=='邮 箱:'||$.trim(email)==null){ alert('邮箱不能为空!'); return false ...
- SQLServer中处理每天四亿三千万记录
我是如何在SQLServer中处理每天四亿三千万记录的 首先声明,我只是个程序员,不是专业的DBA,以下这篇文章是从一个问题的解决过程去写的,而不是一开始就给大家一个正确的结果,如果文中有不对的地 ...
- QT添加exe文件的图标LOGO
首先你需要一个ICO文件,这样的一个: 使用百度搜索到的在线工具直接生成一个ICO文件保存到本地就可以了 将这人LOGO.ico文件保存到自己的resource文件夹下 然后在工程中新建一个qrc文件 ...
- leetcode第18题--Letter Combinations of a Phone Number
Problem: Given a digit string, return all possible letter combinations that the number could represe ...
- 安装uBuntu操作系统 - 初学者系列 - 学习者系列文章
uBuntu是一款不错的Linux操作系统,在上面的应用软件不少,就是说它的支持率挺高.下面就对这款操作系统的安装做下介绍. 1. 下载uBuntu安装文件 打开中文页面.http://www.ub ...
- microsoft NLayerApp项目中的层次结构图
microsoft NLayerApp项目中的层次结构图 回到目录 如果你想学好一样东西,一定要看高手是如何做的 如果你想学好.net,一定要看.net framworks源代码 如果你想学好分层结构 ...
- leetcode[164] Maximum Gap
梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...
- Linux下PHP开发环境搭建
平时写程序时都是在服务器已经搭建好的PHP环境进行的.出于对未知知识的好奇,这几天在自己的机器上搭建起了PHP开发环境.本想轻松顺利的看到phpinfo显示在我的页面上,没想到安装环境时一路的erro ...
- 20个很有用的CSS技巧
导语:下面这几个CSS技巧你可能不知道,1.彩色照片变黑白,2.所有元素垂直居中,3.禁用鼠标,4.模糊文字,小编学完能量满满的,觉得对CSS又充满了爱,你也来看看. 1. 黑白图像 这段代码会让你的 ...