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 Array arr1 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0});
141 Array arr2 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {10});
143 Assert.AreEqual (typeof (int[]).IsAssignableFrom (arr1.GetType ()), true, "#13");
144 Assert.AreEqual (typeof (int[]).IsAssignableFrom (arr2.GetType ()), false, "#14");
146 // Test that arrays of enums can be cast to their base types
147 Assert.AreEqual (typeof (int[]).IsAssignableFrom (typeof (TypeCode[])), true, "#15");
149 // Test that arrays of valuetypes can't be cast to arrays of
151 Assert.AreEqual (typeof (object[]).IsAssignableFrom (typeof (TypeCode[])), false, "#16");
152 Assert.AreEqual (typeof (ValueType[]).IsAssignableFrom (typeof (TypeCode[])), false, "#17");
153 Assert.AreEqual (typeof (Enum[]).IsAssignableFrom (typeof (TypeCode[])), false, "#18");
155 // Test that arrays of enums can't be cast to arrays of references
156 Assert.AreEqual (typeof (object[]).IsAssignableFrom (typeof (TheEnum[])), false, "#19");
157 Assert.AreEqual (typeof (ValueType[]).IsAssignableFrom (typeof (TheEnum[])), false, "#20");
158 Assert.AreEqual (typeof (Enum[]).IsAssignableFrom (typeof (TheEnum[])), false, "#21");
160 // Check that ValueType and Enum are recognized as reference types
161 Assert.AreEqual (typeof (object).IsAssignableFrom (typeof (ValueType)), true, "#22");
162 Assert.AreEqual (typeof (object).IsAssignableFrom (typeof (Enum)), true, "#23");
163 Assert.AreEqual (typeof (ValueType).IsAssignableFrom (typeof (Enum)), true, "#24");
165 Assert.AreEqual (typeof (object[]).IsAssignableFrom (typeof (ValueType[])), true, "#25");
166 Assert.AreEqual (typeof (ValueType[]).IsAssignableFrom (typeof (ValueType[])), true, "#26");
167 Assert.AreEqual (typeof (Enum[]).IsAssignableFrom (typeof (ValueType[])), false, "#27");
169 Assert.AreEqual (typeof (object[]).IsAssignableFrom (typeof (Enum[])), true, "#28");
170 Assert.AreEqual (typeof (ValueType[]).IsAssignableFrom (typeof (Enum[])), true, "#29");
171 Assert.AreEqual (typeof (Enum[]).IsAssignableFrom (typeof (Enum[])), true, "#30");
173 // Tests for byref types
174 MethodInfo mi = typeof (TypeTest).GetMethod ("ByrefMethod", BindingFlags.Instance|BindingFlags.NonPublic);
175 Assert.IsTrue (mi.GetParameters ()[2].ParameterType.IsAssignableFrom (mi.GetParameters ()[1].ParameterType));
176 Assert.IsTrue (mi.GetParameters ()[1].ParameterType.IsAssignableFrom (mi.GetParameters ()[1].ParameterType));
180 public void TestIsSubclassOf () {
181 Assert.IsTrue (typeof (ICloneable).IsSubclassOf (typeof (object)), "#01");
183 // Tests for byref types
184 Type paramType = typeof (TypeTest).GetMethod ("ByrefMethod", BindingFlags.Instance|BindingFlags.NonPublic).GetParameters () [0].ParameterType;
185 Assert.IsTrue (!paramType.IsSubclassOf (typeof (ValueType)), "#02");
186 //Assert.IsTrue (paramType.IsSubclassOf (typeof (Object)), "#03");
187 Assert.IsTrue (!paramType.IsSubclassOf (paramType), "#04");
191 public void TestGetMethodImpl() {
192 // Test binding of new slot methods (using no types)
193 Assert.AreEqual (typeof (Base), typeof (Base).GetMethod("TestVoid").DeclaringType, "#01");
194 Assert.AreEqual (typeof (NewVTable), typeof (NewVTable).GetMethod ("TestVoid").DeclaringType, "#02");
196 // Test binding of new slot methods (using types)
197 Assert.AreEqual (typeof (Base), typeof (Base).GetMethod ("TestInt", new Type[] { typeof (int) }).DeclaringType, "#03");
198 Assert.AreEqual (typeof (NewVTable), typeof (NewVTable).GetMethod ("TestInt", new Type[] { typeof (int) }).DeclaringType, "#04");
200 // Test overload resolution
201 Assert.AreEqual (0, typeof (NewVTable).GetMethod ("Overload", new Type[0]).GetParameters ().Length, "#05");
203 // Test byref parameters
204 Assert.AreEqual (null, typeof (NewVTable).GetMethod ("byref_method", new Type[] { typeof (int) }), "#06");
205 Type byrefInt = typeof (NewVTable).GetMethod ("byref_method").GetParameters ()[0].ParameterType;
206 Assert.IsNotNull (typeof (NewVTable).GetMethod ("byref_method", new Type[] { byrefInt }), "#07");
210 public void TestGetPropertyImpl() {
211 // Test getting property that is exact
212 Assert.AreEqual (typeof (NewVTable), typeof (NewVTable).GetProperty ("Item", new Type[1] { typeof (Int32) }).DeclaringType, "#01");
214 // Test getting property that is not exact
215 Assert.AreEqual (typeof (NewVTable), typeof (NewVTable).GetProperty ("Item", new Type[1] { typeof (Int16) }).DeclaringType, "#02");
217 // Test overriding of properties when only the set accessor is overriden
218 Assert.AreEqual (1, typeof (Derived1).GetProperties ().Length, "#03");
221 #if !TARGET_JVM // StructLayout not supported for TARGET_JVM
222 [StructLayout(LayoutKind.Explicit, Pack = 4, Size = 64)]
223 public class Class1 {
226 [StructLayout(LayoutKind.Explicit, CharSet=CharSet.Unicode)]
227 public class Class2 {
232 public void StructLayoutAttribute () {
233 StructLayoutAttribute attr1 = typeof (TypeTest).StructLayoutAttribute;
234 Assert.AreEqual (LayoutKind.Auto, attr1.Value);
236 StructLayoutAttribute attr2 = typeof (Class1).StructLayoutAttribute;
237 Assert.AreEqual (LayoutKind.Explicit, attr2.Value);
238 Assert.AreEqual (4, attr2.Pack);
239 Assert.AreEqual (64, attr2.Size);
241 StructLayoutAttribute attr3 = typeof (Class2).StructLayoutAttribute;
242 Assert.AreEqual (LayoutKind.Explicit, attr3.Value);
243 Assert.AreEqual (CharSet.Unicode, attr3.CharSet);
249 public void Namespace () {
250 Assert.AreEqual (null, typeof (NoNamespaceClass).Namespace);
253 public static void Reflected (ref int a) {
259 Assert.AreEqual ("Int32&", typeof (TypeTest).GetMethod ("Reflected").GetParameters () [0].ParameterType.Name);
263 public void GetInterfaces () {
264 Type[] t = typeof (Duper).GetInterfaces ();
265 Assert.AreEqual (1, t.Length);
266 Assert.AreEqual (typeof (ICloneable), t[0]);
268 Type[] t2 = typeof (IFace3).GetInterfaces ();
269 Assert.AreEqual (2, t2.Length);
275 public void GetFieldIgnoreCase () {
276 Assert.IsNotNull (typeof (TypeTest).GetField ("afield", BindingFlags.Instance|BindingFlags.Public|BindingFlags.IgnoreCase));
290 public void GetPropertyAccessorModifiers () {
291 Assert.IsNotNull (typeof (TypeTest).GetProperty ("Count", BindingFlags.Instance | BindingFlags.Public));
292 Assert.IsNull (typeof (TypeTest).GetProperty ("Count", BindingFlags.Instance | BindingFlags.NonPublic));
297 public void IsPrimitive () {
298 Assert.IsTrue (typeof (IntPtr).IsPrimitive);
302 [Category("NotDotNet")]
303 // Depends on the GAC working, which it doesn't durring make distcheck.
304 [Category ("NotWorking")]
305 public void GetTypeWithWhitespace () {
306 Assert.IsNotNull (Type.GetType
307 (@"System.Configuration.NameValueSectionHandler,
312 PublicKeyToken=b77a5c561934e089"));
316 public void ExerciseFilterName() {
317 MemberInfo[] mi = typeof(Base).FindMembers(
319 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
320 BindingFlags.Instance | BindingFlags.DeclaredOnly,
321 Type.FilterName, "*");
322 Assert.AreEqual (4, mi.Length);
323 mi = typeof(Base).FindMembers(
325 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
326 BindingFlags.Instance | BindingFlags.DeclaredOnly,
327 Type.FilterName, "Test*");
328 Assert.AreEqual (2, mi.Length);
329 mi = typeof(Base).FindMembers(
331 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
332 BindingFlags.Instance | BindingFlags.DeclaredOnly,
333 Type.FilterName, "TestVoid");
334 Assert.AreEqual (1, mi.Length);
335 mi = typeof(Base).FindMembers(
337 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
338 BindingFlags.Instance | BindingFlags.DeclaredOnly,
339 Type.FilterName, "NonExistingMethod");
340 Assert.AreEqual (0, mi.Length);
344 public void ExerciseFilterNameIgnoreCase() {
345 MemberInfo[] mi = typeof(Base).FindMembers(
347 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
348 BindingFlags.Instance | BindingFlags.DeclaredOnly,
349 Type.FilterNameIgnoreCase, "*");
350 Assert.AreEqual (4, mi.Length);
351 mi = typeof(Base).FindMembers(
353 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
354 BindingFlags.Instance | BindingFlags.DeclaredOnly,
355 Type.FilterNameIgnoreCase, "test*");
356 Assert.AreEqual (2, mi.Length);
357 mi = typeof(Base).FindMembers(
359 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
360 BindingFlags.Instance | BindingFlags.DeclaredOnly,
361 Type.FilterNameIgnoreCase, "TESTVOID");
362 Assert.AreEqual (1, mi.Length);
363 mi = typeof(Base).FindMembers(
365 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
366 BindingFlags.Instance | BindingFlags.DeclaredOnly,
367 Type.FilterNameIgnoreCase, "NonExistingMethod");
368 Assert.AreEqual (0, mi.Length);
371 public class ByRef0 {
373 public int property {
376 public ByRef0 (int i) {}
377 public void f (int i) {}
381 public void ByrefTypes ()
383 Type t = Type.GetType ("MonoTests.System.TypeTest+ByRef0&");
384 Assert.IsNotNull (t);
385 Assert.IsTrue (t.IsByRef);
386 Assert.AreEqual (0, t.GetMethods (BindingFlags.Public | BindingFlags.Instance).Length);
387 Assert.AreEqual (0, t.GetConstructors (BindingFlags.Public | BindingFlags.Instance).Length);
388 Assert.AreEqual (0, t.GetEvents (BindingFlags.Public | BindingFlags.Instance).Length);
389 Assert.AreEqual (0, t.GetProperties (BindingFlags.Public | BindingFlags.Instance).Length);
391 Assert.IsNull (t.GetMethod ("f"));
392 Assert.IsNull (t.GetField ("field"));
393 Assert.IsNull (t.GetProperty ("property"));
402 public void CreateValueTypeNoCtor () {
403 typeof(B).InvokeMember ("", BindingFlags.CreateInstance, null, null, null);
407 [ExpectedException (typeof (MissingMethodException))]
408 public void CreateValueTypeNoCtorArgs () {
409 typeof(B).InvokeMember ("", BindingFlags.CreateInstance, null, null, new object [] { 1 });
414 public static int Value;
422 public void InvokeMemberGetSetField () {
423 typeof (X).InvokeMember ("Value", BindingFlags.Public|BindingFlags.Static|BindingFlags.FlattenHierarchy|BindingFlags.SetField, null, null, new object [] { 5 });
425 Assert.AreEqual (5, X.Value);
426 Assert.AreEqual (5, typeof (X).InvokeMember ("Value", BindingFlags.Public|BindingFlags.Static|BindingFlags.FlattenHierarchy|BindingFlags.GetField, null, null, new object [0]));
427 Assert.AreEqual (5, Y.Value);
428 Assert.AreEqual (5, typeof (Y).InvokeMember ("Value", BindingFlags.Public|BindingFlags.Static|BindingFlags.FlattenHierarchy|BindingFlags.GetField, null, null, new object [0]));
432 public Z (IComparable value) {}
436 public void InvokeMemberMatchPrimitiveTypeWithInterface () {
437 object[] invokeargs = {1};
438 typeof (Z).InvokeMember( "",
439 BindingFlags.DeclaredOnly |
440 BindingFlags.Public |
441 BindingFlags.NonPublic |
442 BindingFlags.Instance |
443 BindingFlags.CreateInstance,
444 null, null, invokeargs
451 public TakesInt (int x)
462 public TakesObject (object x) {}
465 // Filed as bug #75241
467 public void GetConstructorNullInTypes ()
469 // This ends up calling type.GetConstructor ()
470 Activator.CreateInstance (typeof (TakesInt), new object [] { null });
471 Activator.CreateInstance (typeof (TakesObject), new object [] { null });
475 [ExpectedException (typeof (ArgumentNullException))]
476 public void GetConstructorNullInTypes_Bug71300 ()
478 typeof (TakesInt).GetConstructor (new Type[1] { null });
479 // so null in types isn't valid for GetConstructor!
483 public void GetConstructor_TakeInt_Object ()
485 Assert.IsNull (typeof (TakesInt).GetConstructor (new Type[1] { typeof (object) }));
490 public void IsDefined ()
492 Assert.IsTrue (typeof (A).IsDefined (typeof (NemerleAttribute), false), "#1");
493 Assert.IsTrue (typeof (A).IsDefined (typeof (VolatileModifier), false), "#2");
497 public void GetTypeCode ()
499 Assert.AreEqual (TypeCode.Boolean, Type.GetTypeCode (typeof (bool)), "#1");
500 Assert.AreEqual (TypeCode.Byte, Type.GetTypeCode (typeof (byte)), "#2");
501 Assert.AreEqual (TypeCode.Char, Type.GetTypeCode (typeof (char)), "#3");
502 Assert.AreEqual (TypeCode.DateTime, Type.GetTypeCode (typeof (DateTime)), "#4");
503 Assert.AreEqual (TypeCode.DBNull, Type.GetTypeCode (typeof (DBNull)), "#5");
504 Assert.AreEqual (TypeCode.Decimal, Type.GetTypeCode (typeof (decimal)), "#6");
505 Assert.AreEqual (TypeCode.Double, Type.GetTypeCode (typeof (double)), "#7");
506 Assert.AreEqual (TypeCode.Empty, Type.GetTypeCode (null), "#8");
507 Assert.AreEqual (TypeCode.Int16, Type.GetTypeCode (typeof (short)), "#9");
508 Assert.AreEqual (TypeCode.Int32, Type.GetTypeCode (typeof (int)), "#10");
509 Assert.AreEqual (TypeCode.Int64, Type.GetTypeCode (typeof (long)), "#11");
510 Assert.AreEqual (TypeCode.Object, Type.GetTypeCode (typeof (TakesInt)), "#12");
511 Assert.AreEqual (TypeCode.SByte, Type.GetTypeCode (typeof (sbyte)), "#13");
512 Assert.AreEqual (TypeCode.Single, Type.GetTypeCode (typeof (float)), "#14");
513 Assert.AreEqual (TypeCode.String, Type.GetTypeCode (typeof (string)), "#15");
514 Assert.AreEqual (TypeCode.UInt16, Type.GetTypeCode (typeof (ushort)), "#16");
515 Assert.AreEqual (TypeCode.UInt32, Type.GetTypeCode (typeof (uint)), "#17");
516 Assert.AreEqual (TypeCode.UInt64, Type.GetTypeCode (typeof (ulong)), "#18");
520 [ExpectedException (typeof (ArgumentNullException))]
521 public void GetConstructor1a_Bug71300 ()
523 typeof (BindingFlags).GetConstructor (null);
527 [ExpectedException (typeof (ArgumentNullException))]
528 public void GetConstructor1b_Bug71300 ()
530 typeof (BindingFlags).GetConstructor (new Type[1] { null });
534 [ExpectedException (typeof (ArgumentNullException))]
535 public void GetConstructor4_Bug71300 ()
537 typeof (BindingFlags).GetConstructor (BindingFlags.Default, null, new Type[1] { null }, null);
541 [ExpectedException (typeof (ArgumentNullException))]
542 public void GetConstructor5_Bug71300 ()
544 typeof (BindingFlags).GetConstructor (BindingFlags.Default, null, CallingConventions.Any, new Type[1] { null }, null);
548 public void GetMethod_Bug77367 ()
550 MethodInfo i = typeof (Bug77367).GetMethod ("Run", Type.EmptyTypes);
555 public void EqualsUnderlyingType ()
557 AssemblyBuilderAccess access = AssemblyBuilderAccess.RunAndSave;
558 TypeAttributes attribs = TypeAttributes.Public;
560 AssemblyName name = new AssemblyName ();
561 name.Name = "enumtest";
562 AssemblyBuilder assembly =
563 AppDomain.CurrentDomain.DefineDynamicAssembly (
566 ModuleBuilder module = assembly.DefineDynamicModule
567 ("m", "enumtest.dll");
568 EnumBuilder e = module.DefineEnum ("E", attribs, typeof (int));
570 Assert.IsTrue (typeof (int).Equals (e));
574 public void GetElementType_Bug63841 ()
576 Assert.IsNull (typeof (TheEnum).GetElementType (), "#1");
581 public void FullNameGenerics ()
583 Type fooType = typeof (Foo<>);
584 FieldInfo [] fields = fooType.GetFields ();
586 Assert.AreEqual (1, fields.Length, "#0");
588 Assert.IsNotNull (fooType.FullName, "#1");
589 Assert.IsNotNull (fooType.AssemblyQualifiedName, "#1a");
591 FieldInfo field = fooType.GetField ("Whatever");
592 Assert.IsNotNull (field, "#2");
593 Assert.AreEqual (field, fields [0], "#2a");
594 Assert.IsNull (field.FieldType.FullName, "#3");
595 Assert.IsNull (field.FieldType.AssemblyQualifiedName, "#3a");
596 Assert.IsNotNull (field.FieldType.ToString (), "#4");
598 PropertyInfo prop = fooType.GetProperty ("Test");
599 Assert.IsNotNull (prop, "#5");
600 Assert.IsNull (prop.PropertyType.FullName, "#6");
601 Assert.IsNull (prop.PropertyType.AssemblyQualifiedName, "#6a");
602 Assert.IsNotNull (prop.PropertyType.ToString (), "#7");
604 MethodInfo method = fooType.GetMethod("Execute");
605 Assert.IsNotNull (method, "#8");
606 Assert.IsNull (method.ReturnType.FullName, "#9");
607 Assert.IsNull (method.ReturnType.AssemblyQualifiedName, "#9a");
608 Assert.IsNotNull (method.ReturnType.ToString (), "#10");
610 ParameterInfo[] parameters = method.GetParameters();
611 Assert.AreEqual (1, parameters.Length, "#11");
612 Assert.IsNull (parameters[0].ParameterType.FullName, "#12");
613 Assert.IsNull (parameters[0].ParameterType.AssemblyQualifiedName, "#12a");
614 Assert.IsNotNull (parameters[0].ParameterType.ToString (), "#13");
618 public void TypeParameterIsNotGeneric ()
620 Type fooType = typeof (Foo<>);
621 Type type_param = fooType.GetGenericArguments () [0];
622 Assert.IsTrue (type_param.IsGenericParameter);
623 Assert.IsFalse (type_param.IsGenericType);
624 Assert.IsFalse (type_param.IsGenericTypeDefinition);
626 // LAMESPEC: MSDN claims that this should be false, but .NET v2.0.50727 says it's true
627 // http://msdn2.microsoft.com/en-us/library/system.type.isgenerictype.aspx
628 Assert.IsTrue (type_param.ContainsGenericParameters);
632 public void IsAssignable ()
634 Type foo_type = typeof (Foo<>);
635 Type foo_int_type = typeof (Foo<int>);
636 Assert.IsFalse (foo_type.IsAssignableFrom (foo_int_type), "Foo<int> -!-> Foo<>");
637 Assert.IsFalse (foo_int_type.IsAssignableFrom (foo_type), "Foo<> -!-> Foo<int>");
639 Type ibar_short_type = typeof (IBar<short>);
640 Type ibar_int_type = typeof (IBar<int>);
641 Type baz_short_type = typeof (Baz<short>);
642 Type baz_int_type = typeof (Baz<int>);
644 Assert.IsTrue (ibar_int_type.IsAssignableFrom (baz_int_type), "Baz<int> -> IBar<int>");
645 Assert.IsTrue (ibar_short_type.IsAssignableFrom (baz_short_type), "Baz<short> -> IBar<short>");
647 Assert.IsFalse (ibar_int_type.IsAssignableFrom (baz_short_type), "Baz<short> -!-> IBar<int>");
648 Assert.IsFalse (ibar_short_type.IsAssignableFrom (baz_int_type), "Baz<int> -!-> IBar<short>");
651 Assert.IsTrue (typeof (Nullable<int>).IsAssignableFrom (typeof (int)));
652 Assert.IsFalse (typeof (int).IsAssignableFrom (typeof (Nullable<int>)));
653 Assert.IsTrue (typeof (Nullable<FooStruct>).IsAssignableFrom (typeof (FooStruct)));
657 public void IsInstanceOf ()
659 Assert.IsTrue (typeof (Nullable<int>).IsInstanceOfType (5));
663 public void ByrefType ()
665 Type foo_type = typeof (Foo<>);
666 Type type_param = foo_type.GetGenericArguments () [0];
667 Type byref_type_param = type_param.MakeByRefType ();
668 Assert.IsFalse (byref_type_param.IsGenericParameter);
669 Assert.IsNull (byref_type_param.DeclaringType);
673 [Category ("NotWorking")] // BindingFlags.SetField throws since args.Length != 1, even though we have SetProperty
674 public void Bug79023 ()
676 ArrayList list = new ArrayList();
679 // The next line used to throw because we had SetProperty
680 list.GetType().InvokeMember("Item",
681 BindingFlags.SetField|BindingFlags.SetProperty|
682 BindingFlags.Instance|BindingFlags.Public,
683 null, list, new object[] { 0, "bar" });
684 Assert.AreEqual ("bar", list[0]);
688 public class ComFoo<T> {
692 public void GetCustomAttributesGenericInstance ()
694 Assert.AreEqual (1, typeof (ComFoo<int>).GetCustomAttributes (typeof (ComVisibleAttribute), true).Length);
697 interface ByRef1<T> { void f (ref T t); }
698 interface ByRef2 { void f<T> (ref T t); }
700 interface ByRef3<T> where T:struct { void f (ref T? t); }
701 interface ByRef4 { void f<T> (ref T? t) where T:struct; }
703 void CheckGenericByRef (Type t)
705 string name = t.Name;
706 t = t.GetMethod ("f").GetParameters () [0].ParameterType;
708 Assert.IsFalse (t.IsGenericType, name);
709 Assert.IsFalse (t.IsGenericTypeDefinition, name);
710 Assert.IsFalse (t.IsGenericParameter, name);
714 public void GenericByRef ()
716 CheckGenericByRef (typeof (ByRef1<>));
717 CheckGenericByRef (typeof (ByRef2));
718 CheckGenericByRef (typeof (ByRef3<>));
719 CheckGenericByRef (typeof (ByRef4));
722 public class Bug80242<T> {
723 public interface IFoo { }
724 public class Bar : IFoo { }
725 public class Baz : Bar { }
729 public void TestNestedTypes ()
731 Type t = typeof (Bug80242<object>);
732 Assert.IsFalse (t.IsGenericTypeDefinition);
733 foreach (Type u in t.GetNestedTypes ()) {
734 Assert.IsTrue (u.IsGenericTypeDefinition, "{0} isn't a generic definition", u);
735 Assert.AreEqual (u, u.GetGenericArguments () [0].DeclaringType);
740 public class NemerleAttribute : Attribute
743 public class VolatileModifier : NemerleAttribute
752 public class Bug77367
754 public void Run (bool b)