.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借记卡=~=. 那么在这漫长的清明节假期里,只有学习能满足空虚的内心(划掉 ...
 
随机推荐
- <<Python基础课程>>学习笔记 | 文章13章 | 数据库支持
			
备注:本章介绍了比较简单,只是比较使用样品,主要假设是把握连接,利用数据库.和SQLite做演示样本 ------ Python数据库API 为了解决Python中各种数据库模块间的兼容问题,如今已经 ...
 - Django----模板
			
为了将html页面和python代码分离开来,有必要介绍一下模板的作用,Django中自带很多模板. 以下一个html模板文件: <html> <head><title& ...
 - Python系列教程大汇总
			
Python初级教程 Python快速教程 (手册) Python基础01 Hello World! Python基础02 基本数据类型 Python基础03 序列 Python基础04 运算 Pyt ...
 - ORACLE 中极易混淆的几个 NAME 的分析和总结
			
我们知道,Oracle中的各种NAME会在我们的各个配置文件里常常出现,大致有下面这些: 在init.ora中有DB_NAME,INSTANCE_NAME,SERVICE_NAME 配置DG的时候,为 ...
 - java设计模式之二抽象工厂模式(Abstract Factory)
			
工厂方法模式有一个问题就是,类的创建依赖工厂类,也就是说,如果想要拓展程序,必须对工厂类进行修改,这违背了闭包原则,所以,从设计角度考虑,有一定的问题,如何解决?就用到抽象工厂模式,创建多个工厂类,这 ...
 - 1.3 LINQ查询
			
LINQ最具突破性的优势在于将文本查询与对象操作完美集成,它让查询数据和操作对象一样安全和轻松.查询(Query)是LINQ的核心概念之一. 传统意义上的数据查询语言,通常是比较易懂,具有一定语义的文 ...
 - 小公司免费的ERP软件
			
http://www.2bizbox.cn/ https://www.odoo.com/
 - 常用Jquery插件整理大全
			
做项目的时候总是少不了要用到Jquery插件,但是Jquery插件有太多,每次都要花费一些时间,因此本人就抽时间整理了一些Jquery插件,每个插件都有Demo或者是使用文档供大家下载.整理了一晚上才 ...
 - Katana介绍以及使用
			
Katana介绍以及使用 接上篇OWIN产生的背景以及简单介绍,在了解了OWIN规范的来龙去脉后,接下来看一下Katana这个OWIN规范的实现,并看看如何使用在我们的Web开发中. 阅读目录: 一. ...
 - android 实现分享功能两种方法
			
当我想做一个智能的记事本的时候,我就在尝试自己写一组分享功能.后来才知道,原来每个社交软件中都有自己的分享接口. 这就大大减少了我们的代码量了. 第一种方法:特点--简单 package com.ex ...