2 // MethodOnTypeBuilderInstTest - NUnit Test Cases for MethodOnTypeBuilderInst
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // Copyright (C) 2008 Gert Driesen
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Globalization;
33 using System.Reflection;
34 using System.Reflection.Emit;
36 using NUnit.Framework;
38 namespace MonoTests.System.Reflection.Emit
41 public class ConstructorOnTypeBuilderInstTest
43 private static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.ConstructorOnTypeBuilderInstTest";
45 private AssemblyBuilder assembly;
46 private ModuleBuilder module;
47 private Type typeBarOfInt32;
48 private ConstructorBuilder cb;
49 private ConstructorInfo ci;
50 private TypeBuilder tb;
55 SetUp (AssemblyBuilderAccess.RunAndSave);
58 void SetUp (AssemblyBuilderAccess access)
60 AssemblyName assemblyName = new AssemblyName ();
61 assemblyName.Name = ASSEMBLY_NAME;
63 assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
67 module = assembly.DefineDynamicModule ("module1");
69 tb = module.DefineType ("Bar");
70 GenericTypeParameterBuilder [] typeParams = tb.DefineGenericParameters ("T");
72 cb = tb.DefineConstructor (MethodAttributes.Public,
73 CallingConventions.Standard,
74 new Type [] { typeof (string), typeof (int) });
75 ILGenerator ig = cb.GetILGenerator ();
76 ig.Emit (OpCodes.Ret);
78 typeBarOfInt32 = tb.MakeGenericType (typeof (int));
79 ci = TypeBuilder.GetConstructor (typeBarOfInt32, cb);
83 [Category ("NotWorking")]
84 public void Attributes ()
86 Assert.AreEqual (MethodAttributes.PrivateScope |
87 MethodAttributes.Public | MethodAttributes.SpecialName,
90 Assert.AreEqual (MethodAttributes.PrivateScope |
91 MethodAttributes.Public | MethodAttributes.SpecialName,
96 [Category ("NotWorking")]
97 public void CallingConvention ()
99 Assert.AreEqual (CallingConventions.HasThis,
100 ci.CallingConvention, "#1");
102 Assert.AreEqual (CallingConventions.HasThis,
103 ci.CallingConvention, "#2");
107 public void ContainsGenericParameters ()
109 Assert.IsFalse (ci.ContainsGenericParameters, "#1");
111 Assert.IsFalse (ci.ContainsGenericParameters, "#2");
115 public void DeclaringType ()
117 Assert.AreSame (typeBarOfInt32, ci.DeclaringType, "#1");
119 Assert.AreSame (typeBarOfInt32, ci.DeclaringType, "#2");
122 [Test] // GetCustomAttributes (Boolean)
123 public void GetCustomAttributes1 ()
126 ci.GetCustomAttributes (false);
128 } catch (NotSupportedException ex) {
129 // The invoked member is not supported in a dynamic module
130 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
131 Assert.IsNull (ex.InnerException, "#A3");
132 Assert.IsNotNull (ex.Message, "#A4");
138 ci.GetCustomAttributes (false);
140 } catch (NotSupportedException ex) {
141 // The invoked member is not supported in a dynamic module
142 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
143 Assert.IsNull (ex.InnerException, "#B3");
144 Assert.IsNotNull (ex.Message, "#B4");
148 [Test] // GetCustomAttributes (Type, Boolean)
149 public void GetCustomAttributes2 ()
152 ci.GetCustomAttributes (typeof (FlagsAttribute), false);
154 } catch (NotSupportedException ex) {
155 // The invoked member is not supported in a dynamic module
156 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
157 Assert.IsNull (ex.InnerException, "#A3");
158 Assert.IsNotNull (ex.Message, "#A4");
164 ci.GetCustomAttributes (typeof (FlagsAttribute), false);
166 } catch (NotSupportedException ex) {
167 // The invoked member is not supported in a dynamic module
168 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
169 Assert.IsNull (ex.InnerException, "#B3");
170 Assert.IsNotNull (ex.Message, "#B4");
175 public void GetGenericArguments ()
178 ci.GetGenericArguments ();
180 } catch (NotSupportedException ex) {
181 // Derived classes must provide an implementation
182 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
183 Assert.IsNull (ex.InnerException, "#A3");
184 Assert.IsNotNull (ex.Message, "#A4");
190 ci.GetGenericArguments ();
192 } catch (NotSupportedException ex) {
193 // Derived classes must provide an implementation
194 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
195 Assert.IsNull (ex.InnerException, "#B3");
196 Assert.IsNotNull (ex.Message, "#B4");
201 public void GetMethodImplementationFlags ()
203 Assert.AreEqual (MethodImplAttributes.Managed,
204 ci.GetMethodImplementationFlags (), "#1");
206 Assert.AreEqual (MethodImplAttributes.Managed,
207 ci.GetMethodImplementationFlags (), "#2");
211 public void GetParameters ()
216 } catch (NotSupportedException ex) {
217 // Type has not been created
218 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
219 Assert.IsNull (ex.InnerException, "#A3");
220 Assert.IsNotNull (ex.Message, "#A4");
225 ParameterInfo [] parameters = ci.GetParameters ();
226 Assert.IsNotNull (parameters, "#B1");
227 Assert.AreEqual (2, parameters.Length, "#B2");
229 Assert.AreEqual (ParameterAttributes.None, parameters [0].Attributes, "#C1");
230 Assert.IsNull (parameters [0].Name, "#C2");
231 Assert.AreEqual (typeof (string), parameters [0].ParameterType, "#C3");
232 Assert.AreEqual (0, parameters [0].Position, "#C4");
234 Assert.AreEqual (ParameterAttributes.None, parameters [1].Attributes, "#D1");
235 Assert.IsNull (parameters [1].Name, "#D2");
236 Assert.AreEqual (typeof (int), parameters [1].ParameterType, "#D3");
237 Assert.AreEqual (1, parameters [1].Position, "#D4");
240 [Test] // Invoke (Object [])
241 public void Invoke1 ()
244 ci.Invoke (new object [0]);
246 } catch (InvalidOperationException ex) {
247 // Operation is not valid due to the current
248 // state of the object
249 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
250 Assert.IsNull (ex.InnerException, "#A3");
251 Assert.IsNotNull (ex.Message, "#A4");
257 ci.Invoke (new object [0]);
259 } catch (InvalidOperationException ex) {
260 // Operation is not valid due to the current
261 // state of the object
262 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
263 Assert.IsNull (ex.InnerException, "#B3");
264 Assert.IsNotNull (ex.Message, "#B4");
268 [Test] // Invoke (Object, Object [])
269 public void Invoke2 ()
272 ci.Invoke (null, new object [0]);
274 } catch (NotSupportedException ex) {
275 // Specified method is not supported
276 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
277 Assert.IsNull (ex.InnerException, "#A3");
278 Assert.IsNotNull (ex.Message, "#A4");
284 ci.Invoke (null, new object [0]);
286 } catch (NotSupportedException ex) {
287 // Specified method is not supported
288 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
289 Assert.IsNull (ex.InnerException, "#B3");
290 Assert.IsNotNull (ex.Message, "#B4");
294 [Test] // Invoke (BindingFlags, Binder, Object [], CultureInfo)
295 public void Invoke3 ()
298 ci.Invoke (BindingFlags.Default, null, new object [0],
299 CultureInfo.InvariantCulture);
301 } catch (InvalidOperationException ex) {
302 // Operation is not valid due to the current
303 // state of the object
304 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
305 Assert.IsNull (ex.InnerException, "#A3");
306 Assert.IsNotNull (ex.Message, "#A4");
312 ci.Invoke (BindingFlags.Default, null, new object [0],
313 CultureInfo.InvariantCulture);
315 } catch (InvalidOperationException ex) {
316 // Operation is not valid due to the current
317 // state of the object
318 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
319 Assert.IsNull (ex.InnerException, "#B3");
320 Assert.IsNotNull (ex.Message, "#B4");
324 [Test] // Invoke (Object, BindingFlags, Binder, Object [], CultureInfo)
325 public void Invoke4 ()
328 ci.Invoke (null, BindingFlags.Default, null,
329 new object [0], CultureInfo.InvariantCulture);
331 } catch (NotSupportedException ex) {
332 // Specified method is not supported
333 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
334 Assert.IsNull (ex.InnerException, "#A3");
335 Assert.IsNotNull (ex.Message, "#A4");
341 ci.Invoke (null, BindingFlags.Default, null,
342 new object [0], CultureInfo.InvariantCulture);
344 } catch (NotSupportedException ex) {
345 // Specified method is not supported
346 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
347 Assert.IsNull (ex.InnerException, "#B3");
348 Assert.IsNotNull (ex.Message, "#B4");
353 public void IsDefined ()
356 ci.IsDefined (typeof (FlagsAttribute), false);
358 } catch (NotSupportedException ex) {
359 // The invoked member is not supported in a dynamic module
360 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
361 Assert.IsNull (ex.InnerException, "#A3");
362 Assert.IsNotNull (ex.Message, "#A4");
368 ci.IsDefined (typeof (FlagsAttribute), false);
370 } catch (NotSupportedException ex) {
371 // The invoked member is not supported in a dynamic module
372 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
373 Assert.IsNull (ex.InnerException, "#B3");
374 Assert.IsNotNull (ex.Message, "#B4");
379 public void IsGenericMethodDefinition ()
381 Assert.IsFalse (ci.IsGenericMethodDefinition, "#1");
383 Assert.IsFalse (ci.IsGenericMethodDefinition, "#2");
387 public void IsGenericMethod ()
389 Assert.IsFalse (ci.IsGenericMethod, "#1");
391 Assert.IsFalse (ci.IsGenericMethod, "#2");
395 public void MemberType ()
397 Assert.AreEqual (MemberTypes.Constructor, ci.MemberType, "#1");
399 Assert.AreEqual (MemberTypes.Constructor, ci.MemberType, "#2");
403 public void MethodHandle ()
406 RuntimeMethodHandle handle = ci.MethodHandle;
407 Assert.Fail ("#A1:" + handle);
408 } catch (NotSupportedException ex) {
409 // The invoked member is not supported in a dynamic module
410 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
411 Assert.IsNull (ex.InnerException, "#A3");
412 Assert.IsNotNull (ex.Message, "#A4");
418 RuntimeMethodHandle handle = ci.MethodHandle;
419 Assert.Fail ("#B1:" + handle);
420 } catch (NotSupportedException ex) {
421 // The invoked member is not supported in a dynamic module
422 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
423 Assert.IsNull (ex.InnerException, "#B3");
424 Assert.IsNotNull (ex.Message, "#B4");
429 public void Module ()
431 Assert.AreSame (module, ci.Module, "#1");
433 Assert.AreSame (module, ci.Module, "#2");
439 Assert.AreEqual (".ctor", ci.Name, "#1");
441 Assert.AreEqual (".ctor", ci.Name, "#2");
445 public void ReflectedType ()
447 Assert.AreSame (typeBarOfInt32, ci.ReflectedType, "#1");
449 Assert.AreSame (typeBarOfInt32, ci.ReflectedType, "#2");
453 [Category ("NotDotNet")]
454 // CompilerContext no longer supported
455 [Category ("NotWorking")]
456 public void MetadataTokenWorksUnderCompilerContext ()
458 SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
459 int cb_token = cb.MetadataToken;
460 int inst_token = ci.MetadataToken;
461 Assert.AreEqual (cb_token, inst_token, "#1");