仅允许在属性类型上使用。如果标记了属性类型,那么该类型的所有自定义属性也将被标记。
请注意,低和中级托管剥离级别不会删除任何自定义属性。
using System; using UnityEngine; using UnityEngine.Scripting;
public class NewBehaviourScript : MonoBehaviour { void Start() { var f = new Foo(); foreach (var attr in f.GetType().CustomAttributes) { if (attr.AttributeType == typeof(TypeUsedAttribute)) { Debug.Log(attr.AttributeType); } } } }
[TypeUsed] // Will survive because TypeUsedAttribute is used [Required] // Will survive because RequiredAttribute has the attribute [RequireAttributeUsages] [UnusedAndNotRequiredAttribute] // Is considered valid for managed code stripping to remove class Foo { }
class TypeUsedAttribute : Attribute { }
[RequireAttributeUsages] class RequiredAttribute : Attribute { }
class UnusedAndNotRequiredAttribute : Attribute { }