2007-11-16 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mcs / class / corlib / Test / System / TypeTest.cs
1 // TypeTest.cs - NUnit Test Cases for the System.Type class
2 //
3 // Authors:
4 //      Zoltan Varga (vargaz@freemail.hu)
5 //  Patrik Torstensson
6 //
7 // (C) 2003 Ximian, Inc.  http://www.ximian.com
8 // 
9
10 using NUnit.Framework;
11 using System;
12 using System.Collections;
13 #if NET_2_0
14 using System.Collections.Generic;
15 #endif
16 using System.IO;
17 using System.Reflection;
18 using System.Reflection.Emit;
19 using System.Runtime.InteropServices;
20 using System.Text;
21 using System.Globalization;
22
23 class NoNamespaceClass {
24 }
25
26 namespace MonoTests.System
27 {
28         class Super : ICloneable
29         {
30                 public virtual object Clone ()
31                 {
32                         return null;
33                 }
34         }
35
36         class Duper: Super
37         {
38         }
39
40         interface IFace1
41         {
42                 void foo ();
43         }
44
45         interface IFace2 : IFace1
46         {
47                 void bar ();
48         }
49
50         interface IFace3 : IFace2
51         {
52         }
53
54         enum TheEnum
55         {
56                 A,
57                 B,
58                 C
59         };
60
61         abstract class Base
62         {
63                 public int level;
64
65                 public abstract int this [byte i] {
66                         get;
67                 }
68
69                 public abstract int this [int i] {
70                         get;
71                 }
72
73                 public abstract void TestVoid ();
74                 public abstract void TestInt (int i);
75         }
76
77         class DeriveVTable : Base
78         {
79                 public override int this [byte i] {
80                         get { return 1; }
81                 }
82
83                 public override int this [int i] {
84                         get { return 1; }
85                 }
86
87                 public override void TestVoid ()
88                 {
89                         level = 1;
90                 }
91
92                 public override void TestInt (int i)
93                 {
94                         level = 1;
95                 }
96         }
97
98         class NewVTable : DeriveVTable
99         {
100                 public new int this [byte i] {
101                         get { return 2; }
102                 }
103
104                 public new int this [int i] {
105                         get { return 2; }
106                 }
107
108                 public new void TestVoid ()
109                 {
110                         level = 2;
111                 }
112
113                 public new void TestInt (int i)
114                 {
115                         level = 2;
116                 }
117
118                 public void Overload ()
119                 {
120                 }
121
122                 public void Overload (int i)
123                 {
124                 }
125
126                 public NewVTable (out int i)
127                 {
128                         i = 0;
129                 }
130
131                 public void byref_method (out int i)
132                 {
133                         i = 0;
134                 }
135         }
136
137         class Base1
138         {
139                 public virtual int Foo {
140                         get { return 1; }
141                         set { }
142                 }
143         }
144
145         class Derived1 : Base1
146         {
147                 public override int Foo {
148                         set { }
149                 }
150         }
151
152         class Derived2 : Base1
153         {
154                 public new int Foo {
155                         get { return 1; }
156                         set { }
157                 }
158         }
159
160 #if NET_2_0
161         public class Foo<T>
162         {
163                 public T Whatever;
164         
165                 public T Test {
166                         get { throw new NotImplementedException (); }
167                 }
168
169                 public T Execute (T a)
170                 {
171                         return a;
172                 }
173         }
174
175         public interface IBar<T>
176         {
177         }
178
179         public class Baz<T> : IBar<T>
180         {
181         }
182 #endif
183
184         [TestFixture]
185         public class TypeTest
186         {
187                 private AssemblyBuilder assembly;
188                 private ModuleBuilder module;
189                 const string ASSEMBLY_NAME = "MonoTests.System.TypeTest";
190                 static int typeIndexer = 0;
191
192                 [SetUp]
193                 public void SetUp ()
194                 {
195                         AssemblyName assemblyName = new AssemblyName ();
196                         assemblyName.Name = ASSEMBLY_NAME;
197                         assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
198                                         assemblyName, AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
199                         module = assembly.DefineDynamicModule ("module1");
200                 }
201
202                 private string genTypeName ()
203                 {
204                         return "t" + (typeIndexer++);
205                 }
206
207                 private void ByrefMethod (ref int i, ref Derived1 j, ref Base1 k)
208                 {
209                 }
210 #if NET_2_0
211                 private void GenericMethod<Q> (Q q)
212                 {
213                 }
214 #endif
215                 [Test]
216                 public void TestIsAssignableFrom ()
217                 {
218                         // Simple tests for inheritance
219                         Assert.AreEqual (typeof (Super).IsAssignableFrom (typeof (Duper)) , true, "#01");
220                         Assert.AreEqual (typeof (Duper).IsAssignableFrom (typeof (Duper)), true, "#02");
221                         Assert.AreEqual (typeof (Object).IsAssignableFrom (typeof (Duper)), true, "#03");
222                         Assert.AreEqual (typeof (ICloneable).IsAssignableFrom (typeof (Duper)), true, "#04");
223
224                         // Tests for arrays
225                         Assert.AreEqual (typeof (Super[]).IsAssignableFrom (typeof (Duper[])), true, "#05");
226                         Assert.AreEqual (typeof (Duper[]).IsAssignableFrom (typeof (Super[])), false, "#06");
227                         Assert.AreEqual (typeof (Object[]).IsAssignableFrom (typeof (Duper[])), true, "#07");
228                         Assert.AreEqual (typeof (ICloneable[]).IsAssignableFrom (typeof (Duper[])), true, "#08");
229
230                         // Tests for multiple dimensional arrays
231                         Assert.AreEqual (typeof (Super[][]).IsAssignableFrom (typeof (Duper[][])), true, "#09");
232                         Assert.AreEqual (typeof (Duper[][]).IsAssignableFrom (typeof (Super[][])), false, "#10");
233                         Assert.AreEqual (typeof (Object[][]).IsAssignableFrom (typeof (Duper[][])), true, "#11");
234                         Assert.AreEqual (typeof (ICloneable[][]).IsAssignableFrom (typeof (Duper[][])), true, "#12");
235
236                         // Tests for vectors<->one dimensional arrays */
237 #if TARGET_JVM // Lower bounds arrays are not supported for TARGET_JVM.
238                         Array arr1 = Array.CreateInstance (typeof (int), new int[] {1});
239                         Assert.AreEqual (typeof (int[]).IsAssignableFrom (arr1.GetType ()), true, "#13");
240 #else
241                         Array arr1 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0});
242                         Array arr2 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {10});
243
244                         Assert.AreEqual (typeof (int[]).IsAssignableFrom (arr1.GetType ()), true, "#13");
245                         Assert.AreEqual (typeof (int[]).IsAssignableFrom (arr2.GetType ()), false, "#14");
246 #endif // TARGET_JVM
247
248                         // Test that arrays of enums can be cast to their base types
249                         Assert.AreEqual (typeof (int[]).IsAssignableFrom (typeof (TypeCode[])), true, "#15");
250
251                         // Test that arrays of valuetypes can't be cast to arrays of
252                         // references
253                         Assert.AreEqual (typeof (object[]).IsAssignableFrom (typeof (TypeCode[])), false, "#16");
254                         Assert.AreEqual (typeof (ValueType[]).IsAssignableFrom (typeof (TypeCode[])), false, "#17");
255                         Assert.AreEqual (typeof (Enum[]).IsAssignableFrom (typeof (TypeCode[])), false, "#18");
256
257                         // Test that arrays of enums can't be cast to arrays of references
258                         Assert.AreEqual (typeof (object[]).IsAssignableFrom (typeof (TheEnum[])), false, "#19");
259                         Assert.AreEqual (typeof (ValueType[]).IsAssignableFrom (typeof (TheEnum[])), false, "#20");
260                         Assert.AreEqual (typeof (Enum[]).IsAssignableFrom (typeof (TheEnum[])), false, "#21");
261
262                         // Check that ValueType and Enum are recognized as reference types
263                         Assert.AreEqual (typeof (object).IsAssignableFrom (typeof (ValueType)), true, "#22");
264                         Assert.AreEqual (typeof (object).IsAssignableFrom (typeof (Enum)), true, "#23");
265                         Assert.AreEqual (typeof (ValueType).IsAssignableFrom (typeof (Enum)), true, "#24");
266
267                         Assert.AreEqual (typeof (object[]).IsAssignableFrom (typeof (ValueType[])), true, "#25");
268                         Assert.AreEqual (typeof (ValueType[]).IsAssignableFrom (typeof (ValueType[])), true, "#26");
269                         Assert.AreEqual (typeof (Enum[]).IsAssignableFrom (typeof (ValueType[])), false, "#27");
270
271                         Assert.AreEqual (typeof (object[]).IsAssignableFrom (typeof (Enum[])), true, "#28");
272                         Assert.AreEqual (typeof (ValueType[]).IsAssignableFrom (typeof (Enum[])), true, "#29");
273                         Assert.AreEqual (typeof (Enum[]).IsAssignableFrom (typeof (Enum[])), true, "#30");
274
275                         // Tests for byref types
276                         MethodInfo mi = typeof (TypeTest).GetMethod ("ByrefMethod", BindingFlags.Instance|BindingFlags.NonPublic);
277                         Assert.IsTrue (mi.GetParameters ()[2].ParameterType.IsAssignableFrom (mi.GetParameters ()[1].ParameterType));
278                         Assert.IsTrue (mi.GetParameters ()[1].ParameterType.IsAssignableFrom (mi.GetParameters ()[1].ParameterType));
279
280                         // Tests for type parameters
281 #if NET_2_0
282                         mi = typeof (TypeTest).GetMethod ("GenericMethod", BindingFlags.Instance|BindingFlags.NonPublic);
283                         Assert.IsTrue (mi.GetParameters ()[0].ParameterType.IsAssignableFrom (mi.GetParameters ()[0].ParameterType));
284                         Assert.IsFalse (mi.GetParameters ()[0].ParameterType.IsAssignableFrom (typeof (int)));
285 #endif
286                 }
287
288                 [Test]
289                 public void TestIsSubclassOf ()
290                 {
291                         Assert.IsTrue (typeof (ICloneable).IsSubclassOf (typeof (object)), "#01");
292
293                         // Tests for byref types
294                         Type paramType = typeof (TypeTest).GetMethod ("ByrefMethod", BindingFlags.Instance|BindingFlags.NonPublic).GetParameters () [0].ParameterType;
295                         Assert.IsTrue (!paramType.IsSubclassOf (typeof (ValueType)), "#02");
296                         //Assert.IsTrue (paramType.IsSubclassOf (typeof (Object)), "#03");
297                         Assert.IsTrue (!paramType.IsSubclassOf (paramType), "#04");
298                 }
299
300                 [Test]
301                 public void TestGetMethodImpl ()
302                 {
303                         // Test binding of new slot methods (using no types)
304                         Assert.AreEqual (typeof (Base), typeof (Base).GetMethod("TestVoid").DeclaringType, "#01");
305                         Assert.AreEqual (typeof (NewVTable), typeof (NewVTable).GetMethod ("TestVoid").DeclaringType, "#02");
306
307                         // Test binding of new slot methods (using types)
308                         Assert.AreEqual (typeof (Base), typeof (Base).GetMethod ("TestInt", new Type[] { typeof (int) }).DeclaringType, "#03");
309                         Assert.AreEqual (typeof (NewVTable), typeof (NewVTable).GetMethod ("TestInt", new Type[] { typeof (int) }).DeclaringType, "#04");
310
311                         // Test overload resolution
312                         Assert.AreEqual (0, typeof (NewVTable).GetMethod ("Overload", new Type[0]).GetParameters ().Length, "#05");
313
314                         // Test byref parameters
315                         Assert.AreEqual (null, typeof (NewVTable).GetMethod ("byref_method", new Type[] { typeof (int) }), "#06");
316                         Type byrefInt = typeof (NewVTable).GetMethod ("byref_method").GetParameters ()[0].ParameterType;
317                         Assert.IsNotNull (typeof (NewVTable).GetMethod ("byref_method", new Type[] { byrefInt }), "#07");
318                 }
319
320                 [Test]
321                 [Category ("TargetJvmNotWorking")]
322                 public void TestGetPropertyImpl ()
323                 {
324                         // Test getting property that is exact
325                         Assert.AreEqual (typeof (NewVTable), typeof (NewVTable).GetProperty ("Item", new Type[1] { typeof (Int32) }).DeclaringType, "#01");
326
327                         // Test getting property that is not exact
328                         Assert.AreEqual (typeof (NewVTable), typeof (NewVTable).GetProperty ("Item", new Type[1] { typeof (Int16) }).DeclaringType, "#02");
329
330                         // Test overriding of properties when only the set accessor is overriden
331                         Assert.AreEqual (1, typeof (Derived1).GetProperties ().Length, "#03");
332                 }
333
334                 [Test]
335                 public void GetProperties ()
336                 {
337                         // Test hide-by-name-and-signature
338                         Assert.AreEqual (1, typeof (Derived2).GetProperties ().Length);
339                         Assert.AreEqual (typeof (Derived2), typeof (Derived2).GetProperties ()[0].DeclaringType);
340                 }
341
342 #if !TARGET_JVM // StructLayout not supported for TARGET_JVM
343                 [StructLayout(LayoutKind.Explicit, Pack = 4, Size = 64)]
344                 public class Class1
345                 {
346                 }
347
348                 [StructLayout(LayoutKind.Explicit, CharSet=CharSet.Unicode)]
349                 public class Class2
350                 {
351                 }
352
353 #if NET_2_0
354                 [Test]
355                 public void StructLayoutAttribute ()
356                 {
357                         StructLayoutAttribute attr1 = typeof (TypeTest).StructLayoutAttribute;
358                         Assert.AreEqual (LayoutKind.Auto, attr1.Value);
359
360                         StructLayoutAttribute attr2 = typeof (Class1).StructLayoutAttribute;
361                         Assert.AreEqual (LayoutKind.Explicit, attr2.Value);
362                         Assert.AreEqual (4, attr2.Pack);
363                         Assert.AreEqual (64, attr2.Size);
364
365                         StructLayoutAttribute attr3 = typeof (Class2).StructLayoutAttribute;
366                         Assert.AreEqual (LayoutKind.Explicit, attr3.Value);
367                         Assert.AreEqual (CharSet.Unicode, attr3.CharSet);
368                 }
369 #endif
370 #endif // TARGET_JVM
371
372                 [Test]
373                 public void Namespace ()
374                 {
375                         Assert.AreEqual (null, typeof (NoNamespaceClass).Namespace);
376                 }
377
378                 public static void Reflected (ref int a)
379                 {
380                 }
381
382                 [Test]
383                 public void Name ()
384                 {
385                         Assert.AreEqual ("Int32&", typeof (TypeTest).GetMethod ("Reflected").GetParameters () [0].ParameterType.Name);
386                 }
387
388                 [Test]
389                 public void GetInterfaces ()
390                 {
391                         Type[] t = typeof (Duper).GetInterfaces ();
392                         Assert.AreEqual (1, t.Length);
393                         Assert.AreEqual (typeof (ICloneable), t[0]);
394
395                         Type[] t2 = typeof (IFace3).GetInterfaces ();
396                         Assert.AreEqual (2, t2.Length);
397                 }
398
399                 public int AField;
400
401                 [Test]
402                 public void GetFieldIgnoreCase ()
403                 {
404                         Assert.IsNotNull (typeof (TypeTest).GetField ("afield", BindingFlags.Instance|BindingFlags.Public|BindingFlags.IgnoreCase));
405                 }
406
407 #if NET_2_0
408                 public int Count {
409                         internal get {
410                                 return 0;
411                         }
412
413                         set {
414                         }
415                 }
416
417                 [Test]
418                 public void GetPropertyAccessorModifiers ()
419                 {
420                         Assert.IsNotNull (typeof (TypeTest).GetProperty ("Count", BindingFlags.Instance | BindingFlags.Public));
421                         Assert.IsNull (typeof (TypeTest).GetProperty ("Count", BindingFlags.Instance | BindingFlags.NonPublic));
422                 }
423 #endif
424
425                 [Test]
426                 public void IsAbstract ()
427                 {
428                         Assert.IsFalse (typeof (string).IsAbstract, "#1");
429                         Assert.IsTrue (typeof (ICloneable).IsAbstract, "#2");
430                         Assert.IsTrue (typeof (ValueType).IsAbstract, "#3");
431                         Assert.IsTrue (typeof (Enum).IsAbstract, "#4");
432                         Assert.IsFalse (typeof (TimeSpan).IsAbstract, "#5");
433                         Assert.IsTrue (typeof (TextReader).IsAbstract, "#6");
434
435 #if NET_2_0
436                         // LAMESPEC:
437                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=286308
438                         Type [] typeArgs = typeof (List<>).GetGenericArguments ();
439                         Assert.IsFalse (typeArgs [0].IsAbstract, "#7");
440 #endif
441                 }
442
443                 [Test]
444                 public void IsCOMObject ()
445                 {
446                         Type type = typeof (string);
447                         Assert.IsFalse (type.IsCOMObject, "#1");
448
449                         TypeBuilder tb = module.DefineType (genTypeName ());
450                         type = tb.CreateType ();
451                         Assert.IsFalse (type.IsCOMObject, "#2");
452                 }
453
454                 [Test]
455                 public void IsImport ()
456                 {
457                         Type type = typeof (string);
458                         Assert.IsFalse (type.IsImport, "#1");
459
460                         TypeBuilder tb = module.DefineType (genTypeName ());
461                         type = tb.CreateType ();
462                         Assert.IsFalse (type.IsImport, "#2");
463
464                         tb = module.DefineType (genTypeName (), TypeAttributes.Import |
465                                 TypeAttributes.Interface | TypeAttributes.Abstract);
466                         type = tb.CreateType ();
467                         Assert.IsTrue (type.IsImport, "#3");
468                 }
469
470                 [Test]
471                 public void IsInterface ()
472                 {
473                         Assert.IsFalse (typeof (string).IsInterface, "#1");
474                         Assert.IsTrue (typeof (ICloneable).IsInterface, "#2");
475                 }
476
477                 [Test]
478                 public void IsPrimitive () {
479                         Assert.IsTrue (typeof (IntPtr).IsPrimitive, "#1");
480                         Assert.IsTrue (typeof (int).IsPrimitive, "#2");
481                         Assert.IsFalse (typeof (string).IsPrimitive, "#2");
482                 }
483
484                 [Test]
485                 public void IsValueType ()
486                 {
487                         Assert.IsTrue (typeof (int).IsValueType, "#1");
488                         Assert.IsFalse (typeof (Enum).IsValueType, "#2");
489                         Assert.IsFalse (typeof (ValueType).IsValueType, "#3");
490                         Assert.IsTrue (typeof (AttributeTargets).IsValueType, "#4");
491                         Assert.IsFalse (typeof (string).IsValueType, "#5");
492                         Assert.IsTrue (typeof (TimeSpan).IsValueType, "#6");
493                 }
494
495                 [Test]
496                 [Category("NotDotNet")]
497                 // Depends on the GAC working, which it doesn't durring make distcheck.
498                 [Category ("NotWorking")]
499                 public void GetTypeWithWhitespace ()
500                 {
501                         Assert.IsNotNull (Type.GetType
502                                                    (@"System.Configuration.NameValueSectionHandler,
503                         System,
504 Version=1.0.5000.0,
505 Culture=neutral
506 ,
507 PublicKeyToken=b77a5c561934e089"));
508                 }
509                 
510                 [Test]
511                 public void ExerciseFilterName ()
512                 {
513                         MemberInfo[] mi = typeof(Base).FindMembers(
514                                 MemberTypes.Method, 
515                                 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
516                                 BindingFlags.Instance | BindingFlags.DeclaredOnly,
517                                 Type.FilterName, "*");
518                         Assert.AreEqual (4, mi.Length);
519                         mi = typeof(Base).FindMembers(
520                                 MemberTypes.Method, 
521                                 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
522                                 BindingFlags.Instance | BindingFlags.DeclaredOnly,
523                                 Type.FilterName, "Test*");
524                         Assert.AreEqual (2, mi.Length);
525                         mi = typeof(Base).FindMembers(
526                                 MemberTypes.Method, 
527                                 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
528                                 BindingFlags.Instance | BindingFlags.DeclaredOnly,
529                                 Type.FilterName, "TestVoid");
530                         Assert.AreEqual (1, mi.Length);
531                         mi = typeof(Base).FindMembers(
532                                 MemberTypes.Method, 
533                                 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
534                                 BindingFlags.Instance | BindingFlags.DeclaredOnly,
535                                 Type.FilterName, "NonExistingMethod");
536                         Assert.AreEqual (0, mi.Length);
537                 }
538                 
539                 [Test]
540                 public void ExerciseFilterNameIgnoreCase ()
541                 {
542                         MemberInfo[] mi = typeof(Base).FindMembers(
543                                 MemberTypes.Method, 
544                                 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
545                                 BindingFlags.Instance | BindingFlags.DeclaredOnly,
546                                 Type.FilterNameIgnoreCase, "*");
547                         Assert.AreEqual (4, mi.Length);
548                         mi = typeof(Base).FindMembers(
549                                 MemberTypes.Method, 
550                                 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
551                                 BindingFlags.Instance | BindingFlags.DeclaredOnly,
552                                 Type.FilterNameIgnoreCase, "test*");
553                         Assert.AreEqual (2, mi.Length);
554                         mi = typeof(Base).FindMembers(
555                                 MemberTypes.Method, 
556                                 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
557                                 BindingFlags.Instance | BindingFlags.DeclaredOnly,
558                                 Type.FilterNameIgnoreCase, "TESTVOID");
559                         Assert.AreEqual (1, mi.Length);
560                         mi = typeof(Base).FindMembers(
561                                 MemberTypes.Method, 
562                                 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
563                                 BindingFlags.Instance | BindingFlags.DeclaredOnly,
564                                 Type.FilterNameIgnoreCase, "NonExistingMethod");
565                         Assert.AreEqual (0, mi.Length);
566                 }
567
568                 public class ByRef0
569                 {
570                         public int field;
571                         public int property {
572                                 get { return 0; }
573                         }
574                         public ByRef0 (int i) {}
575                         public void f (int i) {}
576                 }
577
578                 [Test]
579                 public void ByrefTypes ()
580                 {
581                         Type t = Type.GetType ("MonoTests.System.TypeTest+ByRef0&");
582                         Assert.IsNotNull (t);
583                         Assert.IsTrue (t.IsByRef);
584                         Assert.AreEqual (0, t.GetMethods (BindingFlags.Public | BindingFlags.Instance).Length);
585                         Assert.AreEqual (0, t.GetConstructors (BindingFlags.Public | BindingFlags.Instance).Length);
586                         Assert.AreEqual (0, t.GetEvents (BindingFlags.Public | BindingFlags.Instance).Length);
587                         Assert.AreEqual (0, t.GetProperties (BindingFlags.Public | BindingFlags.Instance).Length);
588
589                         Assert.IsNull (t.GetMethod ("f"));
590                         Assert.IsNull (t.GetField ("field"));
591                         Assert.IsNull (t.GetProperty ("property"));
592                 }
593                 
594                 [Test]
595                 public void TestAssemblyQualifiedName ()
596                 {
597                         Type t = Type.GetType ("System.Byte[]&");
598                         Assert.IsTrue (t.AssemblyQualifiedName.StartsWith ("System.Byte[]&"));
599                         
600                         t = Type.GetType ("System.Byte*&");
601                         Assert.IsTrue (t.AssemblyQualifiedName.StartsWith ("System.Byte*&"));
602                         
603                         t = Type.GetType ("System.Byte&");
604                         Assert.IsTrue (t.AssemblyQualifiedName.StartsWith ("System.Byte&"));
605                 }
606
607                 struct B
608                 {
609                         int value;
610                 }
611
612                 [Test]
613                 public void CreateValueTypeNoCtor ()
614                 {
615                         typeof(B).InvokeMember ("", BindingFlags.CreateInstance, null, null, null);
616                 }
617
618                 [Test]
619                 [ExpectedException (typeof (MissingMethodException))]
620                 public void CreateValueTypeNoCtorArgs ()
621                 {
622                         typeof(B).InvokeMember ("", BindingFlags.CreateInstance, null, null, new object [] { 1 });
623                 }
624
625                 static string bug336841 (string param1, params string [] param2)
626                 {
627                         StringBuilder sb = new StringBuilder ();
628                         sb.Append ("#A:");
629                         sb.Append (param1);
630                         sb.Append ("|");
631                         for (int i = 0; i < param2.Length; i++) {
632                                 if (i > 0)
633                                         sb.Append (",");
634                                 sb.Append (param2 [i]);
635                         }
636                         return sb.ToString ();
637                 }
638
639                 static string bug336841 (string param1)
640                 {
641                         return "#B:" + param1;
642                 }
643
644                 static string bug336841 (params string [] param1)
645                 {
646                         StringBuilder sb = new StringBuilder ();
647                         sb.Append ("#C:");
648                         for (int i = 0; i < param1.Length; i++) {
649                                 if (i > 0)
650                                         sb.Append (";");
651                                 sb.Append (param1 [i]);
652                         }
653                         return sb.ToString ();
654                 }
655
656                 [Test]
657                 public void InvokeMember_GetSetField ()
658                 {
659                         typeof (X).InvokeMember ("Value", BindingFlags.Public |
660                                 BindingFlags.Static | BindingFlags.FlattenHierarchy |
661                                 BindingFlags.SetField, null, null, new object [] { 5 });
662
663                         Assert.AreEqual (5, X.Value, "#A1");
664                         Assert.AreEqual (5, typeof (X).InvokeMember ("Value",
665                                 BindingFlags.Public | BindingFlags.Static |
666                                 BindingFlags.FlattenHierarchy | BindingFlags.GetField,
667                                 null, null, new object [0]), "#A2");
668                         Assert.AreEqual (5, Y.Value, "#A3");
669                         Assert.AreEqual (5, typeof (Y).InvokeMember ("Value",
670                                 BindingFlags.Public | BindingFlags.Static |
671                                 BindingFlags.FlattenHierarchy | BindingFlags.GetField,
672                                 null, null, new object [0]), "#A4");
673
674                         try {
675                                 typeof (X).InvokeMember ("Value", BindingFlags.Public |
676                                         BindingFlags.Static | BindingFlags.FlattenHierarchy |
677                                         BindingFlags.GetField | BindingFlags.SetField,
678                                         null, null, new object [] { 5 });
679                                 Assert.Fail ("#B1");
680                         } catch (ArgumentException ex) {
681                                 // Cannot specify both Get and Set on a field
682                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
683                                 Assert.IsNull (ex.InnerException, "#B3");
684                                 Assert.IsNotNull (ex.Message, "#B4");
685                                 Assert.IsNotNull (ex.ParamName, "#B5");
686 #if NET_2_0
687                                 Assert.AreEqual ("bindingFlags", ex.ParamName, "#B6");
688 #else
689                                 Assert.AreEqual ("invokeAttr", ex.ParamName, "#B6");
690 #endif
691                         }
692                 }
693
694                 [Test]
695                 public void InvokeMember_GetSetProperty ()
696                 {
697                         try {
698                                 typeof (ArrayList).InvokeMember ("Item",
699                                         BindingFlags.GetProperty | BindingFlags.SetProperty |
700                                         BindingFlags.Instance | BindingFlags.Public,
701                                         null, new ArrayList (), new object [] { 0, "bar" });
702                                 Assert.Fail ("#1");
703                         } catch (ArgumentException ex) {
704                                 // Cannot specify both Get and Set on a property
705                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
706                                 Assert.IsNull (ex.InnerException, "#3");
707                                 Assert.IsNotNull (ex.Message, "#4");
708                                 Assert.IsNotNull (ex.ParamName, "#5");
709 #if NET_2_0
710                                 Assert.AreEqual ("bindingFlags", ex.ParamName, "#6");
711 #else
712                                 Assert.AreEqual ("invokeAttr", ex.ParamName, "#6");
713 #endif
714                         }
715                 }
716
717
718                 [Test]
719                 public void InvokeMember_InvokeMethod_Set ()
720                 {
721                         try {
722                                 typeof (ArrayList).InvokeMember ("ToString",
723                                         BindingFlags.InvokeMethod | BindingFlags.SetField |
724                                         BindingFlags.Instance | BindingFlags.Public,
725                                         null, new ArrayList (), new object [0]);
726                                 Assert.Fail ("#A1");
727                         } catch (ArgumentException ex) {
728                                 // Cannot specify Set on a field and Invoke on a method
729                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
730                                 Assert.IsNull (ex.InnerException, "#A3");
731                                 Assert.IsNotNull (ex.Message, "#A4");
732                                 Assert.IsNotNull (ex.ParamName, "#A5");
733 #if NET_2_0
734                                 Assert.AreEqual ("bindingFlags", ex.ParamName, "#A6");
735 #else
736                                 Assert.AreEqual ("invokeAttr", ex.ParamName, "#A6");
737 #endif
738                         }
739
740                         try {
741                                 typeof (ArrayList).InvokeMember ("ToString",
742                                         BindingFlags.InvokeMethod | BindingFlags.SetProperty |
743                                         BindingFlags.Instance | BindingFlags.Public,
744                                         null, new ArrayList (), new object [0]);
745                                 Assert.Fail ("#B1");
746                         } catch (ArgumentException ex) {
747                                 // Cannot specify Set on a property and Invoke on a method
748                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
749                                 Assert.IsNull (ex.InnerException, "#B3");
750                                 Assert.IsNotNull (ex.Message, "#B4");
751                                 Assert.IsNotNull (ex.ParamName, "#B5");
752 #if NET_2_0
753                                 Assert.AreEqual ("bindingFlags", ex.ParamName, "#B6");
754 #else
755                                 Assert.AreEqual ("invokeAttr", ex.ParamName, "#B6");
756 #endif
757                         }
758                 }
759
760                 [Test]
761                 public void InvokeMember_MatchPrimitiveTypeWithInterface ()
762                 {
763                         object [] invokeargs = { 1 };
764                         typeof (Z).InvokeMember ("", BindingFlags.DeclaredOnly |
765                                 BindingFlags.Public | BindingFlags.NonPublic |
766                                 BindingFlags.Instance | BindingFlags.CreateInstance,
767                                 null, null, invokeargs);
768                 }
769
770                 [Test]
771                 public void InvokeMember_Name_Null ()
772                 {
773                         try {
774                                 typeof (X).InvokeMember ((string) null,
775                                         BindingFlags.Public | BindingFlags.Static |
776                                         BindingFlags.FlattenHierarchy | BindingFlags.SetField,
777                                         null, null, new object [] { 5 });
778                                 Assert.Fail ("#1");
779                         } catch (ArgumentNullException ex) {
780                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
781                                 Assert.IsNull (ex.InnerException, "#3");
782                                 Assert.IsNotNull (ex.Message, "#4");
783                                 Assert.IsNotNull (ex.ParamName, "#5");
784                                 Assert.AreEqual ("name", ex.ParamName, "#6");
785                         }
786                 }
787
788                 [Test]
789                 public void InvokeMember_NoOperation ()
790                 {
791                         try {
792                                 typeof (TypeTest).InvokeMember ("Run", BindingFlags.Public |
793                                         BindingFlags.Static, null, null, new object [0]);
794                                 Assert.Fail ("#1");
795                         } catch (ArgumentException ex) {
796                                 // Must specify binding flags describing the
797                                 // invoke operation required
798                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
799                                 Assert.IsNull (ex.InnerException, "#3");
800                                 Assert.IsNotNull (ex.Message, "#4");
801                                 Assert.IsNotNull (ex.ParamName, "#5");
802 #if NET_2_0
803                                 Assert.AreEqual ("bindingFlags", ex.ParamName, "#6");
804 #else
805                                 Assert.AreEqual ("invokeAttr", ex.ParamName, "#6");
806 #endif
807                         }
808                 }
809
810                 [Test] // bug #321735
811                 public void InvokeMember_SetFieldProperty ()
812                 {
813                         ArrayList list = new ArrayList ();
814                         list.Add ("foo");
815                         list.GetType ().InvokeMember ("Item",
816                                 BindingFlags.SetField | BindingFlags.SetProperty |
817                                 BindingFlags.Instance | BindingFlags.Public,
818                                 null, list, new object [] { 0, "bar" });
819                         Assert.AreEqual ("bar", list [0]);
820                 }
821
822                 [Test]
823                 public void InvokeMember_SetField_ProvidedArgs ()
824                 {
825                         try {
826                                 typeof (X).InvokeMember ("Value", BindingFlags.Public |
827                                         BindingFlags.Static | BindingFlags.SetField,
828                                         null, null, new object [0]);
829                                 Assert.Fail ("#A1");
830                         } catch (ArgumentException ex) {
831                                 // Only the field value can be specified to set
832                                 // a field value
833                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
834                                 Assert.IsNull (ex.InnerException, "#A3");
835                                 Assert.IsNotNull (ex.Message, "#A4");
836                                 Assert.IsNotNull (ex.ParamName, "#A5");
837 #if NET_2_0
838                                 Assert.AreEqual ("bindingFlags", ex.ParamName, "#6");
839 #else
840                                 Assert.AreEqual ("invokeAttr", ex.ParamName, "#6");
841 #endif
842                         }
843
844                         try {
845                                 typeof (X).InvokeMember ("Value", BindingFlags.Public |
846                                         BindingFlags.Static | BindingFlags.SetField,
847                                         null, null, null);
848                                 Assert.Fail ("#B1");
849 #if NET_2_0
850                         } catch (ArgumentNullException ex) {
851                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
852                                 Assert.IsNull (ex.InnerException, "#B3");
853                                 Assert.IsNotNull (ex.Message, "#B4");
854                                 Assert.IsNotNull (ex.ParamName, "#B5");
855                                 Assert.AreEqual ("providedArgs", ex.ParamName, "#B6");
856                         }
857 #else
858                         } catch (ArgumentException ex) {
859                                 // Only the field value can be specified to set
860                                 // a field value
861                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
862                                 Assert.IsNull (ex.InnerException, "#B3");
863                                 Assert.IsNotNull (ex.Message, "#B4");
864                                 Assert.IsNotNull (ex.ParamName, "#B5");
865 #if NET_2_0
866                                 Assert.AreEqual ("bindingFlags", ex.ParamName, "#B6");
867 #else
868                                 Assert.AreEqual ("invokeAttr", ex.ParamName, "#B6");
869 #endif
870                         }
871 #endif
872                 }
873
874                 [Test] // bug #336841
875                 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=306797
876                 public void InvokeMember_VarArgs ()
877                 {
878                         BindingFlags flags = BindingFlags.InvokeMethod | BindingFlags.Public |
879                                 BindingFlags.NonPublic | BindingFlags.OptionalParamBinding |
880                                 BindingFlags.Static | BindingFlags.FlattenHierarchy |
881                                 BindingFlags.Instance;
882
883                         Type type = typeof (TypeTest);
884                         string result = (string) type.InvokeMember ("bug336841",
885                                 flags, null, null, new object [] { "1" });
886                         Assert.IsNotNull (result, "#A1");
887                         Assert.AreEqual ("#B:1", result, "#A2");
888
889                         result = (string) type.InvokeMember ("bug336841", flags,
890                                 null, null, new object [] { "1", "2", "3", "4" });
891                         Assert.IsNotNull (result, "#B1");
892                         Assert.AreEqual ("#A:1|2,3,4", result, "#B2");
893                 }
894
895                 class X
896                 {
897                         public static int Value;
898                 }
899
900                 class Y : X
901                 {
902                 }
903
904                 class Z
905                 {
906                         public Z (IComparable value)
907                         {
908                         }
909                 }
910         
911                 public static void Run ()
912                 {
913                 }
914
915                 class TakesInt
916                 {
917                         private int i;
918
919                         public TakesInt (int x)
920                         {
921                                 i = x;
922                         }
923
924                         public int Integer {
925                                 get { return i; }
926                         }
927                 }
928
929                 class TakesObject
930                 {
931                         public TakesObject (object x) {}
932                 }
933
934                 [Test] // bug #75241
935                 public void GetConstructorNullInTypes ()
936                 {
937                         // This ends up calling type.GetConstructor ()
938                         Activator.CreateInstance (typeof (TakesInt), new object [] { null });
939                         Activator.CreateInstance (typeof (TakesObject), new object [] { null });
940                 }
941
942                 [Test]
943                 public void GetConstructor_TakeInt_Object ()
944                 {
945                         Assert.IsNull (typeof (TakesInt).GetConstructor (new Type[1] { typeof (object) }));
946                 }
947
948                 [Test]
949                 public void GetCustomAttributes_All ()
950                 {
951                         object [] attrs = typeof (A).GetCustomAttributes (false);
952                         Assert.AreEqual (2, attrs.Length, "#A1");
953                         Assert.IsTrue (HasAttribute (attrs, typeof (FooAttribute)), "#A2");
954                         Assert.IsTrue (HasAttribute (attrs, typeof (VolatileModifier)), "#A3");
955
956                         attrs = typeof (BA).GetCustomAttributes (false);
957                         Assert.AreEqual (1, attrs.Length, "#B1");
958                         Assert.AreEqual (typeof (BarAttribute), attrs [0].GetType (), "#B2");
959
960                         attrs = typeof (BA).GetCustomAttributes (true);
961                         Assert.AreEqual (2, attrs.Length, "#C1");
962                         Assert.IsTrue (HasAttribute (attrs, typeof (BarAttribute)), "#C2");
963                         Assert.IsTrue (HasAttribute (attrs, typeof (VolatileModifier)), "#C3");
964
965                         attrs = typeof (CA).GetCustomAttributes (false);
966                         Assert.AreEqual (0, attrs.Length, "#D");
967
968                         attrs = typeof (CA).GetCustomAttributes (true);
969                         Assert.AreEqual (1, attrs.Length, "#E1");
970                         Assert.AreEqual (typeof (VolatileModifier), attrs [0].GetType (), "#E2");
971                 }
972
973                 static bool HasAttribute (object [] attrs, Type attributeType)
974                 {
975                         foreach (object attr in attrs)
976                                 if (attr.GetType () == attributeType)
977                                         return true;
978                         return false;
979                 }
980
981                 [Test]
982                 public void GetCustomAttributes_Type ()
983                 {
984                         object [] attrs = null;
985
986                         attrs = typeof (A).GetCustomAttributes (
987                                 typeof (VolatileModifier), false);
988                         Assert.AreEqual (1, attrs.Length, "#A1");
989                         Assert.AreEqual (typeof (VolatileModifier), attrs [0].GetType (), "#A2");
990                         attrs = typeof (A).GetCustomAttributes (
991                                 typeof (VolatileModifier), true);
992                         Assert.AreEqual (1, attrs.Length, "#A3");
993                         Assert.AreEqual (typeof (VolatileModifier), attrs [0].GetType (), "#A4");
994
995                         attrs = typeof (A).GetCustomAttributes (
996                                 typeof (NemerleAttribute), false);
997                         Assert.AreEqual (1, attrs.Length, "#B1");
998                         Assert.AreEqual (typeof (VolatileModifier), attrs [0].GetType (), "#B2");
999                         attrs = typeof (A).GetCustomAttributes (
1000                                 typeof (NemerleAttribute), true);
1001                         Assert.AreEqual (1, attrs.Length, "#B3");
1002                         Assert.AreEqual (typeof (VolatileModifier), attrs [0].GetType (), "#B4");
1003
1004                         attrs = typeof (A).GetCustomAttributes (
1005                                 typeof (FooAttribute), false);
1006                         Assert.AreEqual (1, attrs.Length, "#C1");
1007                         Assert.AreEqual (typeof (FooAttribute), attrs [0].GetType (), "#C2");
1008                         attrs = typeof (A).GetCustomAttributes (
1009                                 typeof (FooAttribute), false);
1010                         Assert.AreEqual (1, attrs.Length, "#C3");
1011                         Assert.AreEqual (typeof (FooAttribute), attrs [0].GetType (), "#C4");
1012
1013                         attrs = typeof (BA).GetCustomAttributes (
1014                                 typeof (VolatileModifier), false);
1015                         Assert.AreEqual (0, attrs.Length, "#D1");
1016                         attrs = typeof (BA).GetCustomAttributes (
1017                                 typeof (VolatileModifier), true);
1018                         Assert.AreEqual (1, attrs.Length, "#D2");
1019                         Assert.AreEqual (typeof (VolatileModifier), attrs [0].GetType (), "#D3");
1020
1021                         attrs = typeof (BA).GetCustomAttributes (
1022                                 typeof (NemerleAttribute), false);
1023                         Assert.AreEqual (0, attrs.Length, "#E1");
1024                         attrs = typeof (BA).GetCustomAttributes (
1025                                 typeof (NemerleAttribute), true);
1026                         Assert.AreEqual (1, attrs.Length, "#E2");
1027                         Assert.AreEqual (typeof (VolatileModifier), attrs [0].GetType (), "#E3");
1028
1029                         attrs = typeof (BA).GetCustomAttributes (
1030                                 typeof (FooAttribute), false);
1031                         Assert.AreEqual (1, attrs.Length, "#F1");
1032                         Assert.AreEqual (typeof (BarAttribute), attrs [0].GetType (), "#F2");
1033                         attrs = typeof (BA).GetCustomAttributes (
1034                                 typeof (FooAttribute), true);
1035                         Assert.AreEqual (1, attrs.Length, "#F3");
1036                         Assert.AreEqual (typeof (BarAttribute), attrs [0].GetType (), "#F4");
1037
1038                         attrs = typeof (bug82431A1).GetCustomAttributes (
1039                                 typeof (InheritAttribute), false);
1040                         Assert.AreEqual (1, attrs.Length, "#G1");
1041                         Assert.AreEqual (typeof (NotInheritAttribute), attrs [0].GetType (), "#G2");
1042                         attrs = typeof (bug82431A1).GetCustomAttributes (
1043                                 typeof (InheritAttribute), true);
1044                         Assert.AreEqual (1, attrs.Length, "#G3");
1045                         Assert.AreEqual (typeof (NotInheritAttribute), attrs [0].GetType (), "#G4");
1046
1047                         attrs = typeof (bug82431A1).GetCustomAttributes (
1048                                 typeof (NotInheritAttribute), false);
1049                         Assert.AreEqual (1, attrs.Length, "#H1");
1050                         Assert.AreEqual (typeof (NotInheritAttribute), attrs [0].GetType (), "#H2");
1051                         attrs = typeof (bug82431A1).GetCustomAttributes (
1052                                 typeof (InheritAttribute), true);
1053                         Assert.AreEqual (1, attrs.Length, "#H3");
1054                         Assert.AreEqual (typeof (NotInheritAttribute), attrs [0].GetType (), "#H4");
1055
1056                         attrs = typeof (bug82431A2).GetCustomAttributes (
1057                                 typeof (InheritAttribute), false);
1058                         Assert.AreEqual (0, attrs.Length, "#I1");
1059                         attrs = typeof (bug82431A2).GetCustomAttributes (
1060                                 typeof (InheritAttribute), true);
1061                         Assert.AreEqual (0, attrs.Length, "#I2");
1062
1063                         attrs = typeof (bug82431A2).GetCustomAttributes (
1064                                 typeof (NotInheritAttribute), false);
1065                         Assert.AreEqual (0, attrs.Length, "#J1");
1066                         attrs = typeof (bug82431A2).GetCustomAttributes (
1067                                 typeof (NotInheritAttribute), true);
1068                         Assert.AreEqual (0, attrs.Length, "#J2");
1069
1070                         attrs = typeof (bug82431A3).GetCustomAttributes (
1071                                 typeof (InheritAttribute), false);
1072                         Assert.AreEqual (2, attrs.Length, "#K1");
1073                         Assert.IsTrue (HasAttribute (attrs, typeof (InheritAttribute)), "#K2");
1074                         Assert.IsTrue (HasAttribute (attrs, typeof (NotInheritAttribute)), "#K3");
1075                         attrs = typeof (bug82431A3).GetCustomAttributes (
1076                                 typeof (InheritAttribute), true);
1077                         Assert.AreEqual (2, attrs.Length, "#K4");
1078                         Assert.IsTrue (HasAttribute (attrs, typeof (InheritAttribute)), "#K5");
1079                         Assert.IsTrue (HasAttribute (attrs, typeof (NotInheritAttribute)), "#K6");
1080
1081                         attrs = typeof (bug82431A3).GetCustomAttributes (
1082                                 typeof (NotInheritAttribute), false);
1083                         Assert.AreEqual (1, attrs.Length, "#L1");
1084                         Assert.AreEqual (typeof (NotInheritAttribute), attrs [0].GetType (), "#L2");
1085                         attrs = typeof (bug82431A3).GetCustomAttributes (
1086                                 typeof (NotInheritAttribute), true);
1087                         Assert.AreEqual (1, attrs.Length, "#L3");
1088                         Assert.AreEqual (typeof (NotInheritAttribute), attrs [0].GetType (), "#L4");
1089
1090                         attrs = typeof (bug82431B1).GetCustomAttributes (
1091                                 typeof (InheritAttribute), false);
1092                         Assert.AreEqual (1, attrs.Length, "#M1");
1093                         Assert.AreEqual (typeof (InheritAttribute), attrs [0].GetType (), "#M2");
1094                         attrs = typeof (bug82431B1).GetCustomAttributes (
1095                                 typeof (InheritAttribute), true);
1096                         Assert.AreEqual (1, attrs.Length, "#M3");
1097                         Assert.AreEqual (typeof (InheritAttribute), attrs [0].GetType (), "#M4");
1098
1099                         attrs = typeof (bug82431B1).GetCustomAttributes (
1100                                 typeof (NotInheritAttribute), false);
1101                         Assert.AreEqual (0, attrs.Length, "#N1");
1102                         attrs = typeof (bug82431B1).GetCustomAttributes (
1103                                 typeof (NotInheritAttribute), true);
1104                         Assert.AreEqual (0, attrs.Length, "#N2");
1105
1106                         attrs = typeof (bug82431B2).GetCustomAttributes (
1107                                 typeof (InheritAttribute), false);
1108                         Assert.AreEqual (0, attrs.Length, "#O1");
1109                         attrs = typeof (bug82431B2).GetCustomAttributes (
1110                                 typeof (InheritAttribute), true);
1111                         Assert.AreEqual (1, attrs.Length, "#O2");
1112                         Assert.AreEqual (typeof (InheritAttribute), attrs [0].GetType (), "#O3");
1113
1114                         attrs = typeof (bug82431B2).GetCustomAttributes (
1115                                 typeof (NotInheritAttribute), false);
1116                         Assert.AreEqual (0, attrs.Length, "#P1");
1117                         attrs = typeof (bug82431B2).GetCustomAttributes (
1118                                 typeof (NotInheritAttribute), true);
1119                         Assert.AreEqual (0, attrs.Length, "#P2");
1120
1121                         attrs = typeof (bug82431B3).GetCustomAttributes (
1122                                 typeof (InheritAttribute), false);
1123                         Assert.AreEqual (1, attrs.Length, "#Q1");
1124                         Assert.AreEqual (typeof (NotInheritAttribute), attrs [0].GetType (), "#Q2");
1125                         attrs = typeof (bug82431B3).GetCustomAttributes (
1126                                 typeof (InheritAttribute), true);
1127                         Assert.AreEqual (2, attrs.Length, "#Q3");
1128                         Assert.AreEqual (typeof (NotInheritAttribute), attrs [0].GetType (), "#Q4");
1129                         Assert.AreEqual (typeof (InheritAttribute), attrs [1].GetType (), "#Q5");
1130
1131                         attrs = typeof (bug82431B3).GetCustomAttributes (
1132                                 typeof (NotInheritAttribute), false);
1133                         Assert.AreEqual (1, attrs.Length, "#R1");
1134                         Assert.AreEqual (typeof (NotInheritAttribute), attrs [0].GetType (), "#R2");
1135                         attrs = typeof (bug82431B3).GetCustomAttributes (
1136                                 typeof (NotInheritAttribute), true);
1137                         Assert.AreEqual (1, attrs.Length, "#R3");
1138                         Assert.AreEqual (typeof (NotInheritAttribute), attrs [0].GetType (), "#R4");
1139
1140                         attrs = typeof (bug82431B4).GetCustomAttributes (
1141                                 typeof (InheritAttribute), false);
1142                         Assert.AreEqual (0, attrs.Length, "#S1");
1143                         attrs = typeof (bug82431B4).GetCustomAttributes (
1144                                 typeof (InheritAttribute), true);
1145                         Assert.AreEqual (1, attrs.Length, "#S2");
1146                         Assert.AreEqual (typeof (InheritAttribute), attrs [0].GetType (), "#S3");
1147
1148                         attrs = typeof (bug82431B4).GetCustomAttributes (
1149                                 typeof (NotInheritAttribute), false);
1150                         Assert.AreEqual (0, attrs.Length, "#T1");
1151                         attrs = typeof (bug82431B4).GetCustomAttributes (
1152                                 typeof (NotInheritAttribute), true);
1153                         Assert.AreEqual (0, attrs.Length, "#T2");
1154
1155                         attrs = typeof (A).GetCustomAttributes (
1156                                 typeof (string), false);
1157                         Assert.AreEqual (0, attrs.Length, "#U1");
1158                         attrs = typeof (A).GetCustomAttributes (
1159                                 typeof (string), true);
1160                         Assert.AreEqual (0, attrs.Length, "#U2");
1161                 }
1162
1163                 [Test] // bug #76150
1164                 public void IsDefined ()
1165                 {
1166                         Assert.IsTrue (typeof (A).IsDefined (typeof (NemerleAttribute), false), "#A1");
1167                         Assert.IsTrue (typeof (A).IsDefined (typeof (VolatileModifier), false), "#A2");
1168                         Assert.IsTrue (typeof (A).IsDefined (typeof (FooAttribute), false), "#A3");
1169                         Assert.IsFalse (typeof (A).IsDefined (typeof (BarAttribute), false), "#A4");
1170
1171                         Assert.IsFalse (typeof (BA).IsDefined (typeof (NemerleAttribute), false), "#B1");
1172                         Assert.IsFalse (typeof (BA).IsDefined (typeof (VolatileModifier), false), "#B2");
1173                         Assert.IsTrue (typeof (BA).IsDefined (typeof (FooAttribute), false), "#B3");
1174                         Assert.IsTrue (typeof (BA).IsDefined (typeof (BarAttribute), false), "#B4");
1175                         Assert.IsFalse (typeof (BA).IsDefined (typeof (string), false), "#B5");
1176                         Assert.IsFalse (typeof (BA).IsDefined (typeof (int), false), "#B6");
1177                         Assert.IsTrue (typeof (BA).IsDefined (typeof (NemerleAttribute), true), "#B7");
1178                         Assert.IsTrue (typeof (BA).IsDefined (typeof (VolatileModifier), true), "#B8");
1179                         Assert.IsTrue (typeof (BA).IsDefined (typeof (FooAttribute), true), "#B9");
1180                         Assert.IsTrue (typeof (BA).IsDefined (typeof (BarAttribute), true), "#B10");
1181                         Assert.IsFalse (typeof (BA).IsDefined (typeof (string), true), "#B11");
1182                         Assert.IsFalse (typeof (BA).IsDefined (typeof (int), true), "#B12");
1183                 }
1184
1185                 [Test]
1186                 public void IsDefined_AttributeType_Null ()
1187                 {
1188                         try {
1189                                 typeof (BA).IsDefined ((Type) null, false);
1190                                 Assert.Fail ("#1");
1191                         } catch (ArgumentNullException ex) {
1192                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1193                                 Assert.IsNull (ex.InnerException, "#3");
1194                                 Assert.IsNotNull (ex.Message, "#4");
1195                                 Assert.IsNotNull (ex.ParamName, "#5");
1196                                 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
1197                         }
1198                 }
1199
1200                 [Test] // bug #82431
1201 #if NET_2_0
1202                 [Category ("NotWorking")]
1203 #endif
1204                 public void IsDefined_Inherited ()
1205                 {
1206                         Assert.IsFalse (typeof (CA).IsDefined (typeof (NemerleAttribute), false), "#C1");
1207                         Assert.IsFalse (typeof (CA).IsDefined (typeof (VolatileModifier), false), "#C2");
1208                         Assert.IsFalse (typeof (CA).IsDefined (typeof (FooAttribute), false), "#C3");
1209                         Assert.IsFalse (typeof (CA).IsDefined (typeof (BarAttribute), false), "#C4");
1210                         Assert.IsTrue (typeof (CA).IsDefined (typeof (NemerleAttribute), true), "#C5");
1211                         Assert.IsTrue (typeof (CA).IsDefined (typeof (VolatileModifier), true), "#C6");
1212                         Assert.IsFalse (typeof (CA).IsDefined (typeof (FooAttribute), true), "#C7");
1213                         Assert.IsFalse (typeof (CA).IsDefined (typeof (BarAttribute), true), "#C8");
1214
1215                         Assert.IsFalse (typeof (BBA).IsDefined (typeof (NemerleAttribute), false), "#D1");
1216                         Assert.IsFalse (typeof (BBA).IsDefined (typeof (VolatileModifier), false), "#D2");
1217                         Assert.IsFalse (typeof (BBA).IsDefined (typeof (FooAttribute), false), "#D3");
1218                         Assert.IsFalse (typeof (BBA).IsDefined (typeof (BarAttribute), false), "#D4");
1219                         Assert.IsTrue (typeof (BBA).IsDefined (typeof (NemerleAttribute), true), "#D5");
1220                         Assert.IsTrue (typeof (BBA).IsDefined (typeof (VolatileModifier), true), "#D6");
1221 #if NET_2_0
1222                         Assert.IsTrue (typeof (BBA).IsDefined (typeof (FooAttribute), true), "#D7");
1223                         Assert.IsTrue (typeof (BBA).IsDefined (typeof (BarAttribute), true), "#D8");
1224 #else
1225                         Assert.IsFalse (typeof (BBA).IsDefined (typeof (FooAttribute), true), "#D7");
1226                         Assert.IsFalse (typeof (BBA).IsDefined (typeof (BarAttribute), true), "#D8");
1227 #endif
1228
1229                         Assert.IsTrue (typeof (bug82431A1).IsDefined (typeof (InheritAttribute), false), "#E1");
1230                         Assert.IsTrue (typeof (bug82431A1).IsDefined (typeof (NotInheritAttribute), false), "#E2");
1231                         Assert.IsTrue (typeof (bug82431A1).IsDefined (typeof (InheritAttribute), true), "#E3");
1232                         Assert.IsTrue (typeof (bug82431A1).IsDefined (typeof (NotInheritAttribute), true), "#E4");
1233
1234                         Assert.IsFalse (typeof (bug82431A2).IsDefined (typeof (InheritAttribute), false), "#F1");
1235                         Assert.IsFalse (typeof (bug82431A2).IsDefined (typeof (NotInheritAttribute), false), "#F2");
1236 #if NET_2_0
1237                         Assert.IsFalse (typeof (bug82431A2).IsDefined (typeof (InheritAttribute), true), "#F3");
1238 #else
1239                         Assert.IsTrue (typeof (bug82431A2).IsDefined (typeof (InheritAttribute), true), "#F3");
1240 #endif
1241                         Assert.IsFalse (typeof (bug82431A2).IsDefined (typeof (NotInheritAttribute), true), "#F4");
1242
1243                         Assert.IsTrue (typeof (bug82431A3).IsDefined (typeof (InheritAttribute), false), "#G1");
1244                         Assert.IsTrue (typeof (bug82431A3).IsDefined (typeof (NotInheritAttribute), false), "#G2");
1245                         Assert.IsTrue (typeof (bug82431A3).IsDefined (typeof (InheritAttribute), true), "#G3");
1246                         Assert.IsTrue (typeof (bug82431A3).IsDefined (typeof (NotInheritAttribute), true), "#G4");
1247
1248                         Assert.IsTrue (typeof (bug82431B1).IsDefined (typeof (InheritAttribute), false), "#H1");
1249                         Assert.IsFalse (typeof (bug82431B1).IsDefined (typeof (NotInheritAttribute), false), "#H2");
1250                         Assert.IsTrue (typeof (bug82431B1).IsDefined (typeof (InheritAttribute), true), "#H3");
1251                         Assert.IsFalse (typeof (bug82431B1).IsDefined (typeof (NotInheritAttribute), true), "#H4");
1252
1253                         Assert.IsFalse (typeof (bug82431B2).IsDefined (typeof (InheritAttribute), false), "#I1");
1254                         Assert.IsFalse (typeof (bug82431B2).IsDefined (typeof (NotInheritAttribute), false), "#I2");
1255                         Assert.IsTrue (typeof (bug82431B2).IsDefined (typeof (InheritAttribute), true), "#I3");
1256                         Assert.IsFalse (typeof (bug82431B2).IsDefined (typeof (NotInheritAttribute), true), "#I4");
1257
1258                         Assert.IsTrue (typeof (bug82431B3).IsDefined (typeof (InheritAttribute), false), "#J1");
1259                         Assert.IsTrue (typeof (bug82431B3).IsDefined (typeof (NotInheritAttribute), false), "#J2");
1260                         Assert.IsTrue (typeof (bug82431B3).IsDefined (typeof (InheritAttribute), true), "#J3");
1261                         Assert.IsTrue (typeof (bug82431B3).IsDefined (typeof (NotInheritAttribute), true), "#J4");
1262
1263                         Assert.IsFalse (typeof (bug82431B4).IsDefined (typeof (InheritAttribute), false), "#K2");
1264                         Assert.IsFalse (typeof (bug82431B4).IsDefined (typeof (NotInheritAttribute), false), "#K2");
1265                         Assert.IsTrue (typeof (bug82431B4).IsDefined (typeof (InheritAttribute), true), "#K3");
1266                         Assert.IsFalse (typeof (bug82431B4).IsDefined (typeof (NotInheritAttribute), true), "#K4");
1267                 }
1268
1269                 [Test]
1270                 public void GetTypeCode ()
1271                 {
1272                         Assert.AreEqual (TypeCode.Boolean, Type.GetTypeCode (typeof (bool)), "#1");
1273                         Assert.AreEqual (TypeCode.Byte, Type.GetTypeCode (typeof (byte)), "#2");
1274                         Assert.AreEqual (TypeCode.Char, Type.GetTypeCode (typeof (char)), "#3");
1275                         Assert.AreEqual (TypeCode.DateTime, Type.GetTypeCode (typeof (DateTime)), "#4");
1276                         Assert.AreEqual (TypeCode.DBNull, Type.GetTypeCode (typeof (DBNull)), "#5");
1277                         Assert.AreEqual (TypeCode.Decimal, Type.GetTypeCode (typeof (decimal)), "#6");
1278                         Assert.AreEqual (TypeCode.Double, Type.GetTypeCode (typeof (double)), "#7");
1279                         Assert.AreEqual (TypeCode.Empty, Type.GetTypeCode (null), "#8");
1280                         Assert.AreEqual (TypeCode.Int16, Type.GetTypeCode (typeof (short)), "#9");
1281                         Assert.AreEqual (TypeCode.Int32, Type.GetTypeCode (typeof (int)), "#10");
1282                         Assert.AreEqual (TypeCode.Int64, Type.GetTypeCode (typeof (long)), "#11");
1283                         Assert.AreEqual (TypeCode.Object, Type.GetTypeCode (typeof (TakesInt)), "#12");
1284                         Assert.AreEqual (TypeCode.SByte, Type.GetTypeCode (typeof (sbyte)), "#13");
1285                         Assert.AreEqual (TypeCode.Single, Type.GetTypeCode (typeof (float)), "#14");
1286                         Assert.AreEqual (TypeCode.String, Type.GetTypeCode (typeof (string)), "#15");
1287                         Assert.AreEqual (TypeCode.UInt16, Type.GetTypeCode (typeof (ushort)), "#16");
1288                         Assert.AreEqual (TypeCode.UInt32, Type.GetTypeCode (typeof (uint)), "#17");
1289                         Assert.AreEqual (TypeCode.UInt64, Type.GetTypeCode (typeof (ulong)), "#18");
1290                 }
1291
1292                 [Test] // GetConstructor (Type [])
1293                 public void GetConstructor1_Types_Null ()
1294                 {
1295                         try {
1296                                 typeof (BindingFlags).GetConstructor (null);
1297                                 Assert.Fail ("#1");
1298                         } catch (ArgumentNullException ex) {
1299                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1300                                 Assert.IsNull (ex.InnerException, "#3");
1301                                 Assert.IsNotNull (ex.Message, "#4");
1302                                 Assert.IsNotNull (ex.ParamName, "#5");
1303                                 Assert.AreEqual ("types", ex.ParamName, "#6");
1304                         }
1305                 }
1306
1307                 [Test] // GetConstructor (Type [])
1308                 public void GetConstructor1_Types_ItemNull ()
1309                 {
1310                         Type type = typeof (BindingFlags);
1311                         try {
1312                                 type.GetConstructor (new Type[1] { null });
1313                                 Assert.Fail ("#A1");
1314                         } catch (ArgumentNullException ex) {
1315                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
1316                                 Assert.IsNull (ex.InnerException, "#A3");
1317                                 Assert.IsNotNull (ex.Message, "#A4");
1318                                 Assert.IsNotNull (ex.ParamName, "#A5");
1319                                 Assert.AreEqual ("types", ex.ParamName, "#A6");
1320                         }
1321
1322                         type = typeof (TakesInt);
1323                         try {
1324                                 type.GetConstructor (new Type [1] { null });
1325                                 Assert.Fail ("#B1");
1326                         } catch (ArgumentNullException ex) {
1327                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
1328                                 Assert.IsNull (ex.InnerException, "#B3");
1329                                 Assert.IsNotNull (ex.Message, "#B4");
1330                                 Assert.IsNotNull (ex.ParamName, "#B5");
1331                                 Assert.AreEqual ("types", ex.ParamName, "#B6");
1332                         }
1333                 }
1334
1335                 [Test] // GetConstructor (BindingFlags, Binder, Type [], ParameterModifier [])
1336                 public void GetConstructor2_Types_ItemNull ()
1337                 {
1338                         Type type = typeof (BindingFlags);
1339                         try {
1340                                 type.GetConstructor (BindingFlags.Default, null,
1341                                         new Type[1] { null }, null);
1342                                 Assert.Fail ("#1");
1343                         } catch (ArgumentNullException ex) {
1344                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1345                                 Assert.IsNull (ex.InnerException, "#3");
1346                                 Assert.IsNotNull (ex.Message, "#4");
1347                                 Assert.IsNotNull (ex.ParamName, "#5");
1348                                 Assert.AreEqual ("types", ex.ParamName, "#6");
1349                         }
1350                 }
1351
1352                 [Test] // GetConstructor (BindingFlags, Binder, CallingConventions, Type [], ParameterModifier [])
1353                 public void GetConstructor3_Types_ItemNull ()
1354                 {
1355                         Type type = typeof (BindingFlags);
1356                         try {
1357                                 type.GetConstructor (BindingFlags.Default,
1358                                         null, CallingConventions.Any,
1359                                         new Type[1] { null }, null);
1360                                 Assert.Fail ("#1");
1361                         } catch (ArgumentNullException ex) {
1362                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1363                                 Assert.IsNull (ex.InnerException, "#3");
1364                                 Assert.IsNotNull (ex.Message, "#4");
1365                                 Assert.IsNotNull (ex.ParamName, "#5");
1366                                 Assert.AreEqual ("types", ex.ParamName, "#6");
1367                         }
1368                 }
1369
1370                 [Test]
1371                 public void GetMethod_Bug77367 ()
1372                 {
1373                         MethodInfo i = typeof (Bug77367).GetMethod ("Run", Type.EmptyTypes);
1374                         Assert.IsNull (i);
1375                 }
1376
1377 #if !TARGET_JVM // Reflection.Emit is not supported for TARGET_JVM
1378                 [Test]
1379                 public void EqualsUnderlyingType ()
1380                 {
1381                         AssemblyBuilderAccess access = AssemblyBuilderAccess.RunAndSave;
1382                         TypeAttributes attribs = TypeAttributes.Public;
1383
1384                         AssemblyName name = new AssemblyName ();
1385                         name.Name = "enumtest";
1386                         AssemblyBuilder assembly = 
1387                                 AppDomain.CurrentDomain.DefineDynamicAssembly (
1388                                         name, access);
1389
1390                         ModuleBuilder module = assembly.DefineDynamicModule 
1391                                 ("m", "enumtest.dll");
1392                         EnumBuilder e = module.DefineEnum ("E", attribs, typeof (int));
1393
1394                         Assert.IsTrue (typeof (int).Equals (e));
1395                 }
1396 #endif // TARGET_JVM
1397
1398                 [Test]
1399                 public void Equals_Type_Null ()
1400                 {
1401                         Assert.IsFalse (typeof (int).Equals ((Type) null), "#1");
1402                         Assert.IsFalse (typeof (int).Equals ((object) null), "#2");
1403                 }
1404
1405                 [Test]
1406                 public void GetElementType_Bug63841 ()
1407                 {
1408                         Assert.IsNull (typeof (TheEnum).GetElementType (), "#1");
1409                 }
1410
1411 #if NET_2_0
1412                 [Test]
1413                 public void FullNameGenerics ()
1414                 {
1415                         Type fooType = typeof (Foo<>);
1416                         FieldInfo [] fields = fooType.GetFields ();
1417
1418                         Assert.AreEqual (1, fields.Length, "#0");
1419
1420                         Assert.IsNotNull (fooType.FullName, "#1");
1421                         Assert.IsNotNull (fooType.AssemblyQualifiedName, "#1a");
1422
1423                         FieldInfo field = fooType.GetField ("Whatever");
1424                         Assert.IsNotNull (field, "#2");
1425                         Assert.AreEqual (field, fields [0], "#2a");
1426                         Assert.IsNull (field.FieldType.FullName, "#3");
1427                         Assert.IsNull (field.FieldType.AssemblyQualifiedName, "#3a");
1428                         Assert.IsNotNull (field.FieldType.ToString (), "#4");
1429
1430                         PropertyInfo prop = fooType.GetProperty ("Test");
1431                         Assert.IsNotNull (prop, "#5");
1432                         Assert.IsNull (prop.PropertyType.FullName, "#6");
1433                         Assert.IsNull (prop.PropertyType.AssemblyQualifiedName, "#6a");
1434                         Assert.IsNotNull (prop.PropertyType.ToString (), "#7");
1435
1436                         MethodInfo method = fooType.GetMethod("Execute");
1437                         Assert.IsNotNull (method, "#8");
1438                         Assert.IsNull (method.ReturnType.FullName, "#9");
1439                         Assert.IsNull (method.ReturnType.AssemblyQualifiedName, "#9a");
1440                         Assert.IsNotNull (method.ReturnType.ToString (), "#10");
1441
1442                         ParameterInfo[] parameters = method.GetParameters();
1443                         Assert.AreEqual (1, parameters.Length, "#11");
1444                         Assert.IsNull (parameters[0].ParameterType.FullName, "#12");
1445                         Assert.IsNull (parameters[0].ParameterType.AssemblyQualifiedName, "#12a");
1446                         Assert.IsNotNull (parameters[0].ParameterType.ToString (), "#13");
1447                 }
1448
1449                 [Test]
1450                 public void TypeParameterIsNotGeneric ()
1451                 {
1452                         Type fooType = typeof (Foo<>);
1453                         Type type_param = fooType.GetGenericArguments () [0];
1454                         Assert.IsTrue (type_param.IsGenericParameter);
1455                         Assert.IsFalse (type_param.IsGenericType);
1456                         Assert.IsFalse (type_param.IsGenericTypeDefinition);
1457
1458                         // LAMESPEC: MSDN claims that this should be false, but .NET v2.0.50727 says it's true
1459                         // http://msdn2.microsoft.com/en-us/library/system.type.isgenerictype.aspx
1460                         Assert.IsTrue (type_param.ContainsGenericParameters);
1461                 }
1462
1463                 [Test]
1464                 public void IsAssignable ()
1465                 {
1466                         Type foo_type = typeof (Foo<>);
1467                         Type foo_int_type = typeof (Foo<int>);
1468                         Assert.IsFalse (foo_type.IsAssignableFrom (foo_int_type), "Foo<int> -!-> Foo<>");
1469                         Assert.IsFalse (foo_int_type.IsAssignableFrom (foo_type), "Foo<> -!-> Foo<int>");
1470
1471                         Type ibar_short_type = typeof (IBar<short>);
1472                         Type ibar_int_type = typeof (IBar<int>);
1473                         Type baz_short_type = typeof (Baz<short>);
1474                         Type baz_int_type = typeof (Baz<int>);
1475
1476                         Assert.IsTrue (ibar_int_type.IsAssignableFrom (baz_int_type), "Baz<int> -> IBar<int>");
1477                         Assert.IsTrue (ibar_short_type.IsAssignableFrom (baz_short_type), "Baz<short> -> IBar<short>");
1478
1479                         Assert.IsFalse (ibar_int_type.IsAssignableFrom (baz_short_type), "Baz<short> -!-> IBar<int>");
1480                         Assert.IsFalse (ibar_short_type.IsAssignableFrom (baz_int_type), "Baz<int> -!-> IBar<short>");
1481
1482                         // Nullable tests
1483                         Assert.IsTrue (typeof (Nullable<int>).IsAssignableFrom (typeof (int)));
1484                         Assert.IsFalse (typeof (int).IsAssignableFrom (typeof (Nullable<int>)));
1485                         Assert.IsTrue (typeof (Nullable<FooStruct>).IsAssignableFrom (typeof (FooStruct)));
1486                 }
1487
1488                 [Test]
1489                 public void IsInstanceOf ()
1490                 {
1491                         Assert.IsTrue (typeof (Nullable<int>).IsInstanceOfType (5));
1492                 }
1493
1494                 [Test]
1495                 public void ByrefType ()
1496                 {
1497                         Type foo_type = typeof (Foo<>);
1498                         Type type_param = foo_type.GetGenericArguments () [0];
1499                         Type byref_type_param = type_param.MakeByRefType ();
1500                         Assert.IsFalse (byref_type_param.IsGenericParameter);
1501                         Assert.IsNull (byref_type_param.DeclaringType);
1502                 }
1503
1504                 [ComVisible (true)]
1505                 public class ComFoo<T> {
1506                 }
1507
1508                 [Test]
1509                 public void GetCustomAttributesGenericInstance ()
1510                 {
1511                         Assert.AreEqual (1, typeof (ComFoo<int>).GetCustomAttributes (typeof (ComVisibleAttribute), true).Length);
1512                 }
1513
1514                 interface ByRef1<T> { void f (ref T t); }
1515                 interface ByRef2 { void f<T> (ref T t); }
1516
1517                 interface ByRef3<T> where T:struct { void f (ref T? t); }
1518                 interface ByRef4 { void f<T> (ref T? t) where T:struct; }
1519
1520                 void CheckGenericByRef (Type t)
1521                 {
1522                         string name = t.Name;
1523                         t = t.GetMethod ("f").GetParameters () [0].ParameterType;
1524
1525                         Assert.IsFalse (t.IsGenericType, name);
1526                         Assert.IsFalse (t.IsGenericTypeDefinition, name);
1527                         Assert.IsFalse (t.IsGenericParameter, name);
1528                 }
1529
1530                 [Test]
1531                 public void GenericByRef ()
1532                 {
1533                         CheckGenericByRef (typeof (ByRef1<>));
1534                         CheckGenericByRef (typeof (ByRef2));
1535                         CheckGenericByRef (typeof (ByRef3<>));
1536                         CheckGenericByRef (typeof (ByRef4));
1537                 }
1538
1539                 public class Bug80242<T> {
1540                         public interface IFoo { }
1541                         public class Bar : IFoo { }
1542                         public class Baz : Bar { }
1543                 }
1544
1545                 [Test]
1546                 public void TestNestedTypes ()
1547                 {
1548                         Type t = typeof (Bug80242<object>);
1549                         Assert.IsFalse (t.IsGenericTypeDefinition);
1550                         foreach (Type u in t.GetNestedTypes ()) {
1551                                 Assert.IsTrue (u.IsGenericTypeDefinition, "{0} isn't a generic definition", u);
1552                                 Assert.AreEqual (u, u.GetGenericArguments () [0].DeclaringType);
1553                         }
1554                 }
1555
1556                 [Test] // bug #82211
1557                 public void GetMembers_GenericArgument ()
1558                 {
1559                         Type argType = typeof (ComFoo<>).GetGenericArguments () [0];
1560                         MemberInfo [] members = argType.GetMembers ();
1561                         Assert.IsNotNull (members, "#1");
1562                         Assert.AreEqual (4, members.Length, "#2");
1563                 }
1564
1565                 [Test]
1566                 [ExpectedException (typeof (ArgumentNullException))]
1567                 public void ReflectionOnlyGetTypeNullTypeName ()
1568                 {
1569                         Type.ReflectionOnlyGetType (null, false, false);
1570                 }
1571
1572                 [Test]
1573                 public void ReflectionOnlyGetTypeDoNotThrow ()
1574                 {
1575                         Assert.IsNull (Type.ReflectionOnlyGetType ("a, nonexistent.dll", false, false));
1576                 }
1577
1578                 [Test]
1579                 [ExpectedException (typeof (FileNotFoundException))]
1580                 public void ReflectionOnlyGetTypeThrow ()
1581                 {
1582                         Type.ReflectionOnlyGetType ("a, nonexistent.dll", true, false);
1583                 }
1584
1585                 [Test]
1586                 public void ReflectionOnlyGetType ()
1587                 {
1588                         Type t = Type.ReflectionOnlyGetType (typeof (int).AssemblyQualifiedName.ToString (), true, true);
1589                         Assert.AreEqual ("System.Int32", t.FullName);
1590                 }
1591
1592                 [Test] //bug #331199
1593                 //FIXME: 2.0 SP 1 has a diferent behavior
1594                 public void MakeGenericType_UserDefinedType ()
1595                 {
1596                         Type ut = new UserType (typeof (int));
1597                         Type t = typeof (Foo<>).MakeGenericType (ut);
1598                         Assert.IsTrue (t.IsGenericType, "#A1");
1599                         Assert.AreEqual (1, t.GetGenericArguments ().Length, "#A2");
1600
1601                         Type arg = t.GetGenericArguments () [0];
1602                         Assert.IsNotNull (arg, "#B1");
1603                         Assert.IsFalse (arg.IsGenericType, "#B2");
1604                         Assert.AreEqual (typeof (int), arg, "#B3");
1605                 }
1606
1607                 [Category ("NotWorking")]
1608                 //We dont support instantiating a user type 
1609                 public void MakeGenericType_NestedUserDefinedType ()
1610                 {
1611                         Type ut = new UserType (new UserType (typeof (int)));
1612                         Type t = typeof (Foo<>).MakeGenericType (ut);
1613                         Assert.IsTrue (t.IsGenericType, "#A1");
1614                         Assert.AreEqual (1, t.GetGenericArguments ().Length, "#A2");
1615
1616                         Type arg = t.GetGenericArguments () [0];
1617                         Assert.IsNotNull (arg, "#B1");
1618                         Assert.IsFalse (arg.IsGenericType, "#B2");
1619                         Assert.AreEqual (ut, arg, "#B3");
1620                 }
1621                 
1622                 [Test]
1623                 [Category ("NotWorking")]
1624                 public void TestMakeGenericType_UserDefinedType_DotNet20SP1 () 
1625                 {
1626                         Type ut = new UserType(typeof(int));
1627                         Type t = typeof(Foo<>).MakeGenericType(ut);
1628                         Assert.IsTrue (t.IsGenericType, "#1");
1629
1630                         Assert.AreEqual (ut, t.GetGenericArguments()[0], "#2");
1631                 }
1632                 
1633                 [Test]
1634                 public void MakeGenericType_BadUserType ()
1635                 {
1636                         Type ut = new UserType (null);
1637                         try {
1638                                 Type t = typeof (Foo<>).MakeGenericType (ut);
1639                                 Assert.Fail ("#1");
1640                         } catch (ArgumentException) {
1641                         }
1642                 }
1643 #endif
1644
1645                 public class NemerleAttribute : Attribute
1646                 {
1647                 }
1648
1649                 public class VolatileModifier : NemerleAttribute
1650                 {
1651                 }
1652
1653                 [VolatileModifier]
1654                 [FooAttribute]
1655                 class A
1656                 {
1657                 }
1658
1659                 [AttributeUsage (AttributeTargets.Class, Inherited=false)]
1660                 public class FooAttribute : Attribute
1661                 {
1662                 }
1663
1664                 public class BarAttribute : FooAttribute
1665                 {
1666                 }
1667
1668                 [BarAttribute]
1669                 class BA : A
1670                 {
1671                 }
1672
1673                 class BBA : BA
1674                 {
1675                 }
1676
1677                 class CA : A
1678                 {
1679                 }
1680
1681                 [AttributeUsage (AttributeTargets.Class, Inherited=true)]
1682                 public class InheritAttribute : Attribute
1683                 {
1684                 }
1685
1686                 [AttributeUsage (AttributeTargets.Class, Inherited=false)]
1687                 public class NotInheritAttribute : InheritAttribute
1688                 {
1689                 }
1690
1691                 [NotInheritAttribute]
1692                 public class bug82431A1
1693                 {
1694                 }
1695
1696                 public class bug82431A2 : bug82431A1
1697                 {
1698                 }
1699
1700                 [NotInheritAttribute]
1701                 [InheritAttribute]
1702                 public class bug82431A3 : bug82431A1
1703                 {
1704                 }
1705
1706                 [InheritAttribute]
1707                 public class bug82431B1
1708                 {
1709                 }
1710
1711                 public class bug82431B2 : bug82431B1
1712                 {
1713                 }
1714
1715                 [NotInheritAttribute]
1716                 public class bug82431B3 : bug82431B2
1717                 {
1718                 }
1719
1720                 public class bug82431B4 : bug82431B3
1721                 {
1722                 }
1723
1724                 struct FooStruct
1725                 {
1726                 }
1727
1728                 public class Bug77367
1729                 {
1730                         public void Run (bool b)
1731                         {
1732                         }
1733                 }
1734         }
1735
1736 #if NET_2_0
1737         class UserType : Type
1738         {
1739                 private Type type;
1740         
1741                 public UserType(Type type) {
1742                         this.type = type;
1743                 }
1744         
1745                 public override Type UnderlyingSystemType { get { return this.type; } }
1746         
1747                 public override Assembly Assembly { get { return this.type.Assembly; } }
1748         
1749                 public override string AssemblyQualifiedName { get { return null; } }
1750         
1751                 public override Type BaseType { get { return null; } }
1752         
1753                 public override Module Module { get { return this.type.Module; } }
1754         
1755                 public override string Namespace { get { return null; } }
1756         
1757                 public override bool IsGenericParameter { get { return true; } }
1758          
1759                 public override RuntimeTypeHandle TypeHandle { get { throw new NotSupportedException(); } }
1760         
1761                 public override bool ContainsGenericParameters { get { return true; } }
1762         
1763                 public override string FullName { get { return this.type.Name; } }
1764         
1765                 public override Guid GUID { get { throw new NotSupportedException(); } }
1766         
1767         
1768                 protected override bool IsArrayImpl() {
1769                         return false;
1770                 }
1771         
1772                 protected override bool IsByRefImpl()
1773                 {
1774                         return false;
1775                 }
1776         
1777                 protected override bool IsCOMObjectImpl()
1778                 {
1779                         return false;
1780                 }
1781         
1782                 protected override bool IsPointerImpl()
1783                 {
1784                         return false;
1785                 }
1786         
1787                 protected override bool IsPrimitiveImpl()
1788                 {
1789                         return false;
1790                 }
1791         
1792         
1793                 protected override TypeAttributes GetAttributeFlagsImpl()
1794                 {
1795                         return 0;
1796                 }
1797         
1798                 protected override ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr, Binder binder,
1799                                                                            CallingConventions callConvention, Type[] types,
1800                                                                            ParameterModifier[] modifiers)
1801                 {
1802                         return null;
1803                 }
1804         
1805                 public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
1806                 {
1807                         return null;
1808                 }
1809         
1810                 public override Type GetElementType()
1811                 {
1812                         return null;
1813                 }
1814         
1815                 public override EventInfo GetEvent(string name, BindingFlags bindingAttr)
1816                 {
1817                         return null;
1818                 }
1819         
1820         
1821                 public override FieldInfo GetField(string name, BindingFlags bindingAttr)
1822                 {
1823                         return null;
1824                 }
1825         
1826         
1827                 public override Type GetInterface(string name, bool ignoreCase)
1828                 {
1829                         return null;
1830                 }
1831         
1832                 public override Type[] GetInterfaces()
1833                 {
1834                         return null;
1835                 }
1836         
1837                 public override MemberInfo[] GetMembers(BindingFlags bindingAttr)
1838                 {
1839                         return null;
1840                 }
1841         
1842                 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
1843                 {
1844                         return null;
1845                 }
1846         
1847                 public override object[] GetCustomAttributes(bool inherit)
1848                 {
1849                         return null;
1850                 }
1851         
1852                 public override bool IsDefined(Type attributeType, bool inherit)
1853                 {
1854                         return false;
1855                 }
1856         
1857                 public override string Name { get { return this.type.Name; } }
1858         
1859                 public override EventInfo[] GetEvents(BindingFlags bindingAttr)
1860                 {
1861                         throw new NotImplementedException();
1862                 }
1863         
1864                 public override FieldInfo[] GetFields(BindingFlags bindingAttr)
1865                 {
1866                         throw new NotImplementedException();
1867                 }
1868         
1869                 protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder binder,
1870                                                                  CallingConventions callConvention, Type[] types,
1871                                                                  ParameterModifier[] modifiers)
1872                 {
1873                         return null;
1874                 }
1875         
1876                 public override MethodInfo[] GetMethods(BindingFlags bindingAttr)
1877                 {
1878                         return null;
1879                 }
1880         
1881                 public override Type GetNestedType(string name, BindingFlags bindingAttr)
1882                 {
1883                         return null;
1884                 }
1885         
1886                 public override Type[] GetNestedTypes(BindingFlags bindingAttr)
1887                 {
1888                         return null;
1889                 }
1890         
1891                 public override PropertyInfo[] GetProperties(BindingFlags bindingAttr)
1892                 {
1893                         return null;
1894                 }
1895         
1896                 protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder binder,
1897                                                                  Type returnType, Type[] types, ParameterModifier[] modifiers)
1898                 {
1899                         return null;
1900                 }
1901         
1902                 protected override bool HasElementTypeImpl()
1903                 {
1904                         return false;
1905                 }
1906         
1907                 public override object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target,
1908                                                          object[] args, ParameterModifier[] modifiers, CultureInfo culture,
1909                                                          string[] namedParameters)
1910                 {
1911                         throw new NotSupportedException();
1912                 }
1913         }
1914 #endif
1915 }