Guest User

Untitled

a guest
Jun 23rd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4.  
  5.  
  6. namespace ForceMetaAttributes
  7. {
  8.  
  9. [System.AttributeUsage ( System.AttributeTargets.Method, AllowMultiple = true )]
  10. class TodoAttribute : System.Attribute
  11. {
  12. public TodoAttribute ( string message )
  13. {
  14. Message = message;
  15. }
  16. public readonly string Message;
  17.  
  18. }
  19.  
  20. [System.AttributeUsage ( System.AttributeTargets.Class |
  21. System.AttributeTargets.Struct, AllowMultiple = true )]
  22. public class AttributeClass : System.Attribute
  23. {
  24. public string Description { get; set; }
  25. public string MusHaveVersion { get; set; }
  26.  
  27.  
  28. public AttributeClass ( string description, string mustHaveVersion )
  29. {
  30. Description = description;
  31. MusHaveVersion = mustHaveVersion ;
  32. }
  33.  
  34. } //eof class
  35.  
  36.  
  37. [AttributeClass("AuthorName" , "1.0.0")]
  38. class ClassToDescribe
  39. {
  40. [Todo ( " A todo message " )]
  41. static void Method ()
  42. { }
  43. } //eof class
  44.  
  45. //how to get this one to fail on compile
  46. class AnotherClassToDescribe
  47. {
  48.  
  49. } //eof class
  50.  
  51. class QueryApp
  52. {
  53. public static void Main()
  54. {
  55.  
  56. Type type = typeof(ClassToDescribe);
  57. AttributeClass objAttributeClass;
  58.  
  59.  
  60. //Querying Class Attributes
  61.  
  62. foreach (Attribute attr in type.GetCustomAttributes(true))
  63. {
  64. objAttributeClass = attr as AttributeClass;
  65. if (null != objAttributeClass)
  66. {
  67. Console.WriteLine("Description of AnyClass:n{0}",
  68. objAttributeClass.Description);
  69. }
  70. }
  71.  
  72.  
  73.  
  74. //Querying Class-Method Attributes
  75.  
  76. foreach(MethodInfo method in type.GetMethods())
  77. {
  78. foreach (Attribute attr in method.GetCustomAttributes(true))
  79. {
  80. objAttributeClass = attr as AttributeClass;
  81. if (null != objAttributeClass)
  82. {
  83. Console.WriteLine("Description of {0}:n{1}",
  84. method.Name,
  85. objAttributeClass.Description);
  86. }
  87. }
  88. }
  89. //Querying Class-Field (only public) Attributes
  90.  
  91. foreach(FieldInfo field in type.GetFields())
  92. {
  93. foreach (Attribute attr in field.GetCustomAttributes(true))
  94. {
  95. objAttributeClass= attr as AttributeClass;
  96. if (null != objAttributeClass)
  97. {
  98. Console.WriteLine("Description of {0}:n{1}",
  99. field.Name,objAttributeClass.Description);
  100. }
  101. }
  102. }
  103. Console.WriteLine ( "hit Enter to exit " );
  104. Console.ReadLine ();
  105. } //eof Main
  106. } //eof class
  107.  
  108. } //eof namespace
  109.  
  110.  
  111. //uncomment to check whether it works with external namespace
  112. //namespace TestNamespace {
  113.  
  114. // class Class1 { }
  115. // class Class2 { }
  116.  
  117. //}
  118.  
  119. //PLEASE COMMENT IF YOU FIND BUGS OR SUGGEST IMPROVEMENTS
  120.  
  121.  
  122. using System;
  123. using System.Collections.Generic;
  124. using System.Linq;
  125. using System.Text;
  126. using System.Reflection;
  127.  
  128. namespace MustHaveAttributes
  129. {
  130. [AttributeClass ( "Yordan Georgiev", "1.0.0" )]
  131. class Program
  132. {
  133.  
  134.  
  135. static void Main ( string [] args )
  136. {
  137. bool flagFoundCustomAttrOfTypeAttributeClass = false;
  138. Console.WriteLine ( " START " );
  139.  
  140. // what is in the assembly
  141. Assembly a = Assembly.Load ( "MustHaveAttributes" );
  142. Type[] types = a.GetTypes ();
  143. foreach (Type t in types)
  144. {
  145. object[] arrCustomAttributes = t.GetCustomAttributes ( true );
  146.  
  147.  
  148. if (arrCustomAttributes == null || arrCustomAttributes.GetLength ( 0 ) == 0)
  149. {
  150. //DO NOT CHECK IN
  151. ExitProgram ( t, "Found class without CustomAttributes" );
  152. }
  153.  
  154.  
  155. foreach (object objCustomAttribute in arrCustomAttributes)
  156. {
  157. Console.WriteLine ( "CustomAttribute for type is {0}", t );
  158. if (objCustomAttribute is AttributeClass)
  159. flagFoundCustomAttrOfTypeAttributeClass = true;
  160. }
  161.  
  162. if (flagFoundCustomAttrOfTypeAttributeClass == false)
  163. { //DO NOT CHECK IN
  164. ExitProgram ( t, "Did not found custom attribute of type AttributeClass" );
  165. }
  166. Console.WriteLine ( "Type is {0}", t );
  167. }
  168. Console.WriteLine ("{0} types found", types.Length );
  169.  
  170. //NOW REQUIREMENTS IS PASSED CHECK IN
  171. Console.WriteLine ( " HIT A KEY TO EXIT " );
  172. Console.ReadLine ();
  173. Console.WriteLine ( " END " );
  174. }
  175.  
  176.  
  177.  
  178. static void ExitProgram ( Type t, string strExitMsg )
  179. {
  180.  
  181. Console.WriteLine ( strExitMsg );
  182. Console.WriteLine ( "Type is {0}", t );
  183. Console.WriteLine ( " HIT A KEY TO EXIT " );
  184. Console.ReadLine ();
  185.  
  186. System.Environment.Exit ( 1 );
  187.  
  188. }
  189. } //eof Program
  190.  
  191.  
  192. //This will fail even to compile since the constructor requires two params
  193. //[AttributeClass("OnlyAuthor")]
  194. //class ClassOne
  195. //{
  196.  
  197. //} //eof class
  198.  
  199.  
  200. ////this will not check in since this class does not have required custom
  201. ////attribute
  202. //class ClassWithoutAttrbute
  203. //{ }
  204.  
  205.  
  206.  
  207. [AttributeClass("another author name " , "another version")]
  208. class ClassTwo
  209. {
  210.  
  211. } //eof class
  212.  
  213.  
  214. [System.AttributeUsage ( System.AttributeTargets.Class |
  215. System.AttributeTargets.Struct, AllowMultiple = true )]
  216. public class AttributeClass : System.Attribute
  217. {
  218.  
  219. public string MustHaveDescription { get; set; }
  220. public string MusHaveVersion { get; set; }
  221.  
  222.  
  223. public AttributeClass ( string mustHaveDescription, string mustHaveVersion )
  224. {
  225. MustHaveDescription = mustHaveDescription;
  226. MusHaveVersion = mustHaveVersion;
  227. }
  228.  
  229. } //eof class
Add Comment
Please, Sign In to add comment