• Attributes 特性
     公共语言运行时允许添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型、字段、方法和属性等。Attributes和Microsoft .NET Framework文件的元数据保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为。
     定制特性attribute,本质上是一个类,其为目标元素提供关联附加信息,并在运行期以反射的方式来获取附加信息。 attibute实例,是在编译期进行初始化,而不是运行期。
     定制特性类型,必须直接或者间接的继承自System.Attribute类,而且该类型必须有公有构造函数来创建其实例。所有自定义的特性名称都应该有个Attribute后缀,这是习惯性约定。
  • 目标元素
    • assembly 程序集
    • module 模块
    • type 类型
    • property 属性
    • event 事件
    • field 字段
    • method 方法
    • param 参数
    • return 返回值
  • 展现形式
     定制特性以[,]形式展现,放在紧挨着的元素上,多个特性可以应用于同一元素,特性间以逗号隔开,以下表达规则有效:[AttributeUsage][ Flags]、[AttributeUsage, Flags]、[Flags, AttibuteUsageAttribute]、[AttributeUsage(), FlagesAttribute()]
  • 消除二义性
     C#允许以指定的前缀来表示特性所应用的目标元素,建议这样来处理,因为显式处理可以消除可能带来的二义性。
using System;

namespace Anytao.net

{

[assembly: MyAttribute()] //应用于程序集
[moduel: MyAttribute()] //应用于模块
pubic class Attribute_how2do

{
}

}
  • 获取元素附加信息
public static void Main()

{

Tester t = new Tester();

t.CannotRun();

Type tp = typeof(Tester);

MethodInfo mInfo = tp.GetMethod("CannotRun");

TestAttribute myAtt = (TestAttribute)Attribute.GetCustomAttribute(mInfo, typeof(TestAttribute));

myAtt.RunTest();

}
  • DllImport

在进行互操作的时候,我们需要用DllImport来标识该方法是非托管的代码方法,在编译器编译的时候它能够正确的认识出被该特性标记的是外来代码段,所以能顺利的通过编译,当到达程序运行的时候,也能够正确的认识出该代码是引用非托管的代码,这样就让我们的CLR去加载非托管DLL文件,然后查找到入口点进行调用。

[DllImport("Win32DLL.dll", EntryPoint = "add", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int AddNumber(int x, int y);
    • EntryPoint属性
     EntryPoint属性是用来确定非托管方法的入口点是什么。
    • CharSet属性
     CharSet属性是用来确定在托管与非托管调用的过程中用什么字符编码来封送数据,因为我们的.NET平台是采用的Unicode编码,而标准C++是采用的Ansi编码,在我们了解了非托管代码的编码方式之后,我们就很确定用什么编码,那么如果我们不清楚非托管代码是用什么语言编写的或者不清楚它的编码方式时,我们可以使用CharSet枚举中的auto值,让CLR自动为我们处理相关细节。
    • CallingConvention属性
     CallingConvention属性也是一个比较重要的属性,在平台调用的过程中起到查找入口点的作用,在托管代码进行非托管代码入口点查找时,会通过CallingConvention中的值进行确认非托管入口点的调用约定。

.NET Attributes的更多相关文章

  1. Eclipse "Unable to install breakpoint due to missing line number attributes..."

    Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...

  2. 执行打的maven jar包时出现“Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes”

    Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for ...

  3. webapi filter过滤器中获得请求的方法详情(方法名,Attributes)

    public class GlobalActionFilter : ActionFilterAttribute { private string _requestId; public override ...

  4. 给iOS开发新手送点福利,简述文本属性Attributes的用法

    给iOS开发新手送点福利,简述文本属性Attributes的用法   文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSF ...

  5. jQuery in action 3rd - Working with properties, attributes, and data

    properties properties 是 JavaScript 对象内在的属性,可以进行动态创建,修改等操作. attributes 指的是 DOM 元素标记出来的属性,不是实例对象的属性. 例 ...

  6. Unsupported configuration attributes: [FILE_UPLOAD]

    Caused by: java.lang.IllegalArgumentException: Unsupported configuration attributes: [FILE_UPLOAD] 情 ...

  7. Jade模板引擎(一)之Attributes

    目录: Attributes Boolean Attributes Style Attributes Class Attributes &Attributes Attributes jade中 ...

  8. ASP.NET MVC 使用带有短横线的html Attributes(转载)

    转载地址:http://www.nmtree.net/2013/10/25/asp-net-mvc-use-dash-in-html-attributes.html 情景再现 我们常常需要一个文本框来 ...

  9. Create and Use Custom Attributes

    http://www.codeproject.com/Articles/1811/Creating-and-Using-Attributes-in-your-NET-applicat Create a ...

随机推荐

  1. FineUI(专业版)v2.6.0即将支持的两个新特性!

    特性1:以一挡三,将 160 行代码缩减为 60 行的技巧! 为了更新单元格的编辑值,我们需要下面三个函数同时上阵: GetModifiedDict:修改的单元格值 GetDeletedList:删除 ...

  2. ionic 开发笔记

    1.AngularJS 外部的控制器(DOM 事件.外部的回调函数如 jQuery UI 空间等)调用了 AngularJS 函数之后,必须调用$apply.在这种情况下,你需要命令 AngularJ ...

  3. java collection.sort()根据时间排序list

    首先:定义bean 然后:定义比较器 最后:测试使用 一.userBean package com.butterfly.Class; public class user { private Strin ...

  4. Bootstrap使用后笔记

      Bootstrap Modal 垂直居中 在 bootstrap.js中修改如下代码: Modal.prototype.adjustDialog = function () { var modal ...

  5. 图片加载框架Fresco与V4包冲突解决方法

  6. 【BZOJ 1065】【Vijos 1826】【NOI 2008】奥运物流

    http://www.lydsy.com/JudgeOnline/problem.php?id=1065 https://vijos.org/p/1826 好难的题啊TWT ∈我这辈子也想不出来系列~ ...

  7. 基于Emgu CV的人脸检测代码

    这个提供的代码例子是Emgu CV提供的源码里面自带的例子,很好用,基本不需要改,代码做的是人脸检测不是人脸识别,这个要分清楚.再就是新版本的Emgu CV可能会遇到系统32位和64位处理方式有区别的 ...

  8. 【USACO 2.3】The Longest Prefix

    题意: 给你一个少于200000的字符串,求最长的可以划分为给定词典里的单词的前缀. 题解: dp[i]表示第i位结尾的前缀是否可行,然后枚举每一位如果dp[i-1]==1,枚举所有单词,匹配成功的单 ...

  9. 解决:Cannot get http://gerrit.googlesource.com/git-repo/clone.bundle

    同步cm12.1初始化出现的问题: fatal: Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle fatal: err ...

  10. 项目中遇到的关于兄弟controller之间传值的问题解决

    层级关系如下 <ons-page ng-controller="tabbarIndexController"> <ons-tabbar position=&quo ...