特性类
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
[Demo(10,Name ="小韩")]
public class NewBehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
var testType = GetType();
//IEnumerable<CustomAttributeData> attrs = testType.CustomAttributes;
IEnumerable<Attribute> data = testType.GetCustomAttributes(typeof(DemoAttribute)); //获得一组类型为DemoAttribute的特性对象
var demoData = data.ToList()[0] as DemoAttribute;
Debug.Log(demoData.Name);
}
// Update is called once per frame
void Update()
{
}
}
public class DemoAttribute : Attribute
{
public string Name { get; set; }
public int Age { get; set; }
public DemoAttribute(int age)
{
Age = age;
}
}