一  Attribute原理:

Attribute注解,是附加上方法、属性、类等上面的标签,
可以通过方法的GetCustomAttribute获得粘贴的这个Attribute对象
通过反射调用到粘贴到属性、方法、类等等的对象
任务:改造ORM
ORM约定:类得名字与表的名字一样,主键必须是Id,
改造一下ORM(类名还可以和表名不一样,Id可以与主键不一样)

//在sayHello方法的描述信息MethodInfo上粘了一个RupengAttribute对象
//注解的值必须是常量,不能是动态算出来的 [RupengAttribute(Name=DateTime.Now.ToString())]
//[RupengAttribute(Name="rupengwnag")]
//一般特性Attribute的类名都以Attribute结尾,这样用的时候就不用谢"Attribute"了

二  Attribute步骤:

1 Attribute本身类
2 标记了Attribute的类
3 通过找到这个类的Attribute标记,从而先去执行上面的Attribute类

三 Attribute示例:

//testAttribute.csProj

namespace testAttribute
{
class Program
{
static void Main(string[] args)
{
Type type = typeof(Person);
//1 获得SayHello()上的Attribute注解对象
object[] objs = type.GetMethod("SayHello").GetCustomAttributes(typeof(AlibabaAttribute), true);
foreach(object obj in objs)
{
AlibabaAttribute ali = obj as AlibabaAttribute;
Console.WriteLine(ali.Name);
} //2 在类中的方法中找到Obsolete注解对象
MethodInfo[] methods = type.GetMethods();
foreach(MethodInfo method in methods)
{
Object[] obsols = method.GetCustomAttributes(typeof(ObsoleteAttribute),true);
if (obsols.Length>)
{
ObsoleteAttribute obsol = obsols[] as ObsoleteAttribute;
Console.WriteLine("不能执行该方法:" + obsol.Message);
}
} //3 获得Love()的Attribute注解对象
object[] objtens = type.GetMethod("Love").GetCustomAttributes(typeof(TengxunAttribute), true);
foreach (object obj in objtens)
{
TengxunAttribute ten = obj as TengxunAttribute;
Console.WriteLine(ten.Name);
} Console.ReadKey();
}
} class Person
{
[Alibaba(Name="阿里人是幸福的")]
public void SayHello()
{
} [Obsolete("这个方法已过时,禁止访问")]
public void F1()
{
} [Tengxun(Name = "腾讯是我的最爱")]
public void Love()
{
}
} //
[AttributeUsage(AttributeTargets.Method)]
class AlibabaAttribute : Attribute
{
public AlibabaAttribute() { }
public AlibabaAttribute(string name)
{
this.Name = name;
}
public string Name { get; set; }
} //
class TengxunAttribute : Attribute
{
public string Name { get; set; }
}
}

四  Attribute应用:

当执行某个action方法时,先获得Attribute对象,用于检查权限

//PermissionActionAttribute.cs

    /// <summary>
/// 权限动作的 Attribute注解
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class PermissionActionAttribute:Attribute
{
public string Name { get; set; }
public PermissionActionAttribute(string name)
{
this.Name = name;
}
}

//BaseController.cs

public class BaseController : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//需要约定方法:public void list(HttpContext context) //方法名和action一致
#region 检查用户是否登录
LoginHelper.CheckHasLogin(context);
#endregion
string action = context.Request["action"];
if(action==null)
{
throw new Exception("未找到action:"+action);
}
Type type = this.GetType(); //this是被new的对象,即子类CategoryController等
//try
//{
MethodInfo method = type.GetMethod(action);
#region /获得该action的 Attribute注解对象
//获得action该方法的 Attribute注解对象(注解对象的Name属性就是权限的名称,用于判断权限)
object[] objAttrs = method.GetCustomAttributes(typeof(PermissionActionAttribute), true);
if (objAttrs.Length > )
{
PermissionActionAttribute paa = objAttrs[] as PermissionActionAttribute;
AdminHelper.CheckHasPower(context, paa.Name);
}
#endregion
method.Invoke(this, new object[] { context }); //调用this对象的method方法 (参数列表是object[],其中一个参数是context)
//}
//catch(Exception ex) //如果找不到这个action方法,就说明这个action是错误的/不存在的
//{
// throw new Exception("未知的action:"+action);
//}
} public bool IsReusable
{
get
{
return true;
}
}
}

//CourseController.cs

            [PermissionAction("新增课程")]
public void addnew(HttpContext context)
{
#region 新增展示
string categoryidStr = context.Request["categoryid"];
int categoryid = VolidHelper.CheckStrToInt(categoryidStr);
TD_COURSE course = new TD_COURSE();
course.VIDEOCATEGORYID = categoryid;
RazorHelper.RazorParse(context, "~/Course/CourseAlter.cshtml", new { action = "addnew", course = course });
#endregion
}

