Atributos de Ensamblaje Personalizados


Me gustaría saber si puedo definir atributos de ensamblado personalizados. Los atributos existentes se definen de la siguiente manera:

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

¿Hay alguna manera de que pueda hacer lo siguiente:

[assembly: MyCustomAssemblyAttribute("Hello World! This is a custom attribute.")]
Author: starblue, 2009-12-20

2 answers

Sí puedes. Hacemos este tipo de cosas.

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

Para leer use este tipo de linq stmt.

var attributes = assembly
    .GetCustomAttributes(typeof(MyCustomAttribute), false)
    .Cast<MyCustomAttribute>();
 82
Author: Preet Sangha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-28 14:09:24

Sí, utilice AttributeTargets.Asamblea:

[AttributeUsage(AttributeTargets.Assembly)]
public class AssemblyAttribute : Attribute { ... }
 8
Author: Lee,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-12-20 20:46:13