C# Attributes mandatory property

前端 未结 3 1172
时光说笑
时光说笑 2021-01-14 03:00

I\'ve created attribute like

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
    [Serializable]
    public class TestPropertyAttribute :          


        
3条回答
  •  有刺的猬
    2021-01-14 03:09

    The accepted answer by Jon Skeet is a good solution. However, you can write this with shorter code nowadays. It works the same:

    public class TestPropertyAttribute : Attribute
    {
        public TestPropertyAttribute(string name)
        {
            Name = name;
        }
    
        public string Name { get; }
    }
    

提交回复
热议问题