PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/trunk/Examples/test-suite/csharp/csharp_attributes_runme.cs

#
C# | 266 lines | 202 code | 29 blank | 35 comment | 66 complexity | 0fae30f4cb474a7b2dd154e31379cc01 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. using System;
  2. using System.Reflection;
  3. using System.ComponentModel;
  4. using csharp_attributesNamespace;
  5. public class runme
  6. {
  7. static void Main()
  8. {
  9. // Custom attributes typemap tests
  10. //
  11. // cstype typemap attributechecks
  12. //
  13. // Global function cstype typemap attributes check
  14. Type globaltype = typeof(csharp_attributes);
  15. {
  16. MethodInfo member = (MethodInfo)globaltype.GetMember("GlobalFunction")[0];
  17. if (Attribute.GetCustomAttribute(member, typeof(IntOutAttribute)) == null)
  18. throw new Exception("No IntOut attribute for " + member.Name);
  19. ParameterInfo parameter = member.GetParameters()[0]; // expecting one parameter
  20. if (parameter.Name != "myInt")
  21. throw new Exception("Incorrect parameter name");
  22. Attribute attribute = Attribute.GetCustomAttributes(parameter)[0];
  23. if (attribute.GetType() != typeof(IntInAttribute))
  24. throw new Exception("Expecting IntIn attribute");
  25. }
  26. // Constant - cstype typemap attributes check
  27. {
  28. MemberInfo member = (MemberInfo)globaltype.GetMember("TESTMACRO")[0];
  29. if (Attribute.GetCustomAttribute(member, typeof(IntOutAttribute)) == null)
  30. throw new Exception("No IntOut attribute for " + member.Name);
  31. }
  32. // Non-static method cstype typemap attributes check
  33. Type type = typeof(Stations);
  34. {
  35. MethodInfo member = (MethodInfo)type.GetMember("Reading")[0];
  36. if (Attribute.GetCustomAttribute(member, typeof(IntOutAttribute)) == null)
  37. throw new Exception("No IntOut attribute for " + member.Name);
  38. ParameterInfo parameter = member.GetParameters()[0]; // expecting one parameter
  39. if (parameter.Name != "myInt")
  40. throw new Exception("Incorrect parameter name");
  41. Attribute attribute = Attribute.GetCustomAttributes(parameter)[0];
  42. if (attribute.GetType() != typeof(IntInAttribute))
  43. throw new Exception("Expecting IntIn attribute");
  44. }
  45. // Static method cstype typemap attributes check
  46. {
  47. MethodInfo member = (MethodInfo)type.GetMember("Swindon")[0];
  48. if (Attribute.GetCustomAttribute(member, typeof(IntOutAttribute)) == null)
  49. throw new Exception("No IntOut attribute for " + member.Name);
  50. ParameterInfo parameter = member.GetParameters()[0]; // expecting one parameter
  51. if (parameter.Name != "myInt")
  52. throw new Exception("Incorrect parameter name");
  53. Attribute attribute = Attribute.GetCustomAttributes(parameter)[0];
  54. if (attribute.GetType() != typeof(IntInAttribute))
  55. throw new Exception("Expecting IntIn attribute");
  56. }
  57. // Constructor cstype typemap attributes check
  58. {
  59. ConstructorInfo member = (ConstructorInfo)type.GetConstructors()[0];
  60. ParameterInfo parameter = member.GetParameters()[0]; // expecting one parameter
  61. if (parameter.Name != "myInt")
  62. throw new Exception("Incorrect parameter name");
  63. Attribute attribute = Attribute.GetCustomAttributes(parameter)[0];
  64. if (attribute.GetType() != typeof(IntInAttribute))
  65. throw new Exception("Expecting IntIn attribute");
  66. }
  67. //
  68. // imtype typemap attributechecks
  69. //
  70. // Global function imtype typemap attributes check
  71. Type imclasstype = typeof(csharp_attributesPINVOKE);
  72. {
  73. MethodInfo member = (MethodInfo)imclasstype.GetMember("GlobalFunction")[0];
  74. if (Attribute.GetCustomAttribute(member, typeof(IntegerOutAttribute)) == null)
  75. throw new Exception("No IntegerOut attribute for " + member.Name);
  76. ParameterInfo parameter = member.GetParameters()[0]; // checking 1st parameter
  77. Attribute attribute = Attribute.GetCustomAttributes(parameter)[0];
  78. if (attribute.GetType() != typeof(IntegerInAttribute))
  79. throw new Exception("Expecting IntegerIn attribute");
  80. }
  81. // Constant - imtype typemap attributes check
  82. {
  83. MethodInfo member = (MethodInfo)imclasstype.GetMember("TESTMACRO_get")[0];
  84. if (Attribute.GetCustomAttribute(member, typeof(IntegerOutAttribute)) == null)
  85. throw new Exception("No IntegerOut attribute for " + member.Name);
  86. }
  87. // Non-static method imtype typemap attributes check
  88. {
  89. MethodInfo member = (MethodInfo)imclasstype.GetMember("Stations_Reading")[0];
  90. if (Attribute.GetCustomAttribute(member, typeof(IntegerOutAttribute)) == null)
  91. throw new Exception("No IntegerOut attribute for " + member.Name);
  92. ParameterInfo parameter = member.GetParameters()[1]; // checking 2nd parameter
  93. Attribute attribute = Attribute.GetCustomAttributes(parameter)[0];
  94. if (attribute.GetType() != typeof(IntegerInAttribute))
  95. throw new Exception("Expecting IntegerIn attribute");
  96. }
  97. // Static method imtype typemap attributes check
  98. {
  99. MethodInfo member = (MethodInfo)imclasstype.GetMember("Stations_Swindon")[0];
  100. if (Attribute.GetCustomAttribute(member, typeof(IntegerOutAttribute)) == null)
  101. throw new Exception("No IntegerOut attribute for " + member.Name);
  102. ParameterInfo parameter = member.GetParameters()[0]; // checking 1st parameter
  103. Attribute attribute = Attribute.GetCustomAttributes(parameter)[0];
  104. if (attribute.GetType() != typeof(IntegerInAttribute))
  105. throw new Exception("Expecting IntegerIn attribute");
  106. }
  107. //
  108. // attributes feature
  109. //
  110. Type moretype = typeof(MoreStations);
  111. // Constructor attributes feature check
  112. {
  113. ConstructorInfo member = (ConstructorInfo)moretype.GetConstructors()[0];
  114. if (Attribute.GetCustomAttribute(member, typeof(InterCity1Attribute)) == null)
  115. throw new Exception("MoreStations::MoreStations attribute failed");
  116. }
  117. // Non-static method attributes feature check
  118. {
  119. MethodInfo member = (MethodInfo)moretype.GetMember("Chippenham")[0];
  120. if (Attribute.GetCustomAttribute(member, typeof(InterCity2Attribute)) == null)
  121. throw new Exception("MoreStations::Chippenham attribute failed");
  122. }
  123. // Static method attributes feature check
  124. {
  125. MethodInfo member = (MethodInfo)moretype.GetMember("Bath")[0];
  126. if (Attribute.GetCustomAttribute(member, typeof(InterCity3Attribute)) == null)
  127. throw new Exception("MoreStations::Bath attribute failed");
  128. }
  129. // Non-static member variable attributes feature check
  130. {
  131. PropertyInfo member = (PropertyInfo)moretype.GetProperty("Bristol");
  132. if (Attribute.GetCustomAttribute(member, typeof(InterCity4Attribute)) == null)
  133. throw new Exception("MoreStations::Bristol attribute failed");
  134. }
  135. // Static member variable attributes feature check
  136. {
  137. PropertyInfo member = (PropertyInfo)moretype.GetProperty("WestonSuperMare");
  138. if (Attribute.GetCustomAttribute(member, typeof(InterCity5Attribute)) == null)
  139. throw new Exception("MoreStations::Bristol attribute failed");
  140. }
  141. // Global function attributes feature check
  142. {
  143. MethodInfo member = (MethodInfo)globaltype.GetMember("Paddington")[0];
  144. if (Attribute.GetCustomAttribute(member, typeof(InterCity7Attribute)) == null)
  145. throw new Exception("MoreStations::Paddington attribute failed");
  146. }
  147. // Global variables attributes feature check
  148. {
  149. PropertyInfo member = (PropertyInfo)globaltype.GetProperty("DidcotParkway");
  150. if (Attribute.GetCustomAttribute(member, typeof(InterCity8Attribute)) == null)
  151. throw new Exception("MoreStations::Paddington attribute failed");
  152. }
  153. //
  154. // csattribute typemaps
  155. //
  156. // Class csattribute typemap
  157. {
  158. Object[] attribs = moretype.GetCustomAttributes(true);
  159. Eurostar1Attribute tgv = (Eurostar1Attribute)attribs[0];
  160. if (tgv == null)
  161. throw new Exception("No attribute for MoreStations");
  162. }
  163. // Nested enum csattribute typemap
  164. {
  165. MemberInfo member = (MemberInfo)moretype.GetMember("Wales")[0];
  166. if (Attribute.GetCustomAttribute(member, typeof(Eurostar2Attribute)) == null)
  167. throw new Exception("No attribute for " + member.Name);
  168. }
  169. // Enum value attributes
  170. Type walesType = typeof(MoreStations.Wales);
  171. {
  172. MemberInfo member = (MemberInfo)walesType.GetMember("Cardiff")[0];
  173. DescriptionAttribute attribute = (DescriptionAttribute)Attribute.GetCustomAttribute(member, typeof(System.ComponentModel.DescriptionAttribute));
  174. if (attribute == null)
  175. throw new Exception("No attribute for " + member.Name);
  176. if (attribute.Description != "Cardiff city station")
  177. throw new Exception("Incorrect attribute value for " + member.Name);
  178. }
  179. {
  180. MemberInfo member = (MemberInfo)walesType.GetMember("Swansea")[0];
  181. DescriptionAttribute attribute = (DescriptionAttribute)Attribute.GetCustomAttribute(member, typeof(System.ComponentModel.DescriptionAttribute));
  182. if (attribute == null)
  183. throw new Exception("No attribute for " + member.Name);
  184. if (attribute.Description != "Swansea city station")
  185. throw new Exception("Incorrect attribute value for " + member.Name);
  186. }
  187. // Enum csattribute typemap
  188. {
  189. Type cymrutype = typeof(Cymru);
  190. Object[] attribs = cymrutype.GetCustomAttributes(true);
  191. Eurostar3Attribute tgv = (Eurostar3Attribute)attribs[0];
  192. if (tgv == null)
  193. throw new Exception("No attribute for Cymru");
  194. }
  195. // No runtime test for directorinattributes and directoroutattributes
  196. }
  197. }
  198. // Custom attribute classes
  199. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  200. public class IntInAttribute : Attribute {}
  201. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  202. public class IntOutAttribute : Attribute {}
  203. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  204. public class IntegerInAttribute : Attribute {}
  205. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  206. public class IntegerOutAttribute : Attribute {}
  207. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  208. public class InterCity1Attribute : Attribute {}
  209. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  210. public class InterCity2Attribute : Attribute {}
  211. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  212. public class InterCity3Attribute : Attribute {}
  213. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  214. public class InterCity4Attribute : Attribute {}
  215. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  216. public class InterCity5Attribute : Attribute {}
  217. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  218. public class InterCity6Attribute : Attribute {}
  219. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  220. public class InterCity7Attribute : Attribute {}
  221. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  222. public class InterCity8Attribute : Attribute {}
  223. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  224. public class Eurostar1Attribute : Attribute {}
  225. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  226. public class Eurostar2Attribute : Attribute {}
  227. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  228. public class Eurostar3Attribute : Attribute {}
  229. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  230. public class ThreadSafeAttribute : Attribute {
  231. public ThreadSafeAttribute(bool safe) {}
  232. public ThreadSafeAttribute() {}
  233. }
  234. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  235. public class DirectorIntegerOutAttribute : Attribute {}
  236. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  237. public class DirectorIntegerInAttribute : Attribute {}