Attribute是c#的语言特性

msdn说明如下:

The Attribute class associates predefined system information or user-defined custom information with a target element. A target element can be an assembly, class, constructor, delegate, enum, event field,interface, method, portable executable file module, parameter, property, return value, struct, or another attribute.

Information provided by an attribute is also known as metadata. Metadata can be examined at run time by your application to control how your program processes data, or before run time by external tools to control how your application itself is processed or maintained. For example, the .NET Framework predefines and uses attribute types to control run-time behavior, and some programming languages use attribute types to represent language features not directly supported by the .NET Framework common type system.

All attribute types derive directly or indirectly from the Attribute class. Attributes can be applied to any target element; multiple attributes can be applied to the same target element; and attributes can be inherited by an element derived from a target element. Use the AttributeTargets class to specify the target element to which the attribute is applied.

The Attribute class provides convenient methods to retrieve and test custom attributes. For more information about using attributes, see Applying Attributes and Extending Metadata Using Attributes.

简单翻译如下:

Attribute类可以把目标元素和一个预定义的信息或者是用户自定义信息关联起来。这里的目标元素可以是assembly,class,constructor,delegate,enum,event,field,interface,method,可执行文件模块,parameter,property,return value,struct或其它的Attribute。
Attribute提供的信息也被称为元数据(metadata)。元数据能用于在运行时控制怎样访问你的程序数据,或者在运行前通过额外的工具来控制怎样处理你的程序或部署它。例如.NET Framework预定义并使用attribute去控制运行时行为,一些编程语言使用attribute类型来描述.NET Framework中通用类型不直接支持的语言特性。
所有的Attribute类型直接或间接从Attribute类继承。Attribute能应用到任何target元素;多个Attribute能应用到相同的元素;
Attribute类提供遍历的方法去取出和测试自定义Attribute。更多关于Attribute的信息,可以看Applying Attributes和Extending Metadata Using Attributes。

unity中的预定义attribute

看了上述说明,我们可能还不是很清楚attribute到底怎么用,我们先来看看Unity中常用的预定义attribute是怎么使用的,然后再学习怎么使用我们自己自定义的attribute。

DllImport

DllImport应用于方法,用于告诉运行时该方法位于指定的dll的非托管代码中(如c/c++,oc等),如果dll是托管代码生成的(如c#代码生成的dll),则不需要应用此特性。例如,当我们需要调用iOS中的浏览器打开一个url时,可以先编写oc的方法,然后放在项目的plugin目录中,并且在c#脚本中应用DllImport就可以调用了。

oc代码:

NSString* CreateNSString (const char* string)
{
if (string)
return [NSString stringWithUTF8String: string];
else
return [NSString stringWithUTF8String: ""];
} extern "C" {
void OpenUrl(const char* urlString)
{
//打开浏览器
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:CreateNSString(urlString)]];
return;
}
}

c#代码:

[DllImport("__Internal")]
public static extern string OpenUrl(string url);

其他unity预定义attribute的使用可以参考http://blog.sina.com.cn/s/blog_5b6cb9500101857b.html

使用自定义的attribute

自定义的attribute需要继承System.Attribute,然后我们可以使用c#的预定义特性去注明此attribute的应用范围。如果我们自定义的attribute名为UI,那么我们的自定义的attribute类名则应该为UIAttribute。

using System;

public enum UILayer
{
    Default,
    Secondary,
    Popup,
    TipAndWraning
}

[AttributeUsage(AttributeTargets.Class)]
public class UIAttribute : Attribute
{
   public UILayer Layer { get; set; }
}

如上所示,我们定义了一个attribute,UI,并且指明了应用范围只能在类上应用。我们用这个attribute来注明不同的View类的Layer属性,使用代码如下:

[UI(Layer = UILayer.Default)]
public class LoginView : View
{
//略
}
[UI(Layer = UILayer.Popup)]
public class AlertInfoView : View
{
//略
}

当我们创建不同View类的实例时,可以从attribute中获取到Layer信息:

public View CreateView(System.Type type)
{
UIAttribute classAttribute = (UIAttribute)System.Attribute.GetCustomAttribute(type, typeof(UIAttribute)); View view = System.Activator.CreateInstance(type) as View;
view.Layer = classAttribute.Layer; return view;
} public void Test()
{
View login = CreateView(typeof(LoginView));
View alert = CreateView(typeof(AlertInfoView));
}

