PageRenderTime 30ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-25/SWIG/Examples/test-suite/csharp/csharp_attributes_runme.cs

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