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

一、背景

[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. viewPager使用时加载数据时显示IllegalStateException异常,解决不了。。。。

    从newsPager中得到newsDetailTitles标题的详细内容,这是通过构造器传过来的.打印日志78行能打印,45行打印出来共size是12.但是程序出现了异常java.lang.Illeg ...

  2. 关于nvarchar与varchar的区别

    varchar(x),  nvarchar(x)这里面的x指的是最大的列宽  如果存储的字符串没达到最大列宽  那么他也只获得对应的列宽的存储空间  并不意味着系统就会给它分配x的空间给它 varch ...

  3. CSS的W3C标准的盒子模型和低版本IE浏览器的盒子模型

    CSS中盒子模型的组成由内容区(content).内边距(padding).边框(border).外边距(margin)组成.内边距可细分为 padding-top.padding-right.pad ...

  4. Euclid gcd规则的证明

    Euclid 规则:如果x和y都是正整数,而且x>=y,那么gcd(x,y)=gcd(x mod y, y) 假设x和y的gcd为a,那么必然有 x=a*n1 y=a*n2(gcd(n1,n2) ...

  5. Cmd批处理语法实例

    @echo on :循环获取指定目录下以php为后缀的文件,且重命名后缀为html :for /r "E:\aaa\web" %%v in (*.php) do ren " ...

  6. scrapy中运行爬虫时出现twisted critical unhandled error错误

    1. 试试这条命令: twisted critical unhandled error on scrapy tutorial python python27\scripts\pywin32_posti ...

  7. Windows2008 VPN登录

    上一章已经讲过Windows2008RT搭建VPN服务器搭建过程,接下来说一下win2008和win8的VPN登录 win8VPN连接过程 先说win2008的VPN登录过程.很简单: 打开网络和共享 ...

  8. python执行shell命令

    1 os.system 可以返回运行shell命令状态,同时会在终端输出运行结果 例如 ipython中运行如下命令,返回运行状态status os.system('cat /etc/passwdqc ...

  9. opengl笔记

    GL_ARRAY_BUFFER(表示顶点数据) GL_ELEMENT_ARRAY_BUFFER(表示索引数据) GL_PIXEL_UNPACK_BUFEER( 表示传递给O p e n G L 的像素 ...

  10. client|server 最简单的聊天

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/socket ...