C#之Attitude初步了解

Attribute(特性)使用详解

网上看了一些关于attribute的理解,但是多少感觉有一些本末颠倒了,所以呢,这里只是针对Attribute如何使用,就像怎么用手机拍照,能拍出什么效果来一样,不对其原理进行深究。

很多时候,看见文档里的Attribute,很多时候,并不知道这些Attribute到底起着什么作用。

看一下微软的官方文档,

attribute的主要特点是:为特定程序添加元数据,可以接收自变量,利用反射查看自己的元数据。

如何使用:1使用现成的Attribute

​ 2创建自定义Attribute

常见Attribute:

​ 1标记soap协议:WebMethodAttribute

​ 2描述与本机代码互操作时如何封装方法参数:MarshalAsAttribute

​ 3描述类,方法和接口的COM属性

​ 4调用非托管代码:DllmportAttribute

​ 5从标题、版本、说明或商标方法描述程序集

​ 6描述需要序列化并保留类的哪些成员

​ 7描述如何为了执行XML序列化在类成员和XML节点之间进行映射

​ 8描述的方法的安全要求

​ 9指定用于强制实施安全规范的特征

​ 10通过实时(JIT)编译器控制优化

​ 11获取方法调用方的相关信息

如何创建自定义Attribute:

attribute类是直接或间接的派生自system.Attribute类。

常见的使用:

DllmportAttribute:

[System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)] [System.Runtime.InteropServices.ComVisible(true)] public sealed class DllImportAttribute : Attribute

指示由非托管dll公开为静态入口点的特性化方法(库中的方法公开为静态)

ConditionalAttribute:

[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=true)] [System.Runtime.InteropServices.ComVisible(true)] [System.Serializable] public sealed class ConditionalAttribute : Attribute

指示编译器,除非定义了指定的有条件编译符号,否则,应忽视方法调用或方法(如果没有指定的条件编译符号,就不好进行编译)

SerializableAttribute:

[System.Runtime.InteropServices.ComVisible(true)] [System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Struct, Inherited=false)] public sealed class SerializableAttribute : Attribute

表示类可以序列化,即将其转换为可传输的格式,不可继承

序列化:序列化是讲对象转换成可保持或传输的格式的过程。

反序列化:讲流转换为对象

ComVisibleAttribute:

[System.Runtime.InteropServices.ComVisible(true)] [System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)] public sealed class ComVisibleAttribute : Attribute

控制程序集中单独托管类型或成员,或所有类型对COM的访问性

ObsoleteAttribute

[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)] public sealed class ObsoleteAttribute : Attribute

标记不再使用的程序元素,此类不能被继承。

Message:警告信息

IsError:true为错误,false为警告

AttributeUsage

[System.AttributeUsage(System.AttributeTargets.All, AllowMultiple = false, Inherited = true)]class NewAttribute : System.Attribute { }

指定;另一个Attribute类的用法,该类不可继承

定义自己的Attribute类是,可以通过在属性类上放置AttributeUsageAttribute来控制使用它的方式,指示的Attribute类必须通过或间接的从Attribute类派生。

ValidOn:位置参数指定指示的属性可以放置到的程序元素,即允许使用在哪些程序类型上。在AttributeTargets枚举中列出了可以放置属性的所有元素集合。使用按位“或”运算符(|)组合多个AttributeTargets值。

AllowMultiple:该命名参数指定是否可以为给定的程序元素多次指定指定的属性。

Inherited:该命名参数指定所指示的属性是否由派生类和重写成员继承。

显性调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值