.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 ...
随机推荐
- C#使用SQL语句时候的万用密码问题
实际上万用密码就是因为SQL里面的语句--是注释,利用bug添加用户名和密码. 例如,用户名为 adada’ or 1=1-- 第一个种写法登录成功了 第二种写法登录失败(正确) 第三种写法登录失败( ...
- LeetCode:贪婪算法
LeetCode:贪婪算法 贪婪算法基础 我 717. 1-bit and 2-bit Characters class Solution { public boolean isOneBitChara ...
- python微信库 --- itchat
python实现微信接口——itchat模块 安装 pip install itchat 登录 itchat.auto_login() # 这种方法将会通过微信扫描二维码登录,但是这种登录的方式确实短 ...
- svn及git使用笔记
这周发生好几件大事: 谷歌发布SHA-1安全加密碰撞实例 Cloudflare 泄露网络会话中的加密数据 linux内核漏洞 CVE-2017-6074 加密在网络中越来越受关注,目前github的提 ...
- 剑指offer之 奇数偶数数组位置调整且保存顺序不变
public class Solution { public void reOrderArray(int [] array) { reOrderCore(array,array.length); } ...
- java深入探究06
Tomcat中获取资源文件: ServletContext().getRealPath(/WEB-INF/classes/db.properties);//获取资源文件的在服务器中的绝对路径 Serv ...
- 升级到JDK8,编译时发生 Error:java: java.lang.ExceptionInInitializerError
编译的时候出现这个问题.使用1.7的jdk没问题,但是由于po主的项目中,使用了java8编写的代码,解决java8兼容问题成为解决这个问题的首选方案. 这个日志太过简单,只告知一个异常信息,这个异常 ...
- linux新增用户和删除用户
新增用户 新增用户命令:useradd 参数: 参数 说明 -u 指定UID,也就是自定义UID -g 知道GID,也就是初始化用户组,/etc/passwd文件中的第四个字段. -G 后面接用户组的 ...
- The Quantum L3 router and floating IPs
This post shows how the Quantum L3 Agent uses the Linux IP stack to implement the Quantum L3 Routing ...
- Codeforces 158E Phone Talks:dp
题目链接:http://codeforces.com/problemset/problem/158/E 题意: 你有n个电话要接,每个电话打进来的时刻为第t[i]分钟,时长为d[i]分钟. 每一个电话 ...