2009-12-20 7 views
53

Vorrei sapere se è possibile definire gli attributi dell'assieme personalizzato. attributi esistenti sono definiti nel modo seguente:Attributi dell'Assembly personalizzato

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

C'è un modo che io possa fare quanto segue:

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

risposta

77

Sì, è possibile. Facciamo questo genere di cose

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

Per leggere utilizzare questo tipo di linq stmt.

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

Sì, utilizzare AttributeTargets.Assembly:

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