Attribute注解(用于判断权限)的更多相关文章

  1. Shiro入门之二 --------基于注解方式的权限控制与Ehcache缓存

    一  基于注解方式的权限控制 首先, 在spring配置文件applicationContext.xml中配置自动代理和切面 <!-- 8配置自动代理 -->    <bean cl ...

  2. springboot + 拦截器 + 注解 实现自定义权限验证

    springboot + 拦截器 + 注解 实现自定义权限验证最近用到一种前端模板技术:jtwig,在权限控制上没有用springSecurity.因此用拦截器和注解结合实现了权限控制. 1.1 定义 ...

  3. 「踩坑记」Android API 判断权限申请结果的闪退问题

    这几天尝试着用Android Studio写一个小工具的时候遇到了一个动态权限申请的问题.权限的申请使用的语句为: ActivityCompat.requestPermissions(this, ne ...

  4. Assertor用于判断参数和抛出异常

    代码 /// <summary> 断言器,用于判断和抛出异常 /// </summary> static class Assertor { /// <summary> ...

  5. stop() 是用于停止动画 :animated 用于判断动画是否在进行中

    stop() 是用于停止动画 if($("element").is(":animated"))  用于判断动画是否在进行中

  6. JS时间戳比较大小:对于一组时间戳(开始时间~结束时间)和另一组时间戳进行比较,用于判断被比较时间戳组是否在要求范围内

    /* *JS时间戳比较大小:对于一组时间戳(开始时间~结束时间)和另一组时间戳进行比较,用于判断被比较时间戳组是否在要求范围内 *@param date1 date2(形如:'2015-01-01'类 ...

  7. Attribute注解

    class Program { static void Main(string[] args) { //Attribute注解,Attribute是附加到方法.属性.类等上面的特殊的标签,在类Type ...

  8. obj.getClass() == Person.class 用于判断类型

    obj.getClass() == Person.class  用于判断类型

  9. 一个diff工具,用于判断两个目录下所有的改动(比较新旧版本文件夹)

    需求: 编写一个diff工具,用于判断两个目录下所有的改动 详细介绍: 有A和B两个目录,目录所在位置及层级均不确定 需要以B为基准找出两个目录中所有有改动的文件(文件或内容增加.修改.删除),将有改 ...

随机推荐

  1. nginx日志中$request_body 十六进制字符(\\x22) 引号问题处理记录

    在使用nginx记录访问日志时,发现在含有 request_body 的 PUT , POST 请求时,日志中会含有 x22 x9B x5C x09 x08 字符,不利于阅读和处理. 具体 支持 re ...

  2. .NET自带泛型委托方法Func、Action和Predicate

    Func.Action和Predicate是.NET自带的3个泛型委托方法,三个方法的区别其实并不大,要强行给混着用也是可以的,但是我们是有追求的人,把道理讲清楚总是好的. 一.Func是有返回值的方 ...

  3. Spring MVC 接收多个实体参数

    在SpringMVC 的接收参数中,如果接收一个实体对象,只需要在方法参数中这样做:@RequestBody User user //单个的时候这样接收 @RequestMapping(value = ...

  4. Python面向对象的编程注意细节

    和前文一样,这了也是学习过程中,来源于网上各种资料的一个整合记录,希望能够帮到自己和大家: 主要的关注点是在使用class的时候,应该注意的一些细节: 1.在class里面,有了 __init__(s ...

  5. 算法总结之 数组的partition调整 三个值的升序

    给定一个数组arr, 其中只可能有 0,1,2三个值,请实现arr排序 另一种问法: 有一个数组,只有红 蓝 黄 球,请事先红球全放在数组的左边,蓝球放中间,黄球放右边 另一种问法: 有一个数组,再给 ...

  6. LeetCode——same-tree

    Question Given two binary trees, write a function to check if they are equal or not. Two binary tree ...

  7. wareshark网络协议分析之ARP

    一.ARP协议简介 简单的说ARP协议就是实现ip地址到物理地址的映射.当一台主机把以太网数据帧发送到位于同一局域网上的另一台主机时,是根据48bit的以太网地址(物理地址)来确定网络接口的. ARP ...

  8. tp5 数据库Db增删改操作

    添加数据insert $data = [ 'name_cn' => '张三', 'name_en' => 'jack', ]; $res = Db::name('style')->i ...

  9. DB2数据库管理常用操作

    查询db2数据库相关配置(日志,字符集) db2 get db cfg for uppdb 查询db2数据库db2codepage db2set 在进行数据库导入导出的时候,可能要修改db2codep ...

  10. Microsoft Prism安装使用教程 搭建WPF松耦合架构框架

    Microsoft Prism安装使用教程 搭建WPF松耦合架构框架 Prism是由微软Patterns & Practices团队开发的项目,目的在于帮助开发人员构建松散耦合的.更灵活.更易 ...