【学习资料】

  《C#图解教程》(第24章):https://www.cnblogs.com/moonache/p/7687551.html
  电子书下载:https://pan.baidu.com/s/1mhOmBG0

  • 参考文章 

    C# 特性(Attribute)(建议看一看嗷):https://www.cnblogs.com/zhaoyl9/p/12027938.html

【内容】

    • 特性的用途
    • 特性与注释的区别
    • 内置特性
      • Obsolete(废弃特性)
      • Conditional(条件编译特性)
      • 调试者信息特性(CallerFilePath、CallerLineNumber、CallerMemberName)
      • DebuggerStepThrough(跳过调试特性)
      • 更多内置特性
    • 全局特性
    • 自定义特性
      • 命名规范
      • 使用反射访问特性
    • 限制特性的使用(AttributeUsage)

【笔记】

  • 用途
    • 允许我们向程序集的元数据中的成员上 声明一些特殊的信息(附加信息),是用于保存程序结构信息的 某种特殊类型的类(特性也是类)
    • 主要消费者为:编译器、CLR(反射)、浏览器(编辑器里的dll对象信息查看器)
    • 特性的作用
      • 告诉编译器如何编译
      • 序列化
      • 程序的安全特征(如数据验证)
      • 防止即时编译器对程序代码进行优化从而代码容易调试
      • 等等
    • 注:在程序运行前,特性就已经存在了

  • 与注释的区别

    图片来源:https://www.cnblogs.com/zhaoyl9/p/12027938.html

  • 内置特性
    • Obsolete(废弃特性)

      • 语法: Obsolete(string message, bool error=false) ,其中error传入true,则编译器显示报错
      • 定义在 程序结构(类、结构、成员等等)前
      • 将其标记为过期;在编译器的错误列表中,会显示警告、或错误
      • [Obsolete("func1 已过时")]
        public void Func1() { }
        [Obsolete("func2 已废弃", true)]
        public void Func2() { } void Start()
        {
        Func1();
        Func2();
        }
      • 查看错误列表
    • Conditional(条件编译特性)

      • 语法: Conditional(string conditionString) ,参数为 编译符号 名称
      • 定义在 方法 前
      • 如果编译符号未定义,那么编译器会移除该方法的所有调用
      • #define HELLO // 定义符号 HELLO
        using UnityEngine; public class LearnCS : MonoBehaviour
        {
        [System.Diagnostics.Conditional("HELLO")]
        public void Func1() { Debug.Log("Func1"); }
        [System.Diagnostics.Conditional("WORLD")]
        public void Func2() { Debug.Log("Func2"); } void Start()
        {
        Func1();
        Func2(); // 编译时会被移除
        }
        }
      • 输出结果:只输出 Fun1
    • 调试者信息特性(CallerFilePath、CallerLineNumber、CallerMemberName)

      • 定义在 方法参数 前
      • 编译器会自动给参数赋值:调用方法的文件路径、调用方法的行号、调用方法的方法名
      • public static void MyTrace(string message,
        [CallerFilePath] string fileName = "",
        [CallerLineNumber] int lineNumber = ,
        [CallerMemberName] string callingMember = "")
        {
        Debug.Log(string.Format("File: {0}", fileName));
        Debug.Log(string.Format("Line: {0}", lineNumber));
        Debug.Log(string.Format("Called From: {0}", callingMember));
        Debug.Log(string.Format("Message: {0}", message));
        }
      • 输出结果
    • DebuggerStepThrough(跳过调试特性)

      • 定义在 类、结构、构造函数、方法 前
      • 在单步调试时(Step Into),不会进入方法体内,而是直接跳过
      • 例1:断点在13行,进行单步调试(Step Into,VS的快捷键为F11),可以进入Func1方法体内
      • 例2: 断点在13行,进行单步调试(Step Info,VS快捷键为F11),直接会跳到14行

    • 更多内置特性

  • 全局特性

     传送门:https://www.cnblogs.com/liqingwen/p/5944391.html

  • 自定义特性

    • 继承基类: System.Attribute 
    • 命名规范
      • 以 Attribute 结尾,使用时不需要加这个后缀
      • 如:MyAttributeAttribute,使用为 [MyAttribute]
    • 像类一样声明一个特性,并通过反射获取特性的属性
    • using UnityEngine;
      using System; public class MyAttributeAttribute : System.Attribute
      {
      public string name { get; } // 名字
      public string date { get; } // 日期
      public MyAttributeAttribute(string name, string date)
      {
      this.name = name;
      this.date = date;
      }
      } public class LearnCS : MonoBehaviour
      {
      [Obsolete("MyTest 已过时")]
      [MyAttribute("heihei", "2020-2-2")]
      public class MyTest
      { } void Start()
      {
      // 通过Type的 GetCustomAttributes
      Type t = typeof(MyTest);
      var myAttribute11 = t.GetCustomAttributes(true);
      var myAttribute12 = t.GetCustomAttributes(typeof(MyAttributeAttribute), true); // 通过Attribute的 GetCustomAttribute
      var myAttribute21 = Attribute.GetCustomAttribute(typeof(MyTest), typeof(MyAttributeAttribute)); // 通过Attribute的 GetCustomAttributes
      var myAttribute31 = Attribute.GetCustomAttributes(typeof(MyTest));
      var myAttribute32 = Attribute.GetCustomAttributes(typeof(MyTest), typeof(MyAttributeAttribute)); //
      foreach(var att in myAttribute11)
      {
      //将特性对象转化为动物特性对象
      MyAttributeAttribute myAtt = att as MyAttributeAttribute;
      if (myAtt != null)
      {
      Debug.Log("name=" + myAtt.name + " , date=" + myAtt.date);
      }
      }
      Debug.Log("End");
      }
      }
    • 运行结果
  • 限制特性的使用(AttributeUsage)
    • 定义在特性类型前,用来限制特性的使用目标
    • 例如:只允许类使用,属性方法都不能使用该特性
    • [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited =true)]
      public class MyAttributeAttribute : System.Attribute
      {
      public string name { get; } // 名字
      public string date { get; } // 日期
      public MyAttributeAttribute(string name, string date)
      {
      this.name = name;
      this.date = date;
      }
      }
    • 公共属性

    • 构造函数的 AttributeTargets枚举限制

【Unity|C#】基础篇(13)——特性(Attribute)的更多相关文章

  1. python 基础篇 13 迭代器与生成器

    13. 前⽅⾼能-迭代器和⽣成器本节主要内容:1. 迭代器2. ⽣成器 ⼀. 迭代器我们之前⼀直在⽤可迭代对象进⾏迭代操作. 那么到底什么是可迭代对象. 本⼩节主要讨论可迭代对象. ⾸先我们先回顾⼀下 ...

  2. cocos2dx基础篇(13) 编辑框之二CCEditBox

    [3.x] (1)去掉"CC" (2)设置虚拟键盘的编辑类型 > EditBoxInputMode 变为强枚举 EditBox::EditBoxInputMode // SI ...

  3. 诱人的 react 视频教程-基础篇(14 个视频)

    诱人的 react 视频教程-基础篇(14 个视频) 诱人的 react 视频教程 - 基础篇 #1 介绍「07:25」 诱人的 react 视频教程 - 基础篇 #2 create-react-ap ...

  4. mootools常用特性和示例(基础篇2)

    接着上一篇:mootools常用特性和示例(基础篇1) 1.表单操作 html: <form id="myForm" action="submit.php" ...

  5. mootools常用特性和示例(基础篇1)

    网上关于mootools这个库的信息很少. 公司一些老的项目用到了mootools库,因为要维护,所以接触到了mootools. mootools(文档)官网:http://www.chinamoot ...

  6. 夯实Java基础系列1:Java面向对象三大特性(基础篇)

    本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 [https://github.com/h2pl/Java-Tutorial](https: ...

  7. C# 篇基础知识9——特性、程序集和反射

    特性(Attribute)是用于为程序元素添加额外信息的一种机制.比如记录文件修改时间或代码作者.提示某方法已经过期.描述如何序列化数据等等.方法.变量.属性.类.接口.结构体以及程序集等都是程序元素 ...

  8. python面试题库——1Python基础篇

    第一部分 Python基础篇(80题) 为什么学习Python? 语言本身简洁,优美,功能超级强大,跨平台,从桌面应用,web开发,自动化测试运维,爬虫,人工智能,大数据处理都能做 Python和Ja ...

  9. 深度学习入门者的Python快速教程 - 基础篇

      5.1 Python简介 本章将介绍Python的最基本语法,以及一些和深度学习还有计算机视觉最相关的基本使用. 5.1.1 Python简史 Python是一门解释型的高级编程语言,特点是简单明 ...

随机推荐

  1. window下建立vue.js项目

    安装node.js 直接下载安装文件安装就可以了 vue项目搭建 .到自己要件项目的文件夹运行cmd命令 .如果没有安装vue-cli .npm install -g vue-cli .vue ini ...

  2. Keras深度学习框架之损失函数

    一.损失函数的使用 损失函数[也称目标函数或优化评分函数]是编译模型时所需的两个参数之一. model.compile(loss='mean_squared_error', optimizer='sg ...

  3. Ubuntu Xftp 配置

    sudo apt-get updatesudo apt install openssh-serversudo apt-get install vsftpdsudo service vsftpd res ...

  4. 【转载】s19文件格式详解

    来源:http://blog.csdn.net/xxxl/article/details/19494187 1.概述 为了在不同的计算机平台之间传输程序代码和数据,摩托罗拉将程序和数据文件以一种可打印 ...

  5. 物理机安装ESXI6.7提示No Network Adapters的解决方案

    下载好ESXI6.7.iso镜像,写入U盘后,提示No Network Adapters,找不到网卡驱动. 解决办法:需要重新封装ESXI,将对应的网卡驱动嵌入进来. 1.先下载VMware-Powe ...

  6. h5笔记1

    1.HTML中不支持 空格.回车.制表符,它们都会被解析成一个空白字符 2.适用于大多数 HTML 元素的属性: class 为html元素定义一个或多个类名(classname)(类名从样式文件引入 ...

  7. jenkins 参数化运行性能测试脚本

    概述 我们用jenkins做持续集成的时候,常常需要跑不同的脚本,传不同的参数.尤其是性能基准测试,线程数和持续时间需要实时调整以满足我们的测试需求.那么是不是需求变了,我们 就需要重新准备一套脚本? ...

  8. C++中的多态及虚函数大总结

    多态是C++中很关键的一部分,在面向对象程序设计中的作用尤为突出,其含义是具有多种形式或形态的情形,简单来说,多态:向不同对象发送同一个消息,不同的对象在接收时会产生不同的行为.即用一个函数名可以调用 ...

  9. for遍历用例数据时,报错:TypeError: list indices must be integers, not dict,'int' object is not iterable解决方法

    一:报错:TypeError: list indices must be integers, not dict for i in range(0,len(test_data)): suite.addT ...

  10. SAP VL10B 报错 - 4500000317 000010 交付 $ 1 的交付项目 000010 与 POD 无关-

    SAP VL10B 报错 - 4500000317 000010 交付 $ 1 的交付项目 000010 与 POD 无关- 如下公司间STO单据, 业务背景是货物从公司代码LYSP转入公司代码BTS ...