Custom Assembly Attributes

后端 未结 2 558
遥遥无期
遥遥无期 2020-11-29 01:38

I would like to know if I can define custom assembly attributes. Existing attributes are defined in the following way:

[assembly: AssemblyTitle(\"MyApplicat         


        
相关标签:
2条回答
  • 2020-11-29 02:12

    Yes, use AttributeTargets.Assembly:

    [AttributeUsage(AttributeTargets.Assembly)]
    public class AssemblyAttribute : Attribute { ... }
    
    0 讨论(0)
  • 2020-11-29 02:32

    Yes you can. We do this kind of thing.

    [AttributeUsage(AttributeTargets.Assembly)]
    public class MyCustomAttribute : Attribute {
        string someText;
        public MyCustomAttribute() : this(string.Empty) {}
        public MyCustomAttribute(string txt) { someText = txt; }
        ...
    }
    

    To read use this kind of linq stmt.

    var attributes = assembly
        .GetCustomAttributes(typeof(MyCustomAttribute), false)
        .Cast<MyCustomAttribute>();
    
    0 讨论(0)
提交回复
热议问题