PageRenderTime 38ms CodeModel.GetById 2ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

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

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