AttributeUsage

本文详细介绍了System.AttributeUsage属性的使用范围及原则,解释了如何通过构造函数定义特性是否可被继承及重复使用,并提供了Attribute.GetCustomAttribute方法来获取类的Attribute。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

System.AttributeUsage声明一个Attribute的使用范围与使用原则。


[SerializableAttribute]
[AttributeUsageAttribute(AttributeTargets.Class, Inherited = true)]
[ComVisibleAttribute(true)]
public sealed class AttributeUsageAttribute : Attribute

 构造函数

 

  AllowMultiple 和 Inherited 参数是可选的,所以此代码具有相同的效果:

  

  AttributeTarget的值可以参考1。部分可取值如下:

  

  如果 AllowMultiple 参数设置为 true,则返回特性可对单个实体应用多次。

  如果 Inherited 设置为 false,则该特性不由从特性化的类派生的类继承。

  Attribute.GetCustomAttribute可以获取一个类的Attribute。


using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using UnityEngine; public class SkillHandler { // 用字典缓存方法委托 private static readonly Dictionary<string, Action<ISkill, IGuestData, GameObject>> _skillActions = new Dictionary<string, Action<ISkill, IGuestData, GameObject>>(StringComparer.OrdinalIgnoreCase); // 静态构造函数初始化字典 static SkillHandler() { var methods = typeof(SkillHandler).GetMethods(BindingFlags.Static | BindingFlags.Public); foreach (var method in methods) { // 通过自定义特性过滤技能方法 var attr = method.GetCustomAttribute<SkillMethodAttribute>(); if (attr != null) { var action = (Action<ISkill, IGuestData, GameObject>)Delegate.CreateDelegate( typeof(Action<ISkill, IGuestData, GameObject>), method); _skillActions[attr.SkillName] = action; } } } public static void Execute(ISkill skill, IGuestData guestData, GameObject targetObj) { if (_skillActions.TryGetValue(skill.SkillName, out var action)) { action(skill, guestData, targetObj); } else { Debug.LogError($"未找到技能处理函数: {skill.SkillName}"); } } // 通过特性标记技能方法 [SkillMethod("Fireball")] public static void Fireball(ISkill skill, IGuestData guestData, GameObject target) { Debug.Log($"火球术逻辑: {target.name}"); } [SkillMethod("Heal")] public static void Heal(ISkill skill, IGuestData guestData, GameObject target) { //Debug.Log($"治疗术逻辑: {guestData.HP}"); } } // 自定义特性类 [AttributeUsage(AttributeTargets.Method)] public class SkillMethodAttribute : Attribute { public string SkillName { get; } public SkillMethodAttribute(string skillName) { SkillName = skillName; } } 详细讲解,每一行代码都要说
最新发布
07-20
/// <summary> /// Represents the position of an RFID reader. /// </summary> public class RfidReaderPos()//List<PlcPoint> plcPoints { /// <summary> /// Gets or sets the track number. /// </summary> public string RfidRdrlineCode { get; set; } /// <summary> /// Gets or sets the RFID code. /// </summary> public string RfidCode { get; set; } /// <summary> /// Gets or sets the 处理类型 division 分流 print 调用mes打印 /// </summary> public string RfidProcessType { get; set; } /// <summary> /// Gets or sets the RFID UID value. /// </summary> public string RfidUIDValue { get; set; } /// <summary> /// Gets or sets the RFID tag value. /// </summary> public string RfidTagValue { get; set; } /// <summary> /// Gets or sets the RFID data value. /// </summary> public string RfidDataValue { get; set; } /// <summary> /// Gets or sets the RFID data value timestamp. /// </summary> public string RfidDataValueTimestamp { get; set; } /// <summary> /// Gets or sets the RFID position code. /// </summary> public string RfidRdrpositionCode { get; set; } /// <summary> /// Gets or sets the RFID position name. /// </summary> public string RfidRdrpositionName { get; set; } /// <summary> /// Gets or sets the RFID position description. /// </summary> public string RfidRdrpositionDesc { get; set; } /// <summary> /// Gets or sets the RFID modbus slave ID. /// </summary> public int? RfidRdrSlaveId { get; set; } // 使用可空类型以对应可能的 null 值 public int RfidRdrpollIntervalMs { get; set; } = 50; public int RfidRdrtimeoutMs { get; set; } = 6000; /// <summary> /// Gets or sets the RFID modbus IP address. /// </summary> public string RfidRdrIp { get; set; } /// <summary> /// Gets or sets the RFID modbus port. /// </summary> public string RfidRdrPort { get; set; } /// <summary> /// Gets or sets the RFID protocol, specifically for modbus. /// </summary> public string RfidRdrProtocol { get; set; } /// <summary> /// 读写PLC点位列表(JSON 存储) /// </summary> public List<PlcPoint> PlcPoints { get; set; }//= plcPoints; public SemaphoreSlim ProcessingLock { get; set; } = new (1, 1); } 根据需要加入定义的注解: [AttributeUsage(AttributeTargets.Property)] public class SqliteField : Attribute { //public string DataType { get; set; } = "TEXT"; public bool IsPrimaryKey { get; set; } = false; public int Length { get; set; } public bool AutoIncrement { get; set; } public bool NotNull { get; set; } = false; } [AttributeUsage(AttributeTargets.Property)] public class MaxLengthAttr : Attribute { public int Length { get; } public MaxLengthAttr(int length) => Length = length; } [AttributeUsage(AttributeTargets.Property)] public class PrimaryKey : Attribute { }
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值