C# 自定义程序集属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1936953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:00:36  来源:igfitidea点击:

Custom Assembly Attributes

c#.netassembliesattributes

提问by Charlie Salts

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

我想知道我是否可以定义自定义程序集属性。现有属性按以下方式定义:

[assembly: AssemblyTitle("MyApplication")]  
[assembly: AssemblyDescription("This application is a sample application.")]  
[assembly: AssemblyCopyright("Copyright ? MyCompany 2009")]  

Is there a way I can do the following:

有没有办法我可以做到以下几点:

[assembly: MyCustomAssemblyAttribute("Hello World! This is a custom attribute.")]

采纳答案by Preet Sangha

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.

要阅读使用这种 linq stmt。

var attributes = assembly
    .GetCustomAttributes(typeof(MyCustomAttribute), false)
    .Cast<MyCustomAttribute>();

回答by Lee

Yes, use AttributeTargets.Assembly:

是的,使用 AttributeTargets.Assembly:

[AttributeUsage(AttributeTargets.Assembly)]
public class AssemblyAttribute : Attribute { ... }