C#中Attribute的学习

77 阅读1分钟

Attribute 这个在很多地方都看到,但是没有仔细去弄明白一些用法。而且发现有两种使用方式。一个看起来像构造函数,一个是有等号赋值的构造函数。这两个有什么不同,该如何区别理解。

1 自定义属性类

我定义了几个自定义属性类如下

  [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field)]
    public class ATestAttribute : Attribute
    {
        public string NameA { get; set; }

        private string TestC;
        //public ATestAttribute(string NameA="888")
        //{
        //    TestC = NameA;
        //}

       public string TestD { get; set; }
        public string TestE;

        //public ATestAttribute(string v1, string v2, string v3)
        //{
        //}
    }

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field)]
    public class BTestAttribute : Attribute
    {
        public string NameB { get; set; }
    }

    [AttributeUsage(AttributeTargets.Method)]
    public class MyMethodAttribute : Attribute
    {
        public bool IsImportant { get; set; }
        public string Description { get; set; }

        public MyMethodAttribute(bool isImportant=false, string description="默认数据")
        {
            IsImportant = isImportant;
            Description = description;
        }
    }

2 测试代码

   [ATest(NameA = "MyclassA",TestD ="kkkkk",TestE ="66666")]
    public class MyClassA
    {
        [Required]
        [EmailAddress]
        [MaxLength(50)]
        [ATest(TestD ="DDDD")] 
        public string A { get; set; }

        [Required]
        [EmailAddress]
        [ATest(NameA = "B",TestD ="AAAAAA",TestE ="77777")]
        [BTest(NameB = "BB")]
        public string B { get; set; }
        [MyMethod(true,"hello")]
        public void TestData()
        {

        }

        [MyMethod(IsImportant=true, Description="hello这个能理解")]
        public void CeShi()
        {

        }
        [MyMethod]
        public void CeShi2()
        {

        }
    }
 Type type = typeof(MyClassA);
            MemberInfo[] member = type.GetMembers();
            IEnumerable<Attribute> attrs =type.GetCustomAttributes();

            Console.WriteLine(type.Name + "具有的特性999:");
            foreach (ATestAttribute item in attrs)
            {
                Console.WriteLine(item.NameA+"======"+item.TestD+"====="+item.TestE);
            }

            Console.WriteLine("**********");

            // 循环每个成员
            foreach (MemberInfo item in member)
            {
                // 获取每个成员拥有的特性
                var attrList = item.GetCustomAttributes();
                foreach (Attribute itemNode in attrList)
                {
                    // 如果是特性 ATestAttribute
                    if (itemNode.GetType() == typeof(ATestAttribute))
                       // Console.WriteLine(((ATestAttribute)itemNode).NameA);
                        Console.WriteLine(((ATestAttribute)itemNode).NameA + "======" + ((ATestAttribute)itemNode).TestD + "=====" + ((ATestAttribute)itemNode).TestE);

                    else if (itemNode.GetType() == typeof(BTestAttribute))
                        Console.WriteLine(((BTestAttribute)itemNode).NameB);

                    else if (itemNode.GetType() == typeof(MyMethodAttribute))
                        Console.WriteLine(((MyMethodAttribute)itemNode).Description+"======"+ ((MyMethodAttribute)itemNode).IsImportant);

                    else
                        Console.WriteLine("这不是我定义的特性:" + itemNode.GetType());
                }
            }

3 运行结果

image.png

4 AI 解答

这个回答还算比较好,比看其他人的博文回答好很多。

image.png

image.png

image.png

image.png

image.png

image.png

image.png

说实在的特别,前面的解释没有很明白,但是说到位置参数和命名参数的时候,我就理解了。这个才是重点。

其他人的文章:www.jb51.net/article/247… 位置参数与命名参数文章:www.cnblogs.com/netlyf/p/38…