1 // TypeTest.cs - NUnit Test Cases for the System.Type class
4 // Zoltan Varga (vargaz@freemail.hu)
7 // (C) 2003 Ximian, Inc. http://www.ximian.com
10 using NUnit.Framework;
12 using System.Collections;
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Runtime.InteropServices;
18 class NoNamespaceClass {
21 namespace MonoTests.System
23 class Super : ICloneable {
24 public virtual object Clone () {
35 interface IFace2 : IFace1 {
39 interface IFace3 : IFace2 {
42 enum TheEnum { A, B, C };
47 public abstract int this [byte i] { get; }
48 public abstract int this [int i] { get; }
49 public abstract void TestVoid();
50 public abstract void TestInt(int i);
53 class DeriveVTable : Base {
54 public override int this [byte i] { get { return 1; } }
55 public override int this [int i] { get { return 1; } }
56 public override void TestVoid() { level = 1; }
57 public override void TestInt(int i) { level = 1; }
60 class NewVTable : DeriveVTable {
61 public new int this [byte i] { get { return 2; } }
62 public new int this [int i] { get { return 2; } }
63 public new void TestVoid() { level = 2; }
64 public new void TestInt(int i) { level = 2; }
66 public void Overload () { }
67 public void Overload (int i) { }
69 public NewVTable (out int i) {
73 public void byref_method (out int i) {
80 public virtual int Foo {
89 class Derived1 : Base1 {
90 public override int Foo {
101 get { throw new NotImplementedException (); }
104 public T Execute(T a) {
109 public interface IBar<T> { }
110 public class Baz<T> : IBar<T> { }
114 public class TypeTest
116 private void ByrefMethod (ref int i, ref Derived1 j, ref Base1 k) {
120 public void TestIsAssignableFrom () {
121 // Simple tests for inheritance
122 Assert.AreEqual (typeof (Super).IsAssignableFrom (typeof (Duper)) , true, "#01");
123 Assert.AreEqual (typeof (Duper).IsAssignableFrom (typeof (Duper)), true, "#02");
124 Assert.AreEqual (typeof (Object).IsAssignableFrom (typeof (Duper)), true, "#03");
125 Assert.AreEqual (typeof (ICloneable).IsAssignableFrom (typeof (Duper)), true, "#04");
128 Assert.AreEqual (typeof (Super[]).IsAssignableFrom (typeof (Duper[])), true, "#05");
129 Assert.AreEqual (typeof (Duper[]).IsAssignableFrom (typeof (Super[])), false, "#06");
130 Assert.AreEqual (typeof (Object[]).IsAssignableFrom (typeof (Duper[])), true, "#07");
131 Assert.AreEqual (typeof (ICloneable[]).IsAssignableFrom (typeof (Duper[])), true, "#08");
133 // Tests for multiple dimensional arrays
134 Assert.AreEqual (typeof (Super[][]).IsAssignableFrom (typeof (Duper[][])), true, "#09");
135 Assert.AreEqual (typeof (Duper[][]).IsAssignableFrom (typeof (Super[][])), false, "#10");
136 Assert.AreEqual (typeof (Object[][]).IsAssignableFrom (typeof (Duper[][])), true, "#11");
137 Assert.AreEqual (typeof (ICloneable[][]).IsAssignableFrom (typeof (Duper[][])), true, "#12");
139 // Tests for vectors<->one dimensional arrays */
140 #if TARGET_JVM // Lower bounds arrays are not supported for TARGET_JVM.
141 Array arr1 = Array.CreateInstance (typeof (int), new int[] {1});
142 Assert.AreEqual (typeof (int[]).IsAssignableFrom (arr1.GetType ()), true, "#13");
144 Array arr1 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0});
145 Array arr2 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {10});
147 Assert.AreEqual (typeof (int[]).IsAssignableFrom (arr1.GetType ()), true, "#13");
148 Assert.AreEqual (typeof (int[]).IsAssignableFrom (arr2.GetType ()), false, "#14");
151 // Test that arrays of enums can be cast to their base types
152 Assert.AreEqual (typeof (int[]).IsAssignableFrom (typeof (TypeCode[])), true, "#15");
154 // Test that arrays of valuetypes can't be cast to arrays of
156 Assert.AreEqual (typeof (object[]).IsAssignableFrom (typeof (TypeCode[])), false, "#16");
157 Assert.AreEqual (typeof (ValueType[]).IsAssignableFrom (typeof (TypeCode[])), false, "#17");
158 Assert.AreEqual (typeof (Enum[]).IsAssignableFrom (typeof (TypeCode[])), false, "#18");
160 // Test that arrays of enums can't be cast to arrays of references
161 Assert.AreEqual (typeof (object[]).IsAssignableFrom (typeof (TheEnum[])), false, "#19");
162 Assert.AreEqual (typeof (ValueType[]).IsAssignableFrom (typeof (TheEnum[])), false, "#20");
163 Assert.AreEqual (typeof (Enum[]).IsAssignableFrom (typeof (TheEnum[])), false, "#21");
165 // Check that ValueType and Enum are recognized as reference types
166 Assert.AreEqual (typeof (object).IsAssignableFrom (typeof (ValueType)), true, "#22");
167 Assert.AreEqual (typeof (object).IsAssignableFrom (typeof (Enum)), true, "#23");
168 Assert.AreEqual (typeof (ValueType).IsAssignableFrom (typeof (Enum)), true, "#24");
170 Assert.AreEqual (typeof (object[]).IsAssignableFrom (typeof (ValueType[])), true, "#25");
171 Assert.AreEqual (typeof (ValueType[]).IsAssignableFrom (typeof (ValueType[])), true, "#26");
172 Assert.AreEqual (typeof (Enum[]).IsAssignableFrom (typeof (ValueType[])), false, "#27");
174 Assert.AreEqual (typeof (object[]).IsAssignableFrom (typeof (Enum[])), true, "#28");
175 Assert.AreEqual (typeof (ValueType[]).IsAssignableFrom (typeof (Enum[])), true, "#29");
176 Assert.AreEqual (typeof (Enum[]).IsAssignableFrom (typeof (Enum[])), true, "#30");
178 // Tests for byref types
179 MethodInfo mi = typeof (TypeTest).GetMethod ("ByrefMethod", BindingFlags.Instance|BindingFlags.NonPublic);
180 Assert.IsTrue (mi.GetParameters ()[2].ParameterType.IsAssignableFrom (mi.GetParameters ()[1].ParameterType));
181 Assert.IsTrue (mi.GetParameters ()[1].ParameterType.IsAssignableFrom (mi.GetParameters ()[1].ParameterType));
185 public void TestIsSubclassOf () {
186 Assert.IsTrue (typeof (ICloneable).IsSubclassOf (typeof (object)), "#01");
188 // Tests for byref types
189 Type paramType = typeof (TypeTest).GetMethod ("ByrefMethod", BindingFlags.Instance|BindingFlags.NonPublic).GetParameters () [0].ParameterType;
190 Assert.IsTrue (!paramType.IsSubclassOf (typeof (ValueType)), "#02");
191 //Assert.IsTrue (paramType.IsSubclassOf (typeof (Object)), "#03");
192 Assert.IsTrue (!paramType.IsSubclassOf (paramType), "#04");
196 public void TestGetMethodImpl() {
197 // Test binding of new slot methods (using no types)
198 Assert.AreEqual (typeof (Base), typeof (Base).GetMethod("TestVoid").DeclaringType, "#01");
199 Assert.AreEqual (typeof (NewVTable), typeof (NewVTable).GetMethod ("TestVoid").DeclaringType, "#02");
201 // Test binding of new slot methods (using types)
202 Assert.AreEqual (typeof (Base), typeof (Base).GetMethod ("TestInt", new Type[] { typeof (int) }).DeclaringType, "#03");
203 Assert.AreEqual (typeof (NewVTable), typeof (NewVTable).GetMethod ("TestInt", new Type[] { typeof (int) }).DeclaringType, "#04");
205 // Test overload resolution
206 Assert.AreEqual (0, typeof (NewVTable).GetMethod ("Overload", new Type[0]).GetParameters ().Length, "#05");
208 // Test byref parameters
209 Assert.AreEqual (null, typeof (NewVTable).GetMethod ("byref_method", new Type[] { typeof (int) }), "#06");
210 Type byrefInt = typeof (NewVTable).GetMethod ("byref_method").GetParameters ()[0].ParameterType;
211 Assert.IsNotNull (typeof (NewVTable).GetMethod ("byref_method", new Type[] { byrefInt }), "#07");
215 [Category ("TargetJvmNotWorking")]
216 public void TestGetPropertyImpl() {
217 // Test getting property that is exact
218 Assert.AreEqual (typeof (NewVTable), typeof (NewVTable).GetProperty ("Item", new Type[1] { typeof (Int32) }).DeclaringType, "#01");
220 // Test getting property that is not exact
221 Assert.AreEqual (typeof (NewVTable), typeof (NewVTable).GetProperty ("Item", new Type[1] { typeof (Int16) }).DeclaringType, "#02");
223 // Test overriding of properties when only the set accessor is overriden
224 Assert.AreEqual (1, typeof (Derived1).GetProperties ().Length, "#03");
227 #if !TARGET_JVM // StructLayout not supported for TARGET_JVM
228 [StructLayout(LayoutKind.Explicit, Pack = 4, Size = 64)]
229 public class Class1 {
232 [StructLayout(LayoutKind.Explicit, CharSet=CharSet.Unicode)]
233 public class Class2 {
238 public void StructLayoutAttribute () {
239 StructLayoutAttribute attr1 = typeof (TypeTest).StructLayoutAttribute;
240 Assert.AreEqual (LayoutKind.Auto, attr1.Value);
242 StructLayoutAttribute attr2 = typeof (Class1).StructLayoutAttribute;
243 Assert.AreEqual (LayoutKind.Explicit, attr2.Value);
244 Assert.AreEqual (4, attr2.Pack);
245 Assert.AreEqual (64, attr2.Size);
247 StructLayoutAttribute attr3 = typeof (Class2).StructLayoutAttribute;
248 Assert.AreEqual (LayoutKind.Explicit, attr3.Value);
249 Assert.AreEqual (CharSet.Unicode, attr3.CharSet);
255 public void Namespace () {
256 Assert.AreEqual (null, typeof (NoNamespaceClass).Namespace);
259 public static void Reflected (ref int a) {
265 Assert.AreEqual ("Int32&", typeof (TypeTest).GetMethod ("Reflected").GetParameters () [0].ParameterType.Name);
269 public void GetInterfaces () {
270 Type[] t = typeof (Duper).GetInterfaces ();
271 Assert.AreEqual (1, t.Length);
272 Assert.AreEqual (typeof (ICloneable), t[0]);
274 Type[] t2 = typeof (IFace3).GetInterfaces ();
275 Assert.AreEqual (2, t2.Length);
281 public void GetFieldIgnoreCase () {
282 Assert.IsNotNull (typeof (TypeTest).GetField ("afield", BindingFlags.Instance|BindingFlags.Public|BindingFlags.IgnoreCase));
296 public void GetPropertyAccessorModifiers () {
297 Assert.IsNotNull (typeof (TypeTest).GetProperty ("Count", BindingFlags.Instance | BindingFlags.Public));
298 Assert.IsNull (typeof (TypeTest).GetProperty ("Count", BindingFlags.Instance | BindingFlags.NonPublic));
303 public void IsPrimitive () {
304 Assert.IsTrue (typeof (IntPtr).IsPrimitive);
308 [Category("NotDotNet")]
309 // Depends on the GAC working, which it doesn't durring make distcheck.
310 [Category ("NotWorking")]
311 public void GetTypeWithWhitespace () {
312 Assert.IsNotNull (Type.GetType
313 (@"System.Configuration.NameValueSectionHandler,
318 PublicKeyToken=b77a5c561934e089"));
322 public void ExerciseFilterName() {
323 MemberInfo[] mi = typeof(Base).FindMembers(
325 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
326 BindingFlags.Instance | BindingFlags.DeclaredOnly,
327 Type.FilterName, "*");
328 Assert.AreEqual (4, mi.Length);
329 mi = typeof(Base).FindMembers(
331 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
332 BindingFlags.Instance | BindingFlags.DeclaredOnly,
333 Type.FilterName, "Test*");
334 Assert.AreEqual (2, mi.Length);
335 mi = typeof(Base).FindMembers(
337 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
338 BindingFlags.Instance | BindingFlags.DeclaredOnly,
339 Type.FilterName, "TestVoid");
340 Assert.AreEqual (1, mi.Length);
341 mi = typeof(Base).FindMembers(
343 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
344 BindingFlags.Instance | BindingFlags.DeclaredOnly,
345 Type.FilterName, "NonExistingMethod");
346 Assert.AreEqual (0, mi.Length);
350 public void ExerciseFilterNameIgnoreCase() {
351 MemberInfo[] mi = typeof(Base).FindMembers(
353 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
354 BindingFlags.Instance | BindingFlags.DeclaredOnly,
355 Type.FilterNameIgnoreCase, "*");
356 Assert.AreEqual (4, mi.Length);
357 mi = typeof(Base).FindMembers(
359 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
360 BindingFlags.Instance | BindingFlags.DeclaredOnly,
361 Type.FilterNameIgnoreCase, "test*");
362 Assert.AreEqual (2, mi.Length);
363 mi = typeof(Base).FindMembers(
365 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
366 BindingFlags.Instance | BindingFlags.DeclaredOnly,
367 Type.FilterNameIgnoreCase, "TESTVOID");
368 Assert.AreEqual (1, mi.Length);
369 mi = typeof(Base).FindMembers(
371 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
372 BindingFlags.Instance | BindingFlags.DeclaredOnly,
373 Type.FilterNameIgnoreCase, "NonExistingMethod");
374 Assert.AreEqual (0, mi.Length);
377 public class ByRef0 {
379 public int property {
382 public ByRef0 (int i) {}
383 public void f (int i) {}
387 public void ByrefTypes ()
389 Type t = Type.GetType ("MonoTests.System.TypeTest+ByRef0&");
390 Assert.IsNotNull (t);
391 Assert.IsTrue (t.IsByRef);
392 Assert.AreEqual (0, t.GetMethods (BindingFlags.Public | BindingFlags.Instance).Length);
393 Assert.AreEqual (0, t.GetConstructors (BindingFlags.Public | BindingFlags.Instance).Length);
394 Assert.AreEqual (0, t.GetEvents (BindingFlags.Public | BindingFlags.Instance).Length);
395 Assert.AreEqual (0, t.GetProperties (BindingFlags.Public | BindingFlags.Instance).Length);
397 Assert.IsNull (t.GetMethod ("f"));
398 Assert.IsNull (t.GetField ("field"));
399 Assert.IsNull (t.GetProperty ("property"));
408 public void CreateValueTypeNoCtor () {
409 typeof(B).InvokeMember ("", BindingFlags.CreateInstance, null, null, null);
413 [ExpectedException (typeof (MissingMethodException))]
414 public void CreateValueTypeNoCtorArgs () {
415 typeof(B).InvokeMember ("", BindingFlags.CreateInstance, null, null, new object [] { 1 });
420 public static int Value;
428 public void InvokeMemberGetSetField () {
429 typeof (X).InvokeMember ("Value", BindingFlags.Public|BindingFlags.Static|BindingFlags.FlattenHierarchy|BindingFlags.SetField, null, null, new object [] { 5 });
431 Assert.AreEqual (5, X.Value);
432 Assert.AreEqual (5, typeof (X).InvokeMember ("Value", BindingFlags.Public|BindingFlags.Static|BindingFlags.FlattenHierarchy|BindingFlags.GetField, null, null, new object [0]));
433 Assert.AreEqual (5, Y.Value);
434 Assert.AreEqual (5, typeof (Y).InvokeMember ("Value", BindingFlags.Public|BindingFlags.Static|BindingFlags.FlattenHierarchy|BindingFlags.GetField, null, null, new object [0]));
438 public Z (IComparable value) {}
442 public void InvokeMemberMatchPrimitiveTypeWithInterface () {
443 object[] invokeargs = {1};
444 typeof (Z).InvokeMember( "",
445 BindingFlags.DeclaredOnly |
446 BindingFlags.Public |
447 BindingFlags.NonPublic |
448 BindingFlags.Instance |
449 BindingFlags.CreateInstance,
450 null, null, invokeargs
457 public TakesInt (int x)
468 public TakesObject (object x) {}
471 // Filed as bug #75241
473 public void GetConstructorNullInTypes ()
475 // This ends up calling type.GetConstructor ()
476 Activator.CreateInstance (typeof (TakesInt), new object [] { null });
477 Activator.CreateInstance (typeof (TakesObject), new object [] { null });
481 [ExpectedException (typeof (ArgumentNullException))]
482 public void GetConstructorNullInTypes_Bug71300 ()
484 typeof (TakesInt).GetConstructor (new Type[1] { null });
485 // so null in types isn't valid for GetConstructor!
489 public void GetConstructor_TakeInt_Object ()
491 Assert.IsNull (typeof (TakesInt).GetConstructor (new Type[1] { typeof (object) }));
496 public void IsDefined ()
498 Assert.IsTrue (typeof (A).IsDefined (typeof (NemerleAttribute), false), "#1");
499 Assert.IsTrue (typeof (A).IsDefined (typeof (VolatileModifier), false), "#2");
503 public void GetTypeCode ()
505 Assert.AreEqual (TypeCode.Boolean, Type.GetTypeCode (typeof (bool)), "#1");
506 Assert.AreEqual (TypeCode.Byte, Type.GetTypeCode (typeof (byte)), "#2");
507 Assert.AreEqual (TypeCode.Char, Type.GetTypeCode (typeof (char)), "#3");
508 Assert.AreEqual (TypeCode.DateTime, Type.GetTypeCode (typeof (DateTime)), "#4");
509 Assert.AreEqual (TypeCode.DBNull, Type.GetTypeCode (typeof (DBNull)), "#5");
510 Assert.AreEqual (TypeCode.Decimal, Type.GetTypeCode (typeof (decimal)), "#6");
511 Assert.AreEqual (TypeCode.Double, Type.GetTypeCode (typeof (double)), "#7");
512 Assert.AreEqual (TypeCode.Empty, Type.GetTypeCode (null), "#8");
513 Assert.AreEqual (TypeCode.Int16, Type.GetTypeCode (typeof (short)), "#9");
514 Assert.AreEqual (TypeCode.Int32, Type.GetTypeCode (typeof (int)), "#10");
515 Assert.AreEqual (TypeCode.Int64, Type.GetTypeCode (typeof (long)), "#11");
516 Assert.AreEqual (TypeCode.Object, Type.GetTypeCode (typeof (TakesInt)), "#12");
517 Assert.AreEqual (TypeCode.SByte, Type.GetTypeCode (typeof (sbyte)), "#13");
518 Assert.AreEqual (TypeCode.Single, Type.GetTypeCode (typeof (float)), "#14");
519 Assert.AreEqual (TypeCode.String, Type.GetTypeCode (typeof (string)), "#15");
520 Assert.AreEqual (TypeCode.UInt16, Type.GetTypeCode (typeof (ushort)), "#16");
521 Assert.AreEqual (TypeCode.UInt32, Type.GetTypeCode (typeof (uint)), "#17");
522 Assert.AreEqual (TypeCode.UInt64, Type.GetTypeCode (typeof (ulong)), "#18");
526 [ExpectedException (typeof (ArgumentNullException))]
527 public void GetConstructor1a_Bug71300 ()
529 typeof (BindingFlags).GetConstructor (null);
533 [ExpectedException (typeof (ArgumentNullException))]
534 public void GetConstructor1b_Bug71300 ()
536 typeof (BindingFlags).GetConstructor (new Type[1] { null });
540 [ExpectedException (typeof (ArgumentNullException))]
541 public void GetConstructor4_Bug71300 ()
543 typeof (BindingFlags).GetConstructor (BindingFlags.Default, null, new Type[1] { null }, null);
547 [ExpectedException (typeof (ArgumentNullException))]
548 public void GetConstructor5_Bug71300 ()
550 typeof (BindingFlags).GetConstructor (BindingFlags.Default, null, CallingConventions.Any, new Type[1] { null }, null);
554 public void GetMethod_Bug77367 ()
556 MethodInfo i = typeof (Bug77367).GetMethod ("Run", Type.EmptyTypes);
560 #if !TARGET_JVM // Reflection.Emit is not supported for TARGET_JVM
562 public void EqualsUnderlyingType ()
564 AssemblyBuilderAccess access = AssemblyBuilderAccess.RunAndSave;
565 TypeAttributes attribs = TypeAttributes.Public;
567 AssemblyName name = new AssemblyName ();
568 name.Name = "enumtest";
569 AssemblyBuilder assembly =
570 AppDomain.CurrentDomain.DefineDynamicAssembly (
573 ModuleBuilder module = assembly.DefineDynamicModule
574 ("m", "enumtest.dll");
575 EnumBuilder e = module.DefineEnum ("E", attribs, typeof (int));
577 Assert.IsTrue (typeof (int).Equals (e));
582 public void GetElementType_Bug63841 ()
584 Assert.IsNull (typeof (TheEnum).GetElementType (), "#1");
589 public void FullNameGenerics ()
591 Type fooType = typeof (Foo<>);
592 FieldInfo [] fields = fooType.GetFields ();
594 Assert.AreEqual (1, fields.Length, "#0");
596 Assert.IsNotNull (fooType.FullName, "#1");
597 Assert.IsNotNull (fooType.AssemblyQualifiedName, "#1a");
599 FieldInfo field = fooType.GetField ("Whatever");
600 Assert.IsNotNull (field, "#2");
601 Assert.AreEqual (field, fields [0], "#2a");
602 Assert.IsNull (field.FieldType.FullName, "#3");
603 Assert.IsNull (field.FieldType.AssemblyQualifiedName, "#3a");
604 Assert.IsNotNull (field.FieldType.ToString (), "#4");
606 PropertyInfo prop = fooType.GetProperty ("Test");
607 Assert.IsNotNull (prop, "#5");
608 Assert.IsNull (prop.PropertyType.FullName, "#6");
609 Assert.IsNull (prop.PropertyType.AssemblyQualifiedName, "#6a");
610 Assert.IsNotNull (prop.PropertyType.ToString (), "#7");
612 MethodInfo method = fooType.GetMethod("Execute");
613 Assert.IsNotNull (method, "#8");
614 Assert.IsNull (method.ReturnType.FullName, "#9");
615 Assert.IsNull (method.ReturnType.AssemblyQualifiedName, "#9a");
616 Assert.IsNotNull (method.ReturnType.ToString (), "#10");
618 ParameterInfo[] parameters = method.GetParameters();
619 Assert.AreEqual (1, parameters.Length, "#11");
620 Assert.IsNull (parameters[0].ParameterType.FullName, "#12");
621 Assert.IsNull (parameters[0].ParameterType.AssemblyQualifiedName, "#12a");
622 Assert.IsNotNull (parameters[0].ParameterType.ToString (), "#13");
626 public void TypeParameterIsNotGeneric ()
628 Type fooType = typeof (Foo<>);
629 Type type_param = fooType.GetGenericArguments () [0];
630 Assert.IsTrue (type_param.IsGenericParameter);
631 Assert.IsFalse (type_param.IsGenericType);
632 Assert.IsFalse (type_param.IsGenericTypeDefinition);
634 // LAMESPEC: MSDN claims that this should be false, but .NET v2.0.50727 says it's true
635 // http://msdn2.microsoft.com/en-us/library/system.type.isgenerictype.aspx
636 Assert.IsTrue (type_param.ContainsGenericParameters);
640 public void IsAssignable ()
642 Type foo_type = typeof (Foo<>);
643 Type foo_int_type = typeof (Foo<int>);
644 Assert.IsFalse (foo_type.IsAssignableFrom (foo_int_type), "Foo<int> -!-> Foo<>");
645 Assert.IsFalse (foo_int_type.IsAssignableFrom (foo_type), "Foo<> -!-> Foo<int>");
647 Type ibar_short_type = typeof (IBar<short>);
648 Type ibar_int_type = typeof (IBar<int>);
649 Type baz_short_type = typeof (Baz<short>);
650 Type baz_int_type = typeof (Baz<int>);
652 Assert.IsTrue (ibar_int_type.IsAssignableFrom (baz_int_type), "Baz<int> -> IBar<int>");
653 Assert.IsTrue (ibar_short_type.IsAssignableFrom (baz_short_type), "Baz<short> -> IBar<short>");
655 Assert.IsFalse (ibar_int_type.IsAssignableFrom (baz_short_type), "Baz<short> -!-> IBar<int>");
656 Assert.IsFalse (ibar_short_type.IsAssignableFrom (baz_int_type), "Baz<int> -!-> IBar<short>");
659 Assert.IsTrue (typeof (Nullable<int>).IsAssignableFrom (typeof (int)));
660 Assert.IsFalse (typeof (int).IsAssignableFrom (typeof (Nullable<int>)));
661 Assert.IsTrue (typeof (Nullable<FooStruct>).IsAssignableFrom (typeof (FooStruct)));
665 public void IsInstanceOf ()
667 Assert.IsTrue (typeof (Nullable<int>).IsInstanceOfType (5));
671 public void ByrefType ()
673 Type foo_type = typeof (Foo<>);
674 Type type_param = foo_type.GetGenericArguments () [0];
675 Type byref_type_param = type_param.MakeByRefType ();
676 Assert.IsFalse (byref_type_param.IsGenericParameter);
677 Assert.IsNull (byref_type_param.DeclaringType);
681 [Category ("NotWorking")] // BindingFlags.SetField throws since args.Length != 1, even though we have SetProperty
682 public void Bug79023 ()
684 ArrayList list = new ArrayList();
687 // The next line used to throw because we had SetProperty
688 list.GetType().InvokeMember("Item",
689 BindingFlags.SetField|BindingFlags.SetProperty|
690 BindingFlags.Instance|BindingFlags.Public,
691 null, list, new object[] { 0, "bar" });
692 Assert.AreEqual ("bar", list[0]);
696 public class ComFoo<T> {
700 public void GetCustomAttributesGenericInstance ()
702 Assert.AreEqual (1, typeof (ComFoo<int>).GetCustomAttributes (typeof (ComVisibleAttribute), true).Length);
705 interface ByRef1<T> { void f (ref T t); }
706 interface ByRef2 { void f<T> (ref T t); }
708 interface ByRef3<T> where T:struct { void f (ref T? t); }
709 interface ByRef4 { void f<T> (ref T? t) where T:struct; }
711 void CheckGenericByRef (Type t)
713 string name = t.Name;
714 t = t.GetMethod ("f").GetParameters () [0].ParameterType;
716 Assert.IsFalse (t.IsGenericType, name);
717 Assert.IsFalse (t.IsGenericTypeDefinition, name);
718 Assert.IsFalse (t.IsGenericParameter, name);
722 public void GenericByRef ()
724 CheckGenericByRef (typeof (ByRef1<>));
725 CheckGenericByRef (typeof (ByRef2));
726 CheckGenericByRef (typeof (ByRef3<>));
727 CheckGenericByRef (typeof (ByRef4));
730 public class Bug80242<T> {
731 public interface IFoo { }
732 public class Bar : IFoo { }
733 public class Baz : Bar { }
737 public void TestNestedTypes ()
739 Type t = typeof (Bug80242<object>);
740 Assert.IsFalse (t.IsGenericTypeDefinition);
741 foreach (Type u in t.GetNestedTypes ()) {
742 Assert.IsTrue (u.IsGenericTypeDefinition, "{0} isn't a generic definition", u);
743 Assert.AreEqual (u, u.GetGenericArguments () [0].DeclaringType);
748 public class NemerleAttribute : Attribute
751 public class VolatileModifier : NemerleAttribute
760 public class Bug77367
762 public void Run (bool b)