C# 特性(Attribute)

1.定义特性类

 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class MQDtoAttribute:Attribute
    {
        public string Topic { get; set; }

        public bool IsClient { get; set; } =  false;
        public MQDtoAttribute(string topic)
        {
            this.Topic = topic;
        }

        public MQDtoAttribute(string topic,bool isClient)
        {
            this.Topic = topic;
            this.IsClient = isClient;
        }
    }

2.使用特性类

[MQDto("/sys/post", isClient: true)]
    public class EquipmentEventReport
    {
        /// <summary>
        /// 
        /// </summary>
        public string id { get; set; }
  
    }

 

posted @ 2021-09-08 10:37  北极星下落不明  阅读(41)  评论(0)    收藏  举报