您当前的位置: 首页 >  c#

呆呆敲代码的小Y

暂无认证

  • 9浏览

    0关注

    385博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C#基础知识学习 之 ☀️ 特性(Attribute) 的含义和用法

呆呆敲代码的小Y 发布时间:2021-04-22 19:48:21 ,浏览量:9

C# 特性🔥(Attribute) 什么是特性🙏

特性(Attribute)是用于在运行时传递程序中各种元素(比如类、方法、结构、枚举、组件等)的行为信息的声明性标签。您可以通过使用特性向程序添加声明性信息。一个声明性标签是通过放置在它所应用的元素前面的方括号([ ])来描述的。 特性(Attribute)用于添加元数据,如编译器指令和注释、描述、方法、类等其他信息。.Net 框架提供了两种类型的特性:预定义特性和自定义特性。

特性作用🎅

用以将元数据或声明信息与代码(程 序集、类型、⽅方法、属性等)相关联

特性描述🎄
  • 特性可向程序中添加元数据
  • 可以将一个或多个特性应用到整个程序集,模块或较小的程序元素(如类和属性)—一个程序元素可以添加多个特性
  • 特性可以与⽅方法和属性相同的⽅方式接受参数—特性可以接受参数
  • 程序可以使用反射检查自己的元数据或其他程序内的元数据
  • 特性(元数据)是在编译之后就定义好的
元数据🎁(Meta Data)

描述:元数据是一种⼆进制信息,⽤用以对存储在公共语⾔言运⾏行行库可移植 可执⾏文件 (PE) 文件或存储在内存中的程序进行描述。将您的代 码编译为 PE 文件时,便便会将元数据插入到该文件的一部分中,而 将代码转换为 Microsoft 中间语言 (MSIL) 并将其插⼊入到该文件的 另一部分中。在模块或程序集中定义和引用的每个类型和成员都 将在元数据中进行说明。当执⾏代码时,运⾏库将元数据加载到内存中,并引用它来发现有关代码的类、成员、继承等信息。

在这里插入图片描述

预定义特性💬 AttributeUsage🎉

这个重要还难! 描述:预定义特性 AttributeUsage 主要⽤用于标示⾃自定义特性可以 应⽤用到哪些类型的程序元素上,这个信息由第⼀一个参数给出 实例:

[AttributeUsage(     
validon,//规定特性可被放置的语言元素,它是枚举器 AttributeTargets 的值的组合,默认值是 AttributeTargets.All   
AllowMultiple=allowmultiple, //如果为 true,则该特性可以在同一个元素多次使⽤用,默认值是 false(不不可多次使⽤用)    
Inherited=inherited//如果为 true,则该特性可被派生类继承,默认值是 false(不被继承) )]

1.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[AttributeUsage(AttributeTargets.Class,AllowMultiple =true,Inherited =true)]
public class AuthorAttribute : Attribute
{
    public string author;
    public string lastDate;//最后修改的日期
    public string LastDate
    {
        get
        {
            return lastDate;
        }
        set
        {
            lastDate = value;
        }
    }
    //当前特性的构造函数
    public AuthorAttribute(string author)
    {
        this.author = author;
    }
}

[AttributeUsage(AttributeTargets.Method,AllowMultiple =true)]
public class MyConditionAttribute : Attribute
{
    public MyConditionAttribute()
    { 
    }
}
[Author("Albert",lastDate = "1010.6.13"),Author("Tom",lastDate ="1010.10.13")]
public class UserAttributeDemo : MonoBehaviour
{
    public static  string log="abc";

    [MyCondition]
    public static void ShowLog()
    {
        Debug.Log(log);

    }

}

2.using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Reflection;

[AddComponentMenu("My/UsesAttributeObserver")]
[RequireComponent(typeof(Rigidbody),typeof(BoxCollider))]//组件依赖

public class UsesAttributeObserver : MonoBehaviour
{
    [Header("日志延迟时间")]
    [Range(0,1000)]
    public float  logelayTime=100;

    [SerializeField]//让私有字段也可以显示到监视器面板上
    private string name;

    [ColorUsage(false)]
    public Color col;

    private void Start()
    {
        InvokeMethodByAttribute(typeof(MyConditionAttribute),"ShowLog",true,true);
    }

    /// 
    /// 执行某个方法依据某个特性
    /// 
    /// 方法所在的类的类型
    /// 方法名
    /// 是否静态
    /// 是否公有
    /// 成员方法所在对象
    /// 
    private void InvokeMethodByAttribute(Type methodClasstype, string methodName,bool isStatic,bool isPublic,object methodObj=null)
    {

        BindingFlags staticFlags = isStatic ? BindingFlags.Static : BindingFlags.Instance;
        BindingFlags publicFlags = isPublic ? BindingFlags.Public  : BindingFlags.NonPublic;
        //获取到该方法
        MethodInfo info= methodClasstype.GetMethod(methodName, staticFlags | publicFlags);
        //获取MyConditionAttribute类型的特性
        object[] atts= info.GetCustomAttributes(typeof(MyConditionAttribute), false);

        if (atts.Length>0)
        {
            //info.Invoke();
        }

        Debug.Log("");
    }


    void PrintAttributeMsg()
    {
        //获取类型
        Type type = typeof(UserAttributeDemo);
        //得到该类型的不可继承的特性对象
        object[] atts= type.GetCustomAttributes(false);
        for (int i = 0; i             
关注
打赏
1663854676
查看更多评论
1.1911s