• 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. Android开发之XUtils框架使用和报错处理

    一.XUtils  lib的的添加: 1.点击+,选择第一个Library dependency 2.输入XUtils 按enter键,搜索: 3.然后就是选择XUtils,选择哪个版本就看个人了,接 ...

  2. BZOJ2818 Gcd

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  3. 调用mybatis generator已经生成好的dao来查询例子

    package com.cib.xj.controller; import java.util.List; import javax.annotation.Resource; import org.s ...

  4. 配置maven

    http://www.cnblogs.com/liunanjava/archive/2015/11/05/4936037.html

  5. pair correlation ggpair ggmatrix

    https://zhuanlan.zhihu.com/p/23400450 首发于 R语言数据分析与可视化 关注专栏 登录 写文章     R 语言矩阵散点图 EasyCharts· 15 天前 散点 ...

  6. Win7 64位下PowerDesigner连接64位Oracle11g数据库

    操作系统:WIN7 64旗舰版 Oracle版本:64位11g PowerDesigner版本:15.1 问题描述:因为PowerDesigner是32的程序,连接数据库会默认开启32位的ODBC,因 ...

  7. js获取url信息

    设置或获取对象指定的文件名或路径. alert(window.location.pathname) 设置或获取整个 URL 为字符串. alert(window.location.href); 设置或 ...

  8. C# GUID转换成16位字符串或19位数字并确保唯一

    /// <summary> /// 根据GUID获取16位的唯一字符串 /// </summary> /// <param name=\"guid\" ...

  9. Yii2中如何将Jquery放在head中的方法

    原文地址: https://my.oschina.net/kenblog/blog/547602 方法一(推荐):针对jquery进行components配置,指定Yii2自带jquery自带资源出现 ...

  10. 基于dom的xss漏洞原理

    原文:http://www.anying.org/thread-36-1-1.html转载必须注明原文地址最近看到网络上很多人都在说XSS我就借着暗影这个平台发表下自己对这一块的一些认识.其实对于XS ...