.net之特性(Attribute)
看了一些关于这方面的文档,自我总结:
特性(Attribute)就是对一个方法或类做的一个额外的属性说明,也就是附加说明
下面是我自己抄的一个实例程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection; namespace SomeTest
{ class Program
{ static void Main(string[] args)
{
demo d = new demo();
string username = "Lucy";
MethodInfo mi = d.GetType().GetMethod("Test");
if (mi == null) return;
AllowExecuteAttribute att = Attribute.GetCustomAttribute(mi,typeof(AllowExecuteAttribute)) as AllowExecuteAttribute;
if (att == null) return;
if (att.Check(username))
Console.WriteLine("允许执行");
else
Console.WriteLine("不允许执行"); Console.ReadKey();
}
} class demo
{
[AllowExecute("jack,Tom")]
public void Test()
{ }
} /// <summary>
/// 标识某方法允许执行的用户
/// </summary>
public class AllowExecuteAttribute : Attribute
{
/// <summary>
///
/// </summary>
/// <param name="allowedUsers">允许执行的用户名的串联字符串</param>
public AllowExecuteAttribute(string allowedUsers)
{
this._allowedUsers = allowedUsers;
}
private string _allowedUsers; public bool Check(string userName)
{
return this._allowedUsers.ToLower().IndexOf(userName.ToLower()) > -;
}
}
}
下面是公司项目中MVC中对AuthorizeAttribute特性的一些使用方法:
public class SingleUserAuthorize : AuthorizeAttribute
{
[ValidateInput(false)]
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
Hashtable userOnline = (Hashtable)(httpContext.Application["Online"]);
if (userOnline != null)
{
IDictionaryEnumerator idE = userOnline.GetEnumerator();
string strkey = string.Empty;
if (userOnline.Count > )
{
while (idE.MoveNext())
{
//登录时判断保存的session是否与当前页面的session相同
if (userOnline.Contains(httpContext.Session.SessionID))
{
if (idE.Key != null && idE.Key.ToString().Equals(httpContext.Session.SessionID))
{
//判断当前session保存的值是否为被注销值
if (idE.Value != null && "XXXXXX".Equals(idE.Value.ToString()))
{
//验证被注销则清空session
userOnline.Remove(httpContext.Session.SessionID);
httpContext.Application.Lock();
httpContext.Application["Online"] = userOnline;
httpContext.Response.Write("<script>top.location.href='/Home/Message?message=offline';</script>");
httpContext.Response.End();
return false;
}
}
}
else
{
return false;
}
}
return true;
}
else
{
return false;
}
}
return false;
}
}
感觉这个违背了特性的设计初衷啊
.net之特性(Attribute)的更多相关文章
- [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute
剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...
- [C#] C# 知识回顾 - 特性 Attribute
C# 知识回顾 - 特性 Attribute [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5911289.html 目录 特性简介 使用特性 特性 ...
- C# 知识特性 Attribute
C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用"反射"查询 ...
- 区分元素特性attribute和对象属性property
× 目录 [1]定义 [2]共有 [3]例外[4]特殊[5]自定义[6]混淆[7]总结 前面的话 其实attribute和property两个单词,翻译出来都是属性,但是<javascript高 ...
- .Net内置特性Attribute介绍
特性Attribute概述 特性(Attribute)是一种特殊的类型,可以加载到程序集或者程序集的类型上,这些类型包括模块.类.接口.结构.构造函数.方法.字段等,加载了特性的类型称之为特性的目标. ...
- 【点滴积累】通过特性(Attribute)为枚举添加更多的信息
转:http://www.cnblogs.com/IPrograming/archive/2013/05/26/Enum_DescriptionAttribute.html [点滴积累]通过特性(At ...
- 理解特性attribute 和 属性property的区别 及相关DOM操作总结
查一下英语单词解释,两个都可以表示属性.但attribute倾向于解释为特质,而property倾向于解释私有的.这个property的私有解释可以更方便我们下面的理解. 第一部分:区别点 第一点: ...
- 如何获取类或属性的自定义特性(Attribute)
如何获取类或属性的自定义特性(Attribute) 问题说明: 在ActiveRecord或者其他的ORM等代码中, 我们经常可以看到自定义特性(Attribute)的存在(如下面的代码所示) [Pr ...
- C# 知识特性 Attribute,XMLSerialize,
C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用“反射”查询特性,获取特性集合方 ...
- c#特性attribute:
特性是被编译到metadata中, 是提供给反射用的. 特性attribute:1 什么是attribute,和注释有什么区别 2 声明和使用attribute3 使用attribute完成扩展4 ...
随机推荐
- Advanced GET 9.1 修正汉化版(免注册、页面加载、保存都正常)
http://www.55188.com/viewthread.php?tid=2846679 Advanced GET 9.1 修正汉化版(免注册.页面加载.保存都正常) 网上流传的很多GET9.1 ...
- ios 表情编码
感受 :可以做自定义键盘时候用 很方便 还可以在textView里面看到 用户体验很好~ 但是要和服务器管理员协商好,做好解析转码工作,不然网页上是不显示的. ios表情编码 在ios中可以使用可爱 ...
- Python map,reduce,filter,apply
map(function, iterable, ...) map()函数接收两个参数,一个是函数,一个是可迭代的对象,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回. 基本等 ...
- c的详细学习(3)数据的输入输出
c语言没有专门的数据输入输出语句,而是通过调用系统提供的的标准输入/输出库函数来实现数据的输入和输出. (1)数据的输出: 注意:在使用标准的输入输出库函数时,使用编译预处理命令“#inclu ...
- Flask框架的学习与实战(三):登陆管理
继续flask的学习之旅.今天介绍flask的登陆管理模块,还记得上一篇中的blog小项目么,登录是咱们自己写的验证代码,大概有以下几个步骤: 1.在登录框中输入用户名和密码 2.flask view ...
- mac下搭建前端自动化工程
好多年没有接触前端,发现前端行业发生了巨大的变化,很多新鲜术语,比如node.git.grunt.less.sass.预编译.自动化.模块化等等,看得让人晕头转向,不要担心,我会把这之前之后学习成果都 ...
- RabbitMQ之Exchange Direct模式
场景: 生产者发送消息到交换机并指定一个路由key, 消费者队列绑定到交换机时要指定路由key(key匹配就能接受消息,key不匹配就不能接受消息) 例如:我们可以把路由key设置为insert ,那 ...
- mac iterm2 安装 lrzsz rz sz命令
原文:https://blog.csdn.net/jack85986370/article/details/51382077 首先mac自带的终端是不支持lrzsz的,需要下载安装iterm2,下载地 ...
- ubantu删除文件(夹)
格式:rm -rf 目录名字 -r 就是向下递归,不管有多少级目录,一并删除 -f 就是直接强行删除,不作任何提示的意思 名称 rm - 移除文件或者目录 概述 rm [选项]... 文件列表... ...
- Linux课程---9、安装RPM包(RPM的全称是什么)
Linux课程---9.安装RPM包(RPM的全称是什么) 一.总结 一句话总结: redhat package management 1.在Packages中查找和php相关的文件如何查找? ls ...