Unity中使用Attribute的更多相关文章

  1. 从Unity中的Attribute到AOP(七)

    本章我们将依然讲解Unity中的Attribute,继续命名空间在UnityEngine里的. PropertyAttribute,这个特性主要来控制变量或者类在Inspector里面的显示方式.和P ...

  2. 从Unity中的Attribute到AOP(五)

    今天主要来讲一下Unity中带Menu的Attribute. 首先是AddComponentMenu.这是UnityEngine命名空间下的一个Attribute. 按照官方文档的说法,会在Compo ...

  3. 从Unity中的Attribute到AOP(四)

    本篇我们将逐一讲解Unity中经常使用的Attribute(Unity对应的文档版本为2018.1b). 首先是Serializable,SerializeField以及NonSerialized,H ...

  4. 从Unity中的Attribute到AOP(三)

    上一篇我们对系统的Attributes进行了MSIL代码的查看,了解到了其本质就是一个类的构造函数.本章我们将编写自己的Attributes. 首先我们定义书的属性代码,如下: [AttributeU ...

  5. 从Unity中的Attribute到AOP(六)

    本文将重点对Unity剩下常用的Attribute进行讲解,其他不常用的Attribute各位可以自行去官方文档查阅. 首先是UnityEngine命名空间下的. ColorUsage,这个主要作用于 ...

  6. 从Unity中的Attribute到AOP(八)

    本文将讲一下在UnityEditor命名空间下的一些特性. CallBackOrder,这个Attribute是所有带callback index的Attribute的基类,由于官方也没有给出详细的说 ...

  7. 从Unity中的Attribute到AOP(二)

    上一篇文章我们初步了解了一下Attributes的含义,并且使用系统自带的Attributes写了点代码.在进一步解剖我们的代码之前,我觉得有个概念可能需要巩固一下:什么是元数据? 我们知道C#代码会 ...

  8. 从Unity中的Attribute到AOP(一)

    首先来看一下微软官方对Attributes(C#)的定义: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/conce ...

  9. 【《Effective C#》提炼总结】提高Unity中C#代码质量的21条准则

    作者:Williammao, 腾讯移动客户端开发工程师 商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处. 原文链接:http://wetest.qq.com/lab/view/290.h ...

随机推荐

  1. 【WCF】基址与默认终结点

    五月份的时候,有位老友给老周提了个建议:希望老周写一写WCF的文章.其实老周以前是写过WCF的文章的,只是不是写在这个博客里,老周并不打算把X年前的博客导进来,要写的话,重新写吧.毕竟,那个时候写的文 ...

  2. ASP.NET MVC5+EF6+EasyUI 后台管理系统(57)-插件---ueditor使用

    系列目录 目录: 前言 开发环境 知识点 初始使用 自定义工具栏 设置和读取编辑器内容 文件上传 ueditor加水印 ---------------------------------------- ...

  3. Android消息传递之组件间传递消息

    前言: 上篇学习总结了Android通过Handler消息机制实现了工作线程与UI线程之间的通信,今天来学习一下如何实现组件之间的通信.本文依然是为学习EventBus做铺垫,有对比才能进步,今天主要 ...

  4. dagger2系列之生成类实例

    上一节的最后,我讲到一次注入生成类实例的生成步骤.先来回顾一下: 1  Module中存在创建方法,则看此创建方法有没有参数 如果有参数,这些参数也是由Component提供的,返回步骤1逐一生成参数 ...

  5. 多线程条件通行工具——CyclicBarrier

    CyclicBarrier的作用是,线程进入等待后,需要达到一定数量的等待线程后,再一次性开放通行. CyclicBarrier(int, Runnable)构造方法,参数1为通行所需的线程数量,参数 ...

  6. base的应用

    ------------父类   public class Person   {       public Person(string name,int age)    {       this.Na ...

  7. 设计模式(十四)模板方法模式(Template Pattern)

    一.引言 提到模板,大家肯定不免想到生活中的“简历模板”.“论文模板”.“Word中模版文件”等,在现实生活中,模板的概念就是——有一个规定的格式,然后每个人都可以根据自己的需求或情况去更新它,例如简 ...

  8. ubuntu学习的简单笔记

    l vi编辑器开发步骤 A)输入 vi Hello.java B) 输入 i 插入模式. C)输入 冒号.[保存退出:wq][退出不保存:q!] l 列出当前目录的所有文件:ls 详细信息的列表:ls ...

  9. shiro的使用1 简单的认证

    最近在重构,有空学了一个简单的安全框架shiro,资料比较少,在百度和google上能搜到的中文我看过了,剩下的时间有空会研究下官网的文章和查看下源码, 简单的分享一些学习过程: 1,简单的一些概念上 ...

  10. TYPESDK手游聚合SDK服务端设计思路与架构之三:流程优化之订单保存与通知

    经过前两篇文字的分析与设计,我们已经可以搭建出一个能够支持多游戏多渠道的聚合SDK服务端,但这只是理想化状态下的一个简化模型.如果接入渠道的逻辑都是按照理想化的简化过程来构建,那么对于支付的请求,我们 ...