看了一些关于这方面的文档,自我总结:

特性(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)的更多相关文章

  1. [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute

    剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...

  2. [C#] C# 知识回顾 - 特性 Attribute

    C# 知识回顾 - 特性 Attribute [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5911289.html 目录 特性简介 使用特性 特性 ...

  3. C# 知识特性 Attribute

    C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用"反射"查询 ...

  4. 区分元素特性attribute和对象属性property

    × 目录 [1]定义 [2]共有 [3]例外[4]特殊[5]自定义[6]混淆[7]总结 前面的话 其实attribute和property两个单词,翻译出来都是属性,但是<javascript高 ...

  5. .Net内置特性Attribute介绍

    特性Attribute概述 特性(Attribute)是一种特殊的类型,可以加载到程序集或者程序集的类型上,这些类型包括模块.类.接口.结构.构造函数.方法.字段等,加载了特性的类型称之为特性的目标. ...

  6. 【点滴积累】通过特性(Attribute)为枚举添加更多的信息

    转:http://www.cnblogs.com/IPrograming/archive/2013/05/26/Enum_DescriptionAttribute.html [点滴积累]通过特性(At ...

  7. 理解特性attribute 和 属性property的区别 及相关DOM操作总结

    查一下英语单词解释,两个都可以表示属性.但attribute倾向于解释为特质,而property倾向于解释私有的.这个property的私有解释可以更方便我们下面的理解. 第一部分:区别点 第一点:  ...

  8. 如何获取类或属性的自定义特性(Attribute)

    如何获取类或属性的自定义特性(Attribute) 问题说明: 在ActiveRecord或者其他的ORM等代码中, 我们经常可以看到自定义特性(Attribute)的存在(如下面的代码所示) [Pr ...

  9. C# 知识特性 Attribute,XMLSerialize,

    C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用“反射”查询特性,获取特性集合方 ...

  10. c#特性attribute:

    特性是被编译到metadata中,  是提供给反射用的. 特性attribute:1 什么是attribute,和注释有什么区别 2 声明和使用attribute3 使用attribute完成扩展4 ...

随机推荐

  1. 从分布式锁来看redis和zookpeer!

    从分布式锁来看redis和zookpeer! 目前网上大部分的基于zookpeer,和redis的分布式锁的文章都不够全面.要么就是特意避开集群的情况,要么就是考虑不全,读者看着还是一脸迷茫.坦白说, ...

  2. MSSQL触发器

    1.触发器语法 CREATE TRIGGER<trigger name> ON [<模式名>.]<表名或视图名> [WITH ENCRYPTION] {{{FOR| ...

  3. c# 文件IO操作 StreamReader StreamWriter Split 使用

    StreamWriter(String,Boolean) 若要追加数据到该文件中,则为 true:若要覆盖该文件,则为 false. 如果指定的文件不存在,该参数无效,且构造函数将创建一个新文件. 例 ...

  4. Ubuntu15.10下***搭建及GUI客户端安装

    1.依赖包安装 sudo apt-get install python-pip python-dev build-essential sudo pip install pip sudo apt-get ...

  5. 剑指offer——扑克牌的顺子

    思想: 1.先将输入的几个数进行排序,sort函数是#include<algorithm>下的. 2.统计0的个数,以及相邻数的差值,比较0的个数及差值的和.看是否可以用大王填充中间的差值 ...

  6. 算法(Algorithms)第4版 练习 1.3.42

    After copy Left on Stack r: be to not or be to Left on Stack copy: be to not or be to After r pop Le ...

  7. Selenium-多窗口处理

    弹出新的窗口,该如何处理 1.获取当前窗口句柄 2.元素的操作,打开新的窗口 3.获取所有窗口句柄 4.for循环遍历所有窗口,定位到需要操作的窗口上 和你当前句柄不一样的就说明是新的,通过打印tit ...

  8. 优秀开源项目之二:流媒体直播系统Open Broadcaster Software

    Open Broadcaster Software(OBS)是一款用于音视频录制和直播的免费开源软件.可以轻松部署到多种平台,目前支持Windows.MAC和Linux. 特性: 1.高性能的实时视频 ...

  9. loj517 计算几何瞎暴力

    在序列上维护4个操作 1.在序列的尾端添加x 2.输出Al~Ar的和 3.将所有数异或x 4.将序列从小到大排序 第一眼看上去是Splay于是头铁硬刚了一发 后来发现splay没法异或 去百度“维护异 ...

  10. ACM学习历程—HDU 3949 XOR(xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...