.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 ...
随机推荐
- (转)AAC ADTS格式分析
1,ADTS是个啥ADTS全称是(Audio Data Transport Stream),是AAC的一种十分常见的传输格式记得第一做demux的时候,把AAC音频的ES流从FLV封装格式中抽出来送给 ...
- HTML 获取屏幕,浏览器,页面的高度
1,物理尺寸和分辨率 容器的尺寸是指当前分辨率下的高度.宽度,而不是物理高度.宽度. 如:一个22寸的显示器,屏幕分辨率为1366 * 768,那么获取到的屏幕高度为1366px,宽度为768px. ...
- 每天一个Linux命令(33)cal命令
cal命令用于显示当前日历,或者指定日期的日历. (1)用法: 用法: cal [选项] [[[日] 月] 年] (2)功能: 功能: 用于查看日历等时间信息,如只有一 ...
- Linux LVM管理
创建和管理LVM 要创建一个LVM系统,一般需要经过以下步骤: 1. 创建分区 fdisk /dev/sdb 使用分区工具(如:fdisk等)创建LVM分区,方法和创建其他一般分区的方式是一样的,区别 ...
- hdu 4542 小明系列故事——未知剩余系 反素数 + 打表
小明系列故事——未知剩余系 Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Prob ...
- C语言中的位操作(16)--计算二进制数字尾部连续0的数目
本篇文章介绍计算二进制数字尾部连续0的数目的相关算法,例如:v=(1101000)2,该数尾部连续0的数目=3 方法1:线性时间算法 unsigned int v; // 需要计算的目标整数 int ...
- Spark- 流量日志分析
日志生成 package zx.Utils import java.io.{File, FileWriter} import java.util.Calendar import org.apache. ...
- CSS: iPhone Custom CSS
1. [代码][CSS]代码 <style type="text/css" media="screen"> /* iPhone 4@media on ...
- CSS重置 reset.css
1. [文件] reset.css ~/*------------------------------------------* site:ifnoif.net* Style author:ifnoi ...
- Unity3D之Mesh(五)绘制圆
前言: Unity3D中Mesh的基本单位是三角形,而圆形就是由许许多多的三角形组成的.那么我们就知道了绘制圆形的Mesh需要两个变量:圆的半径 以及分割数: 一.实现过程 基本过程与之前的类似,最 ...