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