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.Collections.Generic;
17 using System.Reflection;
18 using System.Reflection.Emit;
19 using System.Runtime.InteropServices;
21 using System.Globalization;
23 class NoNamespaceClass {
26 namespace MonoTests.System
28 class Super : ICloneable
30 public virtual object Clone ()
45 interface IFace2 : IFace1
50 interface IFace3 : IFace2
65 public abstract int this [byte i] {
69 public abstract int this [int i] {
73 public abstract void TestVoid ();
74 public abstract void TestInt (int i);
77 class DeriveVTable : Base
79 public override int this [byte i] {
83 public override int this [int i] {
87 public override void TestVoid ()
92 public override void TestInt (int i)
98 class NewVTable : DeriveVTable
100 public new int this [byte i] {
104 public new int this [int i] {
108 public new void TestVoid ()
113 public new void TestInt (int i)
118 public void Overload ()
122 public void Overload (int i)
126 public NewVTable (out int i)
131 public void byref_method (out int i)
139 public virtual int Foo {
145 class Derived1 : Base1
147 public override int Foo {
152 class Derived2 : Base1
166 get { throw new NotImplementedException (); }
169 public T Execute (T a)
175 public interface IBar<T>
179 public class Baz<T> : IBar<T>
185 public class TypeTest
187 private AssemblyBuilder assembly;
188 private ModuleBuilder module;
189 const string ASSEMBLY_NAME = "MonoTests.System.TypeTest";
190 static int typeIndexer = 0;
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");
202 private string genTypeName ()
204 return "t" + (typeIndexer++);
207 private void ByrefMethod (ref int i, ref Derived1 j, ref Base1 k)
211 private void GenericMethod<Q> (Q q)
216 public void TestIsAssignableFrom ()
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");
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");
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");
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");
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});
244 Assert.AreEqual (typeof (int[]).IsAssignableFrom (arr1.GetType ()), true, "#13");
245 Assert.AreEqual (typeof (int[]).IsAssignableFrom (arr2.GetType ()), false, "#14");
248 // Test that arrays of enums can be cast to their base types
249 Assert.AreEqual (typeof (int[]).IsAssignableFrom (typeof (TypeCode[])), true, "#15");
251 // Test that arrays of valuetypes can't be cast to arrays of
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");
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");
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");
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");
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");
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));
280 // Tests for type parameters
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)));
289 public void TestIsSubclassOf ()
291 Assert.IsTrue (typeof (ICloneable).IsSubclassOf (typeof (object)), "#01");
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");
301 public void TestGetMethodImpl ()
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");
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");
311 // Test overload resolution
312 Assert.AreEqual (0, typeof (NewVTable).GetMethod ("Overload", new Type[0]).GetParameters ().Length, "#05");
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");
321 [Category ("TargetJvmNotWorking")]
322 public void TestGetPropertyImpl ()
324 // Test getting property that is exact
325 Assert.AreEqual (typeof (NewVTable), typeof (NewVTable).GetProperty ("Item", new Type[1] { typeof (Int32) }).DeclaringType, "#01");
327 // Test getting property that is not exact
328 Assert.AreEqual (typeof (NewVTable), typeof (NewVTable).GetProperty ("Item", new Type[1] { typeof (Int16) }).DeclaringType, "#02");
330 // Test overriding of properties when only the set accessor is overriden
331 Assert.AreEqual (1, typeof (Derived1).GetProperties ().Length, "#03");
335 public void GetProperties ()
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);
342 #if !TARGET_JVM // StructLayout not supported for TARGET_JVM
343 [StructLayout(LayoutKind.Explicit, Pack = 4, Size = 64)]
348 [StructLayout(LayoutKind.Explicit, CharSet=CharSet.Unicode)]
355 public void StructLayoutAttribute ()
357 StructLayoutAttribute attr1 = typeof (TypeTest).StructLayoutAttribute;
358 Assert.AreEqual (LayoutKind.Auto, attr1.Value);
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);
365 StructLayoutAttribute attr3 = typeof (Class2).StructLayoutAttribute;
366 Assert.AreEqual (LayoutKind.Explicit, attr3.Value);
367 Assert.AreEqual (CharSet.Unicode, attr3.CharSet);
373 public void Namespace ()
375 Assert.AreEqual (null, typeof (NoNamespaceClass).Namespace);
378 public static void Reflected (ref int a)
385 Assert.AreEqual ("Int32&", typeof (TypeTest).GetMethod ("Reflected").GetParameters () [0].ParameterType.Name);
389 public void GetInterfaces ()
391 Type[] t = typeof (Duper).GetInterfaces ();
392 Assert.AreEqual (1, t.Length);
393 Assert.AreEqual (typeof (ICloneable), t[0]);
395 Type[] t2 = typeof (IFace3).GetInterfaces ();
396 Assert.AreEqual (2, t2.Length);
402 public void GetFieldIgnoreCase ()
404 Assert.IsNotNull (typeof (TypeTest).GetField ("afield", BindingFlags.Instance|BindingFlags.Public|BindingFlags.IgnoreCase));
418 public void GetPropertyAccessorModifiers ()
420 Assert.IsNotNull (typeof (TypeTest).GetProperty ("Count", BindingFlags.Instance | BindingFlags.Public));
421 Assert.IsNull (typeof (TypeTest).GetProperty ("Count", BindingFlags.Instance | BindingFlags.NonPublic));
426 public void IsAbstract ()
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");
437 // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=286308
438 Type [] typeArgs = typeof (List<>).GetGenericArguments ();
439 Assert.IsFalse (typeArgs [0].IsAbstract, "#7");
444 public void IsCOMObject ()
446 Type type = typeof (string);
447 Assert.IsFalse (type.IsCOMObject, "#1");
449 TypeBuilder tb = module.DefineType (genTypeName ());
450 type = tb.CreateType ();
451 Assert.IsFalse (type.IsCOMObject, "#2");
455 public void IsImport ()
457 Type type = typeof (string);
458 Assert.IsFalse (type.IsImport, "#1");
460 TypeBuilder tb = module.DefineType (genTypeName ());
461 type = tb.CreateType ();
462 Assert.IsFalse (type.IsImport, "#2");
464 tb = module.DefineType (genTypeName (), TypeAttributes.Import |
465 TypeAttributes.Interface | TypeAttributes.Abstract);
466 type = tb.CreateType ();
467 Assert.IsTrue (type.IsImport, "#3");
471 public void IsInterface ()
473 Assert.IsFalse (typeof (string).IsInterface, "#1");
474 Assert.IsTrue (typeof (ICloneable).IsInterface, "#2");
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");
485 public void IsValueType ()
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");
496 [Category("NotDotNet")]
497 // Depends on the GAC working, which it doesn't durring make distcheck.
498 [Category ("NotWorking")]
499 public void GetTypeWithWhitespace ()
501 Assert.IsNotNull (Type.GetType
502 (@"System.Configuration.NameValueSectionHandler,
507 PublicKeyToken=b77a5c561934e089"));
511 public void ExerciseFilterName ()
513 MemberInfo[] mi = typeof(Base).FindMembers(
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(
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(
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(
533 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
534 BindingFlags.Instance | BindingFlags.DeclaredOnly,
535 Type.FilterName, "NonExistingMethod");
536 Assert.AreEqual (0, mi.Length);
540 public void ExerciseFilterNameIgnoreCase ()
542 MemberInfo[] mi = typeof(Base).FindMembers(
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(
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(
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(
562 BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic |
563 BindingFlags.Instance | BindingFlags.DeclaredOnly,
564 Type.FilterNameIgnoreCase, "NonExistingMethod");
565 Assert.AreEqual (0, mi.Length);
571 public int property {
574 public ByRef0 (int i) {}
575 public void f (int i) {}
579 public void ByrefTypes ()
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);
589 Assert.IsNull (t.GetMethod ("f"));
590 Assert.IsNull (t.GetField ("field"));
591 Assert.IsNull (t.GetProperty ("property"));
595 public void TestAssemblyQualifiedName ()
597 Type t = Type.GetType ("System.Byte[]&");
598 Assert.IsTrue (t.AssemblyQualifiedName.StartsWith ("System.Byte[]&"));
600 t = Type.GetType ("System.Byte*&");
601 Assert.IsTrue (t.AssemblyQualifiedName.StartsWith ("System.Byte*&"));
603 t = Type.GetType ("System.Byte&");
604 Assert.IsTrue (t.AssemblyQualifiedName.StartsWith ("System.Byte&"));
613 public void CreateValueTypeNoCtor ()
615 typeof(B).InvokeMember ("", BindingFlags.CreateInstance, null, null, null);
619 [ExpectedException (typeof (MissingMethodException))]
620 public void CreateValueTypeNoCtorArgs ()
622 typeof(B).InvokeMember ("", BindingFlags.CreateInstance, null, null, new object [] { 1 });
625 static string bug336841 (string param1, params string [] param2)
627 StringBuilder sb = new StringBuilder ();
631 for (int i = 0; i < param2.Length; i++) {
634 sb.Append (param2 [i]);
636 return sb.ToString ();
639 static string bug336841 (string param1)
641 return "#B:" + param1;
644 static string bug336841 (params string [] param1)
646 StringBuilder sb = new StringBuilder ();
648 for (int i = 0; i < param1.Length; i++) {
651 sb.Append (param1 [i]);
653 return sb.ToString ();
657 public void InvokeMember_GetSetField ()
659 typeof (X).InvokeMember ("Value", BindingFlags.Public |
660 BindingFlags.Static | BindingFlags.FlattenHierarchy |
661 BindingFlags.SetField, null, null, new object [] { 5 });
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");
675 typeof (X).InvokeMember ("Value", BindingFlags.Public |
676 BindingFlags.Static | BindingFlags.FlattenHierarchy |
677 BindingFlags.GetField | BindingFlags.SetField,
678 null, null, new object [] { 5 });
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");
687 Assert.AreEqual ("bindingFlags", ex.ParamName, "#B6");
689 Assert.AreEqual ("invokeAttr", ex.ParamName, "#B6");
695 public void InvokeMember_GetSetProperty ()
698 typeof (ArrayList).InvokeMember ("Item",
699 BindingFlags.GetProperty | BindingFlags.SetProperty |
700 BindingFlags.Instance | BindingFlags.Public,
701 null, new ArrayList (), new object [] { 0, "bar" });
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");
710 Assert.AreEqual ("bindingFlags", ex.ParamName, "#6");
712 Assert.AreEqual ("invokeAttr", ex.ParamName, "#6");
719 public void InvokeMember_InvokeMethod_Set ()
722 typeof (ArrayList).InvokeMember ("ToString",
723 BindingFlags.InvokeMethod | BindingFlags.SetField |
724 BindingFlags.Instance | BindingFlags.Public,
725 null, new ArrayList (), new object [0]);
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");
734 Assert.AreEqual ("bindingFlags", ex.ParamName, "#A6");
736 Assert.AreEqual ("invokeAttr", ex.ParamName, "#A6");
741 typeof (ArrayList).InvokeMember ("ToString",
742 BindingFlags.InvokeMethod | BindingFlags.SetProperty |
743 BindingFlags.Instance | BindingFlags.Public,
744 null, new ArrayList (), new object [0]);
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");
753 Assert.AreEqual ("bindingFlags", ex.ParamName, "#B6");
755 Assert.AreEqual ("invokeAttr", ex.ParamName, "#B6");
761 public void InvokeMember_MatchPrimitiveTypeWithInterface ()
763 object [] invokeargs = { 1 };
764 typeof (Z).InvokeMember ("", BindingFlags.DeclaredOnly |
765 BindingFlags.Public | BindingFlags.NonPublic |
766 BindingFlags.Instance | BindingFlags.CreateInstance,
767 null, null, invokeargs);
771 public void InvokeMember_Name_Null ()
774 typeof (X).InvokeMember ((string) null,
775 BindingFlags.Public | BindingFlags.Static |
776 BindingFlags.FlattenHierarchy | BindingFlags.SetField,
777 null, null, new object [] { 5 });
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");
789 public void InvokeMember_NoOperation ()
792 typeof (TypeTest).InvokeMember ("Run", BindingFlags.Public |
793 BindingFlags.Static, null, null, new object [0]);
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");
803 Assert.AreEqual ("bindingFlags", ex.ParamName, "#6");
805 Assert.AreEqual ("invokeAttr", ex.ParamName, "#6");
810 [Test] // bug #321735
811 public void InvokeMember_SetFieldProperty ()
813 ArrayList list = new ArrayList ();
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]);
823 public void InvokeMember_SetField_ProvidedArgs ()
826 typeof (X).InvokeMember ("Value", BindingFlags.Public |
827 BindingFlags.Static | BindingFlags.SetField,
828 null, null, new object [0]);
830 } catch (ArgumentException ex) {
831 // Only the field value can be specified to set
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");
838 Assert.AreEqual ("bindingFlags", ex.ParamName, "#6");
840 Assert.AreEqual ("invokeAttr", ex.ParamName, "#6");
845 typeof (X).InvokeMember ("Value", BindingFlags.Public |
846 BindingFlags.Static | BindingFlags.SetField,
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");
858 } catch (ArgumentException ex) {
859 // Only the field value can be specified to set
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");
866 Assert.AreEqual ("bindingFlags", ex.ParamName, "#B6");
868 Assert.AreEqual ("invokeAttr", ex.ParamName, "#B6");
874 [Test] // bug #336841
875 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=306797
876 public void InvokeMember_VarArgs ()
878 BindingFlags flags = BindingFlags.InvokeMethod | BindingFlags.Public |
879 BindingFlags.NonPublic | BindingFlags.OptionalParamBinding |
880 BindingFlags.Static | BindingFlags.FlattenHierarchy |
881 BindingFlags.Instance;
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");
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");
897 public static int Value;
906 public Z (IComparable value)
911 public static void Run ()
919 public TakesInt (int x)
931 public TakesObject (object x) {}
935 public void GetConstructorNullInTypes ()
937 // This ends up calling type.GetConstructor ()
938 Activator.CreateInstance (typeof (TakesInt), new object [] { null });
939 Activator.CreateInstance (typeof (TakesObject), new object [] { null });
943 public void GetConstructor_TakeInt_Object ()
945 Assert.IsNull (typeof (TakesInt).GetConstructor (new Type[1] { typeof (object) }));
949 public void GetCustomAttributes_All ()
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");
956 attrs = typeof (BA).GetCustomAttributes (false);
957 Assert.AreEqual (1, attrs.Length, "#B1");
958 Assert.AreEqual (typeof (BarAttribute), attrs [0].GetType (), "#B2");
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");
965 attrs = typeof (CA).GetCustomAttributes (false);
966 Assert.AreEqual (0, attrs.Length, "#D");
968 attrs = typeof (CA).GetCustomAttributes (true);
969 Assert.AreEqual (1, attrs.Length, "#E1");
970 Assert.AreEqual (typeof (VolatileModifier), attrs [0].GetType (), "#E2");
973 static bool HasAttribute (object [] attrs, Type attributeType)
975 foreach (object attr in attrs)
976 if (attr.GetType () == attributeType)
982 public void GetCustomAttributes_Type ()
984 object [] attrs = null;
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
1163 [Test] // bug #76150
1164 public void IsDefined ()
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");
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");
1186 public void IsDefined_AttributeType_Null ()
1189 typeof (BA).IsDefined ((Type) null, false);
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");
1200 [Test] // bug #82431
1202 [Category ("NotWorking")]
1204 public void IsDefined_Inherited ()
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");
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");
1222 Assert.IsTrue (typeof (BBA).IsDefined (typeof (FooAttribute), true), "#D7");
1223 Assert.IsTrue (typeof (BBA).IsDefined (typeof (BarAttribute), true), "#D8");
1225 Assert.IsFalse (typeof (BBA).IsDefined (typeof (FooAttribute), true), "#D7");
1226 Assert.IsFalse (typeof (BBA).IsDefined (typeof (BarAttribute), true), "#D8");
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");
1234 Assert.IsFalse (typeof (bug82431A2).IsDefined (typeof (InheritAttribute), false), "#F1");
1235 Assert.IsFalse (typeof (bug82431A2).IsDefined (typeof (NotInheritAttribute), false), "#F2");
1237 Assert.IsFalse (typeof (bug82431A2).IsDefined (typeof (InheritAttribute), true), "#F3");
1239 Assert.IsTrue (typeof (bug82431A2).IsDefined (typeof (InheritAttribute), true), "#F3");
1241 Assert.IsFalse (typeof (bug82431A2).IsDefined (typeof (NotInheritAttribute), true), "#F4");
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");
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");
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");
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");
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");
1270 public void GetTypeCode ()
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");
1292 [Test] // GetConstructor (Type [])
1293 public void GetConstructor1_Types_Null ()
1296 typeof (BindingFlags).GetConstructor (null);
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");
1307 [Test] // GetConstructor (Type [])
1308 public void GetConstructor1_Types_ItemNull ()
1310 Type type = typeof (BindingFlags);
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");
1322 type = typeof (TakesInt);
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");
1335 [Test] // GetConstructor (BindingFlags, Binder, Type [], ParameterModifier [])
1336 public void GetConstructor2_Types_ItemNull ()
1338 Type type = typeof (BindingFlags);
1340 type.GetConstructor (BindingFlags.Default, null,
1341 new Type[1] { null }, null);
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");
1352 [Test] // GetConstructor (BindingFlags, Binder, CallingConventions, Type [], ParameterModifier [])
1353 public void GetConstructor3_Types_ItemNull ()
1355 Type type = typeof (BindingFlags);
1357 type.GetConstructor (BindingFlags.Default,
1358 null, CallingConventions.Any,
1359 new Type[1] { null }, null);
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");
1371 public void GetMethod_Bug77367 ()
1373 MethodInfo i = typeof (Bug77367).GetMethod ("Run", Type.EmptyTypes);
1377 #if !TARGET_JVM // Reflection.Emit is not supported for TARGET_JVM
1379 public void EqualsUnderlyingType ()
1381 AssemblyBuilderAccess access = AssemblyBuilderAccess.RunAndSave;
1382 TypeAttributes attribs = TypeAttributes.Public;
1384 AssemblyName name = new AssemblyName ();
1385 name.Name = "enumtest";
1386 AssemblyBuilder assembly =
1387 AppDomain.CurrentDomain.DefineDynamicAssembly (
1390 ModuleBuilder module = assembly.DefineDynamicModule
1391 ("m", "enumtest.dll");
1392 EnumBuilder e = module.DefineEnum ("E", attribs, typeof (int));
1394 Assert.IsTrue (typeof (int).Equals (e));
1396 #endif // TARGET_JVM
1399 public void Equals_Type_Null ()
1401 Assert.IsFalse (typeof (int).Equals ((Type) null), "#1");
1402 Assert.IsFalse (typeof (int).Equals ((object) null), "#2");
1406 public void GetElementType_Bug63841 ()
1408 Assert.IsNull (typeof (TheEnum).GetElementType (), "#1");
1413 public void FullNameGenerics ()
1415 Type fooType = typeof (Foo<>);
1416 FieldInfo [] fields = fooType.GetFields ();
1418 Assert.AreEqual (1, fields.Length, "#0");
1420 Assert.IsNotNull (fooType.FullName, "#1");
1421 Assert.IsNotNull (fooType.AssemblyQualifiedName, "#1a");
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");
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");
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");
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");
1450 public void TypeParameterIsNotGeneric ()
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);
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);
1464 public void IsAssignable ()
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>");
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>);
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>");
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>");
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)));
1489 public void IsInstanceOf ()
1491 Assert.IsTrue (typeof (Nullable<int>).IsInstanceOfType (5));
1495 public void ByrefType ()
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);
1505 public class ComFoo<T> {
1509 public void GetCustomAttributesGenericInstance ()
1511 Assert.AreEqual (1, typeof (ComFoo<int>).GetCustomAttributes (typeof (ComVisibleAttribute), true).Length);
1514 interface ByRef1<T> { void f (ref T t); }
1515 interface ByRef2 { void f<T> (ref T t); }
1517 interface ByRef3<T> where T:struct { void f (ref T? t); }
1518 interface ByRef4 { void f<T> (ref T? t) where T:struct; }
1520 void CheckGenericByRef (Type t)
1522 string name = t.Name;
1523 t = t.GetMethod ("f").GetParameters () [0].ParameterType;
1525 Assert.IsFalse (t.IsGenericType, name);
1526 Assert.IsFalse (t.IsGenericTypeDefinition, name);
1527 Assert.IsFalse (t.IsGenericParameter, name);
1531 public void GenericByRef ()
1533 CheckGenericByRef (typeof (ByRef1<>));
1534 CheckGenericByRef (typeof (ByRef2));
1535 CheckGenericByRef (typeof (ByRef3<>));
1536 CheckGenericByRef (typeof (ByRef4));
1539 public class Bug80242<T> {
1540 public interface IFoo { }
1541 public class Bar : IFoo { }
1542 public class Baz : Bar { }
1546 public void TestNestedTypes ()
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);
1556 [Test] // bug #82211
1557 public void GetMembers_GenericArgument ()
1559 Type argType = typeof (ComFoo<>).GetGenericArguments () [0];
1560 MemberInfo [] members = argType.GetMembers ();
1561 Assert.IsNotNull (members, "#1");
1562 Assert.AreEqual (4, members.Length, "#2");
1566 [ExpectedException (typeof (ArgumentNullException))]
1567 public void ReflectionOnlyGetTypeNullTypeName ()
1569 Type.ReflectionOnlyGetType (null, false, false);
1573 public void ReflectionOnlyGetTypeDoNotThrow ()
1575 Assert.IsNull (Type.ReflectionOnlyGetType ("a, nonexistent.dll", false, false));
1579 [ExpectedException (typeof (FileNotFoundException))]
1580 public void ReflectionOnlyGetTypeThrow ()
1582 Type.ReflectionOnlyGetType ("a, nonexistent.dll", true, false);
1586 public void ReflectionOnlyGetType ()
1588 Type t = Type.ReflectionOnlyGetType (typeof (int).AssemblyQualifiedName.ToString (), true, true);
1589 Assert.AreEqual ("System.Int32", t.FullName);
1592 [Test] //bug #331199
1593 //FIXME: 2.0 SP 1 has a diferent behavior
1594 public void MakeGenericType_UserDefinedType ()
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");
1601 Type arg = t.GetGenericArguments () [0];
1602 Assert.IsNotNull (arg, "#B1");
1603 Assert.IsFalse (arg.IsGenericType, "#B2");
1604 Assert.AreEqual (typeof (int), arg, "#B3");
1607 [Category ("NotWorking")]
1608 //We dont support instantiating a user type
1609 public void MakeGenericType_NestedUserDefinedType ()
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");
1616 Type arg = t.GetGenericArguments () [0];
1617 Assert.IsNotNull (arg, "#B1");
1618 Assert.IsFalse (arg.IsGenericType, "#B2");
1619 Assert.AreEqual (ut, arg, "#B3");
1623 [Category ("NotWorking")]
1624 public void TestMakeGenericType_UserDefinedType_DotNet20SP1 ()
1626 Type ut = new UserType(typeof(int));
1627 Type t = typeof(Foo<>).MakeGenericType(ut);
1628 Assert.IsTrue (t.IsGenericType, "#1");
1630 Assert.AreEqual (ut, t.GetGenericArguments()[0], "#2");
1634 public void MakeGenericType_BadUserType ()
1636 Type ut = new UserType (null);
1638 Type t = typeof (Foo<>).MakeGenericType (ut);
1640 } catch (ArgumentException) {
1645 public class NemerleAttribute : Attribute
1649 public class VolatileModifier : NemerleAttribute
1659 [AttributeUsage (AttributeTargets.Class, Inherited=false)]
1660 public class FooAttribute : Attribute
1664 public class BarAttribute : FooAttribute
1681 [AttributeUsage (AttributeTargets.Class, Inherited=true)]
1682 public class InheritAttribute : Attribute
1686 [AttributeUsage (AttributeTargets.Class, Inherited=false)]
1687 public class NotInheritAttribute : InheritAttribute
1691 [NotInheritAttribute]
1692 public class bug82431A1
1696 public class bug82431A2 : bug82431A1
1700 [NotInheritAttribute]
1702 public class bug82431A3 : bug82431A1
1707 public class bug82431B1
1711 public class bug82431B2 : bug82431B1
1715 [NotInheritAttribute]
1716 public class bug82431B3 : bug82431B2
1720 public class bug82431B4 : bug82431B3
1728 public class Bug77367
1730 public void Run (bool b)
1737 class UserType : Type
1741 public UserType(Type type) {
1745 public override Type UnderlyingSystemType { get { return this.type; } }
1747 public override Assembly Assembly { get { return this.type.Assembly; } }
1749 public override string AssemblyQualifiedName { get { return null; } }
1751 public override Type BaseType { get { return null; } }
1753 public override Module Module { get { return this.type.Module; } }
1755 public override string Namespace { get { return null; } }
1757 public override bool IsGenericParameter { get { return true; } }
1759 public override RuntimeTypeHandle TypeHandle { get { throw new NotSupportedException(); } }
1761 public override bool ContainsGenericParameters { get { return true; } }
1763 public override string FullName { get { return this.type.Name; } }
1765 public override Guid GUID { get { throw new NotSupportedException(); } }
1768 protected override bool IsArrayImpl() {
1772 protected override bool IsByRefImpl()
1777 protected override bool IsCOMObjectImpl()
1782 protected override bool IsPointerImpl()
1787 protected override bool IsPrimitiveImpl()
1793 protected override TypeAttributes GetAttributeFlagsImpl()
1798 protected override ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr, Binder binder,
1799 CallingConventions callConvention, Type[] types,
1800 ParameterModifier[] modifiers)
1805 public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
1810 public override Type GetElementType()
1815 public override EventInfo GetEvent(string name, BindingFlags bindingAttr)
1821 public override FieldInfo GetField(string name, BindingFlags bindingAttr)
1827 public override Type GetInterface(string name, bool ignoreCase)
1832 public override Type[] GetInterfaces()
1837 public override MemberInfo[] GetMembers(BindingFlags bindingAttr)
1842 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
1847 public override object[] GetCustomAttributes(bool inherit)
1852 public override bool IsDefined(Type attributeType, bool inherit)
1857 public override string Name { get { return this.type.Name; } }
1859 public override EventInfo[] GetEvents(BindingFlags bindingAttr)
1861 throw new NotImplementedException();
1864 public override FieldInfo[] GetFields(BindingFlags bindingAttr)
1866 throw new NotImplementedException();
1869 protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder binder,
1870 CallingConventions callConvention, Type[] types,
1871 ParameterModifier[] modifiers)
1876 public override MethodInfo[] GetMethods(BindingFlags bindingAttr)
1881 public override Type GetNestedType(string name, BindingFlags bindingAttr)
1886 public override Type[] GetNestedTypes(BindingFlags bindingAttr)
1891 public override PropertyInfo[] GetProperties(BindingFlags bindingAttr)
1896 protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder binder,
1897 Type returnType, Type[] types, ParameterModifier[] modifiers)
1902 protected override bool HasElementTypeImpl()
1907 public override object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target,
1908 object[] args, ParameterModifier[] modifiers, CultureInfo culture,
1909 string[] namedParameters)
1911 throw new NotSupportedException();