这个框框好烦人啊,删不掉

一、背景

[serializable]
public class Person(){}

这是我第一次看到特性(Attribute),那时我还不知道这是什么,怎么会有这种写法,让我纠结了很久,我只知道,加了这个后,这个类就可以序列化了。

二、使用

定义自己的Attribute

//自定义特性,跟普通的类差不多,继承Attribute,
//这个类中有2个构造函数,和一个属性
public class MyClassAttribute:Attribute
{
public MyClassAttribute(){}
public MyClassAttribute(string text)
{
MyProperties=text;
Console.WriteLine("你输入的参数为:"+text);
}
public string MyProperties{get;set;}
} //这里定义了两个特性,用与后面测试
public class MyMethodAttribute:Attribute
{
public MyMethodAttribute(){}
public MyMethodAttribute(string text)
{
MyProperties=text;
Console.WriteLine("你输入的参数为:"+text);
}
public string MyProperties{get;set;}
}

②如何使用特性:

//特性使用很简单,就是在想使用的地方用一个个中括号引用就可以了
[TestClassAttribute("特性使用在类上")]
public class Test
{
[TestMethodAttribute("特性使用在方法上")]
public void TestAccess()
     {
      Console.WriteLine("这是一个测试方法");
     }
}

③测试

class Program
{
static void Main(string[] args)
{
Test testData=new Test();
TestData.TestAccess();
}
} 输出结果:
这是一个测试方法

结论:
附加特性的类或方法,在运行时不会受特性影响(如果该特性没做什么事)
它只是在类或方法上多加了一段提示信息,类似于一段注释代码,不过用IL反汇编程序(开始菜单中搜索IL),可以看出,特性被编译到dll中,而注释是没有这个待遇的

④获取特性的值

public class GetValueFromMyAttribute
{
public static void Run()
{
bool isDefinedMethodAttribute = Attribute.IsDefined(typeof(Test).GetMethod("TestAccess"), typeof(MyMethodAttribute));
bool isDefinedClassAttribute = Attribute.IsDefined(typeof(Test), typeof(MyClassAttribute)); if (isDefinedMethodAttribute)
{
MyMethodAttribute attr = Attribute.GetCustomAttribute(typeof(Test).GetMethod("TestAccess"), typeof(MyMethodAttribute)) as MyMethodAttribute; Console.WriteLine(attr.MyProperties);
     }      if (isDefinedClassAttribute)
     {
      TestClassAttribute attr = Attribute.GetCustomAttribute(typeof(Test), typeof(MyClassAttribute)) as MyClassAttribute;       Console.WriteLine(attr.MyProperties);
     }
  }
}

⑤测试

class Program
{
static void Main(string[] args)
{
Test testData=new Test();
TestData.TestAccess();
     GetValueFromMyAttribute.Run();
  }
} 输出结果:
这是一个测试方法
你输入的参数为:特性使用在方法上(构造函数)
特性使用在方法上(获取到的值)
你输入的参数为:特性使用在类上
特性使用在类上

三、系统中常用的Attribute

除了一开始出现Serializable、后来还遇到过HttpPost、HttpGet、AjaxPro.AjaxMethod()等等

DllImport:导入第三方dll

class InCommonUse
{
[DllImport("User32.dll")]
public static extern in MessageBox(int hParent,string message,string caption,int type); public static void Test()
{
MessageBox(,"外部导入了User32.dll文件","提示",)
Console.Readkey();
}
}

Conditional:指定模式下运行

[Conditional("DEBUG")]
private static void TestConditional()
{
  Console.WriteLine("只在debug模式下运行");
}

Obsolete:过时提示

这个估计大部分人都遇到过

这次就写到这里了,等待后续学习

四、总结


文章总得有个结尾,前几天考四级,学到了一句,

According what has been discussed above, this is a good thing, hereby, write a blog to record this important moment

学习笔记--C#特性Attribute(一)的更多相关文章

  1. 【ASP.NET MVC 学习笔记】- 02 Attribute

    本文参考:http://www.cnblogs.com/willick/p/3208427.html 1.特性(Attribute)对程序中的元素进行标注,比如类.字段.方法.属性等. 2.在.NET ...

  2. openrisc 之 Wishbone总线学习笔记——总线特性

    特性: 一,互联方式: 支持点到点.共享总线.十字交叉(Crossbar)和基于交换结构(Switch fabric)的互联. 二,数据操作方式:单次读/写操作.块读/写操作,读改写(RMW,Read ...

  3. 转:C#制作ORM映射学习笔记一 自定义Attribute类

    之前在做unity项目时发现只能用odbc连接数据库,感觉非常的麻烦,因为之前做web开发的时候用惯了ORM映射,所以我想在unity中也用一下ORM(虽然我知道出于性能的考虑这样做事不好的,不过自己 ...

  4. javascript 学习笔记 三大特性

    <script type="text/javascript"> //封装 function Person (name,age,sal){ this.name=name; ...

  5. ArcGIS API for JavaScript 4.2学习笔记[0] AJS4.2概述、新特性、未来产品线计划与AJS笔记目录

    放着好好的成熟的AJS 3.19不学,为什么要去碰乳臭未干的AJS 4.2? 4.2全线基础学习请点击[直达] 4.3及更高版本的补充学习请关注我的博客. ArcGIS API for JavaScr ...

  6. angular学习笔记(1)- 四大核心特性

    angular1学习笔记(1) -  angular1四大核心特性 1.MVC model:数据模型层 controller:业务逻辑和控制逻辑 view:视图层,负责展示 2.模块化 Module ...

  7. 代码走查25条疑问 C# 跳转新的标签页 C#线程处理 .Net 特性 attribute 学习 ----自定义特性 看懂 ,学会 .NET 事件的正确姿势-简单版

    代码走查25条疑问   代码走查(Code Review) 是一个开发人员与架构师集中讨论代码的过程.通过代码走查可以提高代码的 质量,同时减少Bug出现的几率.但是在小公司中并没有代码走查的过程在这 ...

  8. 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性

    基于.net的分布式系统限流组件   在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...

  9. C++ 学习笔记(一些新特性总结3)

    C++ 学习笔记(一些新特性总结3) public.protected 和 private 继承 public 继承时,基类的存取限制是不变的. class MyClass { public: // ...

随机推荐

  1. [译] 使用Using Data Quality Services (DQS) 清理用户数据

    SQL Server 2012 Data Quality Services (DQS)  允许你使用自己的知识库来清洗数据. 在本文中我会展示一个简单示例. 使用DQS清理步骤如下: A. 建立DQS ...

  2. Oracle中针对中文进行排序[Z]

    在oracle 9i之前,对中文的排序,是默认按2进制编码来进行排序的. 9i时增加了几种新的选择: 按中文拼音进行排序:SCHINESE_PINYIN_M 按中文部首进行排序:SCHINESE_RA ...

  3. java反射入门

    http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html package reflectTest; class Demo{ / ...

  4. Intellij IDEA 2016 mybatis 生成 mapper

    转载地址:http://gold.xitu.io/entry/57763ab77db2a2005517ae3f

  5. 关于Condition Variable的一些思考

    可能大家都使用过condition variable(之后称cv),一些博客也对cv做了介绍,但是有的说的不完全正确,甚至有误导使用者的倾向,其实最合理的使用方式是查阅文档, 如果你英语还ok的话,h ...

  6. leetcode Valid Sudoku python

    #数独(すうどく,Sūdoku)是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫内的数字均含1-9,不重复.#数独盘 ...

  7. leetcode Merge K sorted Lists python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  8. php 按列值合并数据

    /* * PHP按值合并数组 * */ function my_array_merge(&$array1, &$array2) { $result = Array(); foreach ...

  9. python----设置默认编码

    问题:python的默认编码是ascii.在处理中文的时候可能会出现乱码的情况:这个时候我们就需要把编码设置为对应的编码了. 解决方案: 对python文件的头部做如下修改 import sys re ...

  10. SQL Server 查看一个表上的索引

    方法:1 sys.indexes index_id =0:堆 index_id =1:聚集索引 index_id =2.....:非聚集索引 ----------------------------- ...