C# core knowledge review - 15. Features

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
using static UnityEngine.GraphicsBuffer;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

public class test : MonoBehaviour
{    
    #region 特性是什么
    //特性是一种允许我们向程序的程序集添加元数据的语言结构
    //它是用于保存程序结构信息的某种特殊类型的类
    //特性提供功能强大的方法以将声明信息与C#代码(类型、方法、属性等)相关联。
    //特性与程序实体关联后,即可在运行时使用反射查询特性信息
    //特性的目的是告诉编译器把程序结构的某组元数据嵌入程序集中
    //它可以放置在几乎所有的声明中(类、变量、函数等等申明)

    // 特性本质是个类
    //我们可以利用特性类为元数据添加额外信息
    //比如一个类、成员变量、成员方法等等为他们添加更多的额外信息
    // 之后可以通过反射来获取这些额外信息
    #endregion

    private void Start()
    {
        #region 特性的使用
        Myclass mc = new Myclass();
        //1.第1种声明
        Type t = mc.GetType();
        //2.第2种声明
        //t = typeof(Myclass);
        //3.第3种声明
        //t = Type.GetType("命名空间名.Myclass");

        //判断使用了什么特性
        //参数一:特性类型
        //参数二:是否搜索继承链(属性和事件忽略)
        if (t.IsDefined(typeof(MyCustomA), false))
        {
            Debug.Log("应用了这个特性");
        }

        //得到所有特性
        object[] array = t.GetCustomAttributes(true);
        for (int i = 0; i < array.Length; i++)
        {
            if (array[i] is MyCustomA)
            {
                Debug.Log((array[i] as MyCustomA).info);
                (array[i] as MyCustomA).TestFunc();
            }
        }

        //测试特性过时
        TestClass tc = new TestClass();
        tc.oldspeak("12");
        #endregion
    }
}
#region 自定义特性
//继承特性基类 Attribute
class MyCustomA : Attribute
{
    //特性中的成员 一般根据需求写
    public string info;

    public MyCustomA(string info)
    {
        this.info = info;
    }
    public void TestFunc()
    {
        Debug.Log("特性的方法");
    }
}
#endregion

#region 特性声明
//基本语法:
//[特性名(参数列表)]
//本质上就是在调用特性类的构造函数
//写在哪里?
//类、函数、变量上一行,表示他们具有该特性信息

[MyCustomA("101")]
class Myclass
{
    [MyCustomA("001")]
    public string info;
    [MyCustomA("110")]
    public void TestFun([MyCustomA("999")] int a)
    {
    }
}
#endregion

#region 限制自定义特性的使用范围
//通过为特性类加特性限制其使用范围
[AttributeUsage(AttributeTargets.Class| AttributeTargets.Struct,AllowMultiple = true, Inherited = true)]
//参数—:AttributeTargets--特性用在哪些地方
//参数二:AllowMultiple一是否允许多个特性实例用在同一个目标上
//参数三:Inherited一特性是否能被派生类和重写成员继承

public class MyCustom2A : Attribute
{

}
#endregion

#region 知识点五系统自带特性一过时特性
//过时特性
//Obsolete
//用于提示用户使用的方法等成员已经过时建议使用新方法
//一般加在函数前的特性
class TestClass
{
    //参数一:调用过时方法时提示的内容
    //参数二:true - 使用该方法时会报错   false - 使用该方法时直接警告
    [Obsolete("oldspeak方法已经过时了,请使用speak方法",false)]
    public void oldspeak(string tr)
    {
    }
    public void Speak()
    {
    }
}
#endregion

#region 知识点六系统自带特性一调用者信息特性
//哪个文件调用?
//CallerFilePath特性
//哪一行调用?
//CallerLineNumber特性
//哪个函数调用?
//CallerMemberName特性
//需要引用命名空间using System.Runtime.CompilerServices;
//一般作为函数参数的特性
public class Caller
{
    public void Speakcaller(string tr,[CallerFilePath]string fileName = "",
    [CallerLineNumber] int line = 0,[CallerMemberName]string target = "")
    {
        Debug.Log(tr);
        Debug.Log(fileName);
        Debug.Log(line);
        Debug.Log(target);
    }
}


#endregion

#region 知识点七系统自带特性一条件编译特性
//条件编译特性
//Conditional
//它会和预处理指令#define配合使用
//需要引用命名空间using system.Diagnostics;
//主要可以用在一些调试代码上
//有时想执行有时不想执行的代码

public class TestCond
{
    /// <summary>
    /// 调用使如果需要使用就在using前面加上#define,不然不会执行
    /// </summary>
    static void Fun()
    {
        Debug.Log("CondFun");
    }
}
#endregion

#region 知识点八系统自带特性一外部Dll包函数特性
//D11Import
//用来标记非.Net(C#)的函数,表明该函数在一个外部的DLL中定义。
//一般用来调用C或者C++的D11包写好的方法
//需要引用命名空间using System.Runtime.InteropServices
public class TestDll
{
    [DllImport("Test.dll")]
    //Add方法需要是dll文件里的方法
    public static extern int Add(int a ,int b);
}
#endregion

Guess you like

Origin blog.csdn.net/qq_29296473/article/details/131836814