Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to query custom Attributes in C# during compile time ( not run-time )
    text
    copied!<p>In other words could it be possible to create assembly, which does not even compile (assuming the checking code is not removed ) if each one of the Classes does not have ( "must have" ) custom attributes ( for example Author and Version ) ?</p> <p>Here is the code I have used for querying during run time :</p> <pre><code>using System; using System.Reflection; using System.Collections.Generic; namespace ForceMetaAttributes { [System.AttributeUsage ( System.AttributeTargets.Method, AllowMultiple = true )] class TodoAttribute : System.Attribute { public TodoAttribute ( string message ) { Message = message; } public readonly string Message; } [System.AttributeUsage ( System.AttributeTargets.Class | System.AttributeTargets.Struct, AllowMultiple = true )] public class AttributeClass : System.Attribute { public string Description { get; set; } public string MusHaveVersion { get; set; } public AttributeClass ( string description, string mustHaveVersion ) { Description = description; MusHaveVersion = mustHaveVersion ; } } //eof class [AttributeClass("AuthorName" , "1.0.0")] class ClassToDescribe { [Todo ( " A todo message " )] static void Method () { } } //eof class //how to get this one to fail on compile class AnotherClassToDescribe { } //eof class class QueryApp { public static void Main() { Type type = typeof(ClassToDescribe); AttributeClass objAttributeClass; //Querying Class Attributes foreach (Attribute attr in type.GetCustomAttributes(true)) { objAttributeClass = attr as AttributeClass; if (null != objAttributeClass) { Console.WriteLine("Description of AnyClass:\n{0}", objAttributeClass.Description); } } //Querying Class-Method Attributes foreach(MethodInfo method in type.GetMethods()) { foreach (Attribute attr in method.GetCustomAttributes(true)) { objAttributeClass = attr as AttributeClass; if (null != objAttributeClass) { Console.WriteLine("Description of {0}:\n{1}", method.Name, objAttributeClass.Description); } } } //Querying Class-Field (only public) Attributes foreach(FieldInfo field in type.GetFields()) { foreach (Attribute attr in field.GetCustomAttributes(true)) { objAttributeClass= attr as AttributeClass; if (null != objAttributeClass) { Console.WriteLine("Description of {0}:\n{1}", field.Name,objAttributeClass.Description); } } } Console.WriteLine ( "hit Enter to exit " ); Console.ReadLine (); } //eof Main } //eof class } //eof namespace //uncomment to check whether it works with external namespace //namespace TestNamespace { // class Class1 { } // class Class2 { } //} </code></pre> <p>Edit: Just to justify my choice for answer. I think casperOne provided the correct answer of the question. </p> <p>However the reasons for asking the question seemed to be <a href="https://stackoverflow.com/questions/753255/is-it-possible-to-query-custom-attributes-in-c-during-compile-time-not-run-tim/753301#753301">weak</a>. Probably I should start to use some external tool such as : <a href="http://www.finalbuilder.com/Demos/FinalBuilder_basic_viewlet_swf.html" rel="noreferrer">FinalBuilder</a> or create unit tests checking for this "requirement", using Pex , Nunit or other unit testing frameworks ...</p> <p><strong>EDIT</strong> I added a small <a href="https://stackoverflow.com/questions/753255/is-it-possible-to-query-custom-attributes-in-c-during-compile-time-not-run-tim/763010#763010">code snippet</a> of a console program at the end of the answers that performs the check ... feel free to comment, criticize or suggest improvements<br> Once more I realized that this "requirement" should be implemented as part of the unit testing just before the "check in"</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload