3 // TypeBuilderTest.cs - NUnit Test Cases for the TypeBuilder class
5 // Zoltan Varga (vargaz@freemail.hu)
7 // (C) Ximian, Inc. http://www.ximian.com
10 // - implement a mechnanism for easier testing of null argument exceptions
11 // - with overloaded methods like DefineNestedType (), check the defaults
12 // on the shorter versions.
13 // - ToString on enums with the flags attribute set should print all
14 // values which match, e.g. 0 == AutoLayou,AnsiClass,NotPublic
18 using System.Threading;
19 using System.Reflection;
20 using System.Reflection.Emit;
22 using System.Security;
23 using System.Security.Permissions;
24 using System.Runtime.InteropServices;
25 using NUnit.Framework;
27 namespace MonoTests.System.Reflection.Emit
31 public class TypeBuilderTest : Assertion
33 private interface AnInterface {
45 private AssemblyBuilder assembly;
47 private ModuleBuilder module;
49 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
52 protected void SetUp () {
53 AssemblyName assemblyName = new AssemblyName();
54 assemblyName.Name = ASSEMBLY_NAME;
57 Thread.GetDomain().DefineDynamicAssembly(
58 assemblyName, AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
60 module = assembly.DefineDynamicModule("module1");
63 static int typeIndexer = 0;
65 // Return a unique type name
66 private string genTypeName () {
67 return "t" + (typeIndexer ++);
70 private string nullName () {
71 return String.Format ("{0}", (char)0);
75 public void TestAssembly () {
76 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
77 AssertEquals ("Assembly works",
78 tb.Assembly, assembly);
82 public void TestAssemblyQualifiedName () {
83 TypeBuilder tb = module.DefineType ("A.B.C.D", TypeAttributes.Public);
85 AssertEquals ("AssemblyQualifiedName works",
86 tb.AssemblyQualifiedName, "A.B.C.D, " + assembly.GetName ().FullName);
90 public void TestAttributes () {
91 TypeAttributes attrs = TypeAttributes.Public | TypeAttributes.BeforeFieldInit;
92 TypeBuilder tb = module.DefineType (genTypeName (), attrs);
94 AssertEquals ("Attributes works",
95 tb.Attributes, attrs);
99 public void TestBaseTypeClass () {
100 TypeAttributes attrs = TypeAttributes.Public;
101 TypeBuilder tb = module.DefineType (genTypeName (), attrs);
102 AssertEquals ("BaseType defaults to Object",
103 tb.BaseType, typeof (object));
105 TypeBuilder tb2 = module.DefineType (genTypeName (), attrs, tb);
106 AssertEquals ("BaseType works",
111 [Category("NotWorking")]
113 public void TestBaseTypeInterface ()
115 TypeBuilder tb3 = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
116 AssertEquals ("Interfaces should default to no base type", null, tb3.BaseType);
120 public void TestDeclaringType () {
121 TypeAttributes attrs = 0;
122 TypeBuilder tb = module.DefineType (genTypeName (), attrs);
124 AssertEquals ("Has no declaring type",
125 null, tb.DeclaringType);
127 attrs = TypeAttributes.NestedPublic;
128 TypeBuilder tb2 = tb.DefineNestedType (genTypeName (), attrs);
129 TypeBuilder tb3 = tb2.DefineNestedType (genTypeName (), attrs);
130 AssertEquals ("DeclaringType works",
131 tb, tb3.DeclaringType.DeclaringType);
135 public void TestFullName () {
136 string name = genTypeName ();
137 TypeAttributes attrs = 0;
138 TypeBuilder tb = module.DefineType (name, attrs);
139 AssertEquals ("FullName works",
142 string name2 = genTypeName ();
143 attrs = TypeAttributes.NestedPublic;
144 TypeBuilder tb2 = tb.DefineNestedType (name2, attrs);
146 string name3 = genTypeName ();
147 attrs = TypeAttributes.NestedPublic;
148 TypeBuilder tb3 = tb2.DefineNestedType (name3, attrs);
150 AssertEquals ("FullName works on nested types",
151 name + "+" + name2 + "+" + name3, tb3.FullName);
155 [ExpectedException (typeof(NotSupportedException))]
156 public void TestGUIDIncomplete () {
157 TypeBuilder tb = module.DefineType (genTypeName ());
162 [Category("NotWorking")]
164 public void TestGUIDComplete ()
166 TypeBuilder tb = module.DefineType (genTypeName ());
168 Assert(tb.GUID != Guid.Empty);
172 [Category("NotWorking")]
173 public void TestFixedGUIDComplete ()
175 TypeBuilder tb = module.DefineType (genTypeName ());
177 Guid guid = Guid.NewGuid ();
179 ConstructorInfo guidCtor = typeof(GuidAttribute).GetConstructor(
180 new Type[] {typeof(string)});
182 CustomAttributeBuilder caBuilder = new CustomAttributeBuilder (guidCtor,
183 new object[] { guid.ToString("D") }, new FieldInfo[0], new object[0]);
185 tb.SetCustomAttribute (caBuilder);
187 AssertEquals (guid, tb.GUID);
191 [ExpectedException (typeof(NotSupportedException))]
192 public void TestHasElementType () {
193 // According to the MSDN docs, this member works, but in reality, it
194 // returns a NotSupportedException
195 TypeBuilder tb = module.DefineType (genTypeName ());
196 bool b = tb.HasElementType;
200 public void TestIsAbstract () {
201 TypeBuilder tb = module.DefineType (genTypeName ());
203 false, tb.IsAbstract);
205 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.Abstract);
207 true, tb2.IsAbstract);
211 public void TestIsAnsiClass () {
212 TypeBuilder tb = module.DefineType (genTypeName ());
214 true, tb.IsAnsiClass);
216 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.UnicodeClass);
218 false, tb2.IsAnsiClass);
222 public void TestIsArray () {
223 // How can a TypeBuilder be an array ?
224 string name = genTypeName ();
225 TypeBuilder tb = module.DefineType (name);
226 AssertEquals ("IsArray works",
231 public void TestIsAutoClass () {
232 TypeBuilder tb = module.DefineType (genTypeName ());
234 false, tb.IsAutoClass);
236 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.AutoClass);
238 true, tb2.IsAutoClass);
242 public void TestIsAutoLayout () {
243 TypeBuilder tb = module.DefineType (genTypeName ());
244 AssertEquals ("AutoLayout defaults to true",
245 true, tb.IsAutoLayout);
247 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.ExplicitLayout);
249 false, tb2.IsAutoLayout);
253 public void TestIsByRef () {
254 // How can a TypeBuilder be ByRef ?
255 TypeBuilder tb = module.DefineType (genTypeName ());
256 AssertEquals ("IsByRef works",
261 public void TestIsClass () {
262 TypeBuilder tb = module.DefineType (genTypeName ());
263 AssertEquals ("Most types are classes",
266 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
267 AssertEquals ("Interfaces are not classes",
270 TypeBuilder tb3 = module.DefineType (genTypeName (), 0, typeof (ValueType));
271 AssertEquals ("value types are not classes",
274 TypeBuilder tb4 = module.DefineType (genTypeName (), 0, typeof (Enum));
275 AssertEquals ("enums are not classes",
280 [Category("NotWorking")]
282 public void TestIsCOMObject () {
283 TypeBuilder tb = module.DefineType (genTypeName ());
284 AssertEquals ("Probably not", false, tb.IsCOMObject);
286 tb = module.DefineType (genTypeName (), TypeAttributes.Import);
287 AssertEquals ("type with Import attribute is COM object",
288 true, tb.IsCOMObject);
292 public void TestIsContextful () {
293 TypeBuilder tb = module.DefineType (genTypeName ());
294 AssertEquals (false, tb.IsContextful);
296 TypeBuilder tb2 = module.DefineType (genTypeName (), 0, typeof (ContextBoundObject));
297 AssertEquals (true, tb2.IsContextful);
301 public void TestIsEnum () {
302 TypeBuilder tb = module.DefineType (genTypeName ());
303 AssertEquals (false, tb.IsEnum);
305 // This returns true under both mono and MS .NET ???
306 TypeBuilder tb2 = module.DefineType (genTypeName (), 0, typeof (ValueType));
307 AssertEquals ("value types are not necessary enums",
310 TypeBuilder tb3 = module.DefineType (genTypeName (), 0, typeof (Enum));
311 AssertEquals ("enums are enums", true, tb3.IsEnum);
315 public void TestIsExplicitLayout () {
316 TypeBuilder tb = module.DefineType (genTypeName ());
317 AssertEquals ("ExplicitLayout defaults to false",
318 false, tb.IsExplicitLayout);
320 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.ExplicitLayout);
321 AssertEquals (true, tb2.IsExplicitLayout);
325 public void TestIsImport () {
326 // How can this be true ?
327 TypeBuilder tb = module.DefineType (genTypeName ());
328 AssertEquals (false, tb.IsImport);
332 public void TestIsInterface () {
333 TypeBuilder tb = module.DefineType (genTypeName ());
334 AssertEquals ("Most types are not interfaces",
335 false, tb.IsInterface);
337 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
338 AssertEquals ("Interfaces are interfaces",
339 true, tb2.IsInterface);
341 TypeBuilder tb3 = module.DefineType (genTypeName (), 0, typeof (ValueType));
342 AssertEquals ("value types are not interfaces",
343 false, tb3.IsInterface);
347 public void TestIsLayoutSequential () {
348 TypeBuilder tb = module.DefineType (genTypeName ());
349 AssertEquals ("SequentialLayout defaults to false",
350 false, tb.IsLayoutSequential);
352 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.SequentialLayout);
353 AssertEquals (true, tb2.IsLayoutSequential);
357 public void TestIsMarshalByRef () {
358 TypeBuilder tb = module.DefineType (genTypeName ());
359 AssertEquals (false, tb.IsMarshalByRef);
361 TypeBuilder tb2 = module.DefineType (genTypeName (), 0, typeof (MarshalByRefObject));
362 AssertEquals (true, tb2.IsMarshalByRef);
364 TypeBuilder tb3 = module.DefineType (genTypeName (), 0, typeof (ContextBoundObject));
365 AssertEquals (true, tb3.IsMarshalByRef);
368 // TODO: Visibility properties
371 public void TestIsPointer () {
372 // How can this be true?
373 TypeBuilder tb = module.DefineType (genTypeName ());
374 AssertEquals (false, tb.IsPointer);
378 public void TestIsPrimitive () {
379 TypeBuilder tb = module.DefineType ("int");
380 AssertEquals (false, tb.IsPrimitive);
384 public void IsSealed () {
385 TypeBuilder tb = module.DefineType (genTypeName ());
386 AssertEquals ("Sealed defaults to false",
389 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.Sealed);
390 AssertEquals ("IsSealed works", true, tb2.IsSealed);
394 public void IsSerializable () {
395 TypeBuilder tb = module.DefineType (genTypeName ());
396 AssertEquals (false, tb.IsSerializable);
398 ConstructorInfo[] ctors = typeof (SerializableAttribute).GetConstructors (BindingFlags.Instance | BindingFlags.Public);
399 Assert ("SerializableAttribute should have more than 0 public instance ctors",
402 tb.SetCustomAttribute (new CustomAttributeBuilder (ctors[0], new object[0]));
403 Type createdType = tb.CreateType ();
405 assembly.Save ("TestAssembly.dll");
406 AssertEquals (true, createdType.IsSerializable);
410 public void TestIsSpecialName () {
411 TypeBuilder tb = module.DefineType (genTypeName ());
412 AssertEquals ("SpecialName defaults to false",
413 false, tb.IsSpecialName);
415 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.SpecialName);
416 AssertEquals ("IsSpecialName works",
417 true, tb2.IsSpecialName);
421 public void TestIsUnicodeClass () {
422 TypeBuilder tb = module.DefineType (genTypeName ());
423 AssertEquals (false, tb.IsUnicodeClass);
425 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.UnicodeClass);
426 AssertEquals (true, tb2.IsUnicodeClass);
430 public void TestIsValueType () {
431 TypeBuilder tb = module.DefineType (genTypeName ());
432 AssertEquals ("Most types are not value types",
433 false, tb.IsValueType);
435 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
436 AssertEquals ("Interfaces are not value types",
437 false, tb2.IsValueType);
439 TypeBuilder tb3 = module.DefineType (genTypeName (), 0, typeof (ValueType));
440 AssertEquals ("value types are value types",
441 true, tb3.IsValueType);
443 TypeBuilder tb4 = module.DefineType (genTypeName (), 0, typeof (Enum));
444 AssertEquals ("enums are value types",
445 true, tb4.IsValueType);
449 public void TestMemberType () {
450 TypeBuilder tb = module.DefineType (genTypeName ());
451 AssertEquals ("A type is a type",
452 MemberTypes.TypeInfo, tb.MemberType);
456 public void TestModule () {
457 TypeBuilder tb = module.DefineType (genTypeName ());
458 AssertEquals ("Module works", module, tb.Module);
462 public void TestName () {
463 TypeBuilder tb = module.DefineType ("A");
464 AssertEquals ("A", tb.Name);
466 TypeBuilder tb2 = module.DefineType ("A.B.C.D.E");
467 AssertEquals ("E", tb2.Name);
469 TypeBuilder tb3 = tb2.DefineNestedType ("A");
470 AssertEquals ("A", tb3.Name);
472 /* Is .E a valid name ?
473 TypeBuilder tb4 = module.DefineType (".E");
480 public void TestNamespace () {
481 TypeBuilder tb = module.DefineType ("A");
482 AssertEquals ("", tb.Namespace);
484 TypeBuilder tb2 = module.DefineType ("A.B.C.D.E");
485 AssertEquals ("A.B.C.D", tb2.Namespace);
487 TypeBuilder tb3 = tb2.DefineNestedType ("A");
488 AssertEquals ("", tb3.Namespace);
490 /* Is .E a valid name ?
491 TypeBuilder tb4 = module.DefineType (".E");
498 public void TestPackingSize () {
499 TypeBuilder tb = module.DefineType (genTypeName ());
500 AssertEquals (PackingSize.Unspecified, tb.PackingSize);
502 TypeBuilder tb2 = module.DefineType (genTypeName (), 0, typeof (object),
503 PackingSize.Size16, 16);
504 AssertEquals (PackingSize.Size16, tb2.PackingSize);
508 public void TestReflectedType () {
509 // It is the same as DeclaringType, but why?
510 TypeBuilder tb = module.DefineType (genTypeName ());
511 AssertEquals (null, tb.ReflectedType);
513 TypeBuilder tb2 = tb.DefineNestedType (genTypeName ());
514 AssertEquals (tb, tb2.ReflectedType);
518 [ExpectedException (typeof(ArgumentNullException))]
519 public void TestSetParentNull ()
521 TypeBuilder tb = module.DefineType (genTypeName ());
526 public void TestSetParentIncomplete ()
528 TypeBuilder tb = module.DefineType (genTypeName ());
529 tb.SetParent (typeof(Attribute));
530 AssertEquals (typeof(Attribute), tb.BaseType);
534 [ExpectedException (typeof(InvalidOperationException))]
535 public void TestSetParentComplete ()
537 TypeBuilder tb = module.DefineType (genTypeName ());
539 tb.SetParent (typeof(Attribute));
543 public void TestSize () {
545 TypeBuilder tb = module.DefineType (genTypeName ());
546 AssertEquals (0, tb.Size);
548 AssertEquals (0, tb.Size);
552 TypeBuilder tb = module.DefineType (genTypeName (), 0, typeof (object),
553 PackingSize.Size16, 32);
554 AssertEquals (32, tb.Size);
559 [ExpectedException (typeof(NotSupportedException))]
560 public void TestTypeHandle () {
561 TypeBuilder tb = module.DefineType (genTypeName ());
562 RuntimeTypeHandle handle = tb.TypeHandle;
566 [ExpectedException (typeof(NotSupportedException))]
567 public void TestTypeInitializerIncomplete ()
569 TypeBuilder tb = module.DefineType (genTypeName ());
570 ConstructorInfo cb = tb.TypeInitializer;
574 public void TestTypeInitializerComplete ()
576 TypeBuilder tb = module.DefineType (genTypeName ());
578 ConstructorInfo cb = tb.TypeInitializer;
582 public void TestTypeToken () {
583 TypeBuilder tb = module.DefineType (genTypeName ());
584 TypeToken token = tb.TypeToken;
588 [Category("NotWorking")]
589 public void TestUnderlyingSystemType () {
591 TypeBuilder tb = module.DefineType (genTypeName ());
592 AssertEquals ("For non-enums this equals itself",
593 tb, tb.UnderlyingSystemType);
596 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
597 AssertEquals (tb, tb.UnderlyingSystemType);
600 TypeBuilder tb = module.DefineType (genTypeName (), 0, typeof (ValueType));
601 AssertEquals (tb, tb.UnderlyingSystemType);
605 TypeBuilder tb = module.DefineType (genTypeName (), 0, typeof (Enum));
607 Type t = tb.UnderlyingSystemType;
610 catch (InvalidOperationException) {
613 tb.DefineField ("val", typeof (int), 0);
614 AssertEquals (typeof (int), tb.UnderlyingSystemType);
619 public void TestAddInterfaceImplementation () {
620 TypeBuilder tb = module.DefineType (genTypeName ());
622 tb.AddInterfaceImplementation (null);
625 catch (ArgumentNullException) {
628 tb.AddInterfaceImplementation (typeof (AnInterface));
629 tb.AddInterfaceImplementation (typeof (AnInterface));
631 Type t = tb.CreateType ();
632 AssertEquals ("Should merge identical interfaces",
633 tb.GetInterfaces ().Length, 1);
635 // Can not be called on a created type
637 tb.AddInterfaceImplementation (typeof (AnInterface));
640 catch (InvalidOperationException) {
645 [Category("NotWorking")]
646 public void TestCreateType () {
647 // TODO: LOTS OF TEST SHOULD GO THERE
648 TypeBuilder tb = module.DefineType (genTypeName ());
651 // Can not be called on a created type
656 catch (InvalidOperationException) {
661 public void TestDefineConstructor () {
662 TypeBuilder tb = module.DefineType (genTypeName ());
664 ConstructorBuilder cb = tb.DefineConstructor (0, 0, null);
665 cb.GetILGenerator ().Emit (OpCodes.Ret);
668 // Can not be called on a created type
670 tb.DefineConstructor (0, 0, null);
673 catch (InvalidOperationException) {
678 public void TestDefineDefaultConstructor () {
679 TypeBuilder tb = module.DefineType (genTypeName ());
680 tb.DefineDefaultConstructor (0);
683 // Can not be called on a created type, altough the MSDN docs does not mention this
685 tb.DefineDefaultConstructor (0);
688 catch (InvalidOperationException) {
693 [ExpectedException (typeof(InvalidOperationException))]
694 [Category("NotWorking")]
695 public void TestDefineDefaultConstructorParent () {
696 TypeBuilder tb = module.DefineType (genTypeName ());
697 tb.DefineConstructor (MethodAttributes.Public,
698 CallingConventions.Standard,
699 new Type[] { typeof(string) });
700 Type type = tb.CreateType ();
702 // create TypeBuilder for type that derived from the
703 // previously created type (which has no default ctor)
704 tb = module.DefineType (genTypeName (), TypeAttributes.Class
705 | TypeAttributes.Public, type);
707 // you cannot create a type with a default ctor that
708 // derives from a type without a default ctor
713 public void TestDefineEvent () {
714 TypeBuilder tb = module.DefineType (genTypeName ());
716 // Test invalid arguments
718 tb.DefineEvent (null, 0, typeof (int));
721 catch (ArgumentNullException) {
725 tb.DefineEvent ("FOO", 0, null);
728 catch (ArgumentNullException) {
732 tb.DefineEvent ("", 0, typeof (int));
735 catch (ArgumentException) {
739 // Can not be called on a created type
741 tb.DefineEvent ("BAR", 0, typeof (int));
744 catch (InvalidOperationException) {
749 public void TestDefineField () {
750 TypeBuilder tb = module.DefineType (genTypeName ());
752 // Check invalid arguments
754 tb.DefineField (null, typeof (int), 0);
757 catch (ArgumentNullException) {
761 tb.DefineField ("", typeof (int), 0);
764 catch (ArgumentException) {
768 // Strangely, 'A<NULL>' is accepted...
769 string name = String.Format ("{0}", (char)0);
770 tb.DefineField (name, typeof (int), 0);
771 Fail ("Names with embedded nulls should be rejected");
773 catch (ArgumentException) {
777 tb.DefineField ("A", typeof (void), 0);
780 catch (ArgumentException) {
784 // Can not be called on a created type
786 tb.DefineField ("B", typeof (int), 0);
789 catch (InvalidOperationException) {
794 public void TestDefineInitializedData () {
795 TypeBuilder tb = module.DefineType (genTypeName ());
797 // Check invalid arguments
799 tb.DefineInitializedData (null, new byte[1], 0);
802 catch (ArgumentNullException) {
806 tb.DefineInitializedData ("FOO", null, 0);
809 catch (ArgumentNullException) {
813 tb.DefineInitializedData ("", new byte[1], 0);
816 catch (ArgumentException) {
819 // The size of the data is less than or equal to zero ???
821 tb.DefineInitializedData ("BAR", new byte[0], 0);
824 catch (ArgumentException) {
828 string name = String.Format ("{0}", (char)0);
829 tb.DefineInitializedData (name, new byte[1], 0);
830 Fail ("Names with embedded nulls should be rejected");
832 catch (ArgumentException) {
837 // Can not be called on a created type, altough the MSDN docs does not mention this
839 tb.DefineInitializedData ("BAR2", new byte[1], 0);
842 catch (InvalidOperationException) {
847 public void DefineUninitializedDataInvalidArgs () {
848 TypeBuilder tb = module.DefineType (genTypeName ());
851 tb.DefineUninitializedData (null, 1, 0);
854 catch (ArgumentNullException) {
858 tb.DefineUninitializedData ("", 1, 0);
861 catch (ArgumentException) {
864 // The size of the data is less than or equal to zero ???
866 tb.DefineUninitializedData ("BAR", 0, 0);
869 catch (ArgumentException) {
873 string name = String.Format ("{0}", (char)0);
874 tb.DefineUninitializedData (name, 1, 0);
875 Fail ("Names with embedded nulls should be rejected");
877 catch (ArgumentException) {
882 [ExpectedException (typeof (InvalidOperationException))]
883 public void DefineUninitializedDataAlreadyCreated () {
884 TypeBuilder tb = module.DefineType (genTypeName ());
887 tb.DefineUninitializedData ("BAR2", 1, 0);
891 public void DefineUninitializedData () {
892 TypeBuilder tb = module.DefineType (genTypeName ());
894 tb.DefineUninitializedData ("foo", 4, FieldAttributes.Public);
896 Type t = tb.CreateType ();
898 object o = Activator.CreateInstance (t);
900 FieldInfo fi = t.GetField ("foo");
902 object fieldVal = fi.GetValue (o);
904 IntPtr ptr = Marshal.AllocHGlobal (4);
905 Marshal.StructureToPtr (fieldVal, ptr, true);
906 Marshal.FreeHGlobal (ptr);
910 public void TestDefineMethod () {
911 TypeBuilder tb = module.DefineType (genTypeName ());
913 // Check invalid arguments
915 tb.DefineMethod (null, 0, null, null);
918 catch (ArgumentNullException) {
922 tb.DefineMethod ("", 0, null, null);
925 catch (ArgumentException) {
928 // Check non-virtual methods on an interface
929 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
931 tb2.DefineMethod ("FOO", MethodAttributes.Abstract, null, null);
934 catch (ArgumentException) {
938 // Can not be called on a created type
940 tb.DefineMethod ("bar", 0, null, null);
943 catch (InvalidOperationException) {
947 // TODO: DefineMethodOverride
950 public void TestDefineNestedType () {
951 TypeBuilder tb = module.DefineType (genTypeName ());
953 // Check invalid arguments
955 tb.DefineNestedType (null);
956 Fail ("Should reject null name");
958 catch (ArgumentNullException) {
962 tb.DefineNestedType ("");
963 Fail ("Should reject empty name");
965 catch (ArgumentException) {
969 tb.DefineNestedType (nullName ());
970 Fail ("Should reject name with embedded 0s");
972 catch (ArgumentException) {
975 // If I fix the code so this works then mcs breaks -> how can mcs
976 // works under MS .NET in the first place ???
979 tb.DefineNestedType ("AA", TypeAttributes.Public, null, null);
980 Fail ("Nested visibility must be specified.");
982 catch (ArgumentException) {
987 tb.DefineNestedType ("BB", TypeAttributes.NestedPublic, null,
989 Fail ("Should reject empty interface");
991 catch (ArgumentException) {
994 // I think this should reject non-interfaces, but it does not
995 tb.DefineNestedType ("BB", TypeAttributes.NestedPublic, null,
996 new Type[1] { typeof (object) });
999 tb.DefineNestedType ("Nest");
1003 // According to the MSDN docs, this cannnot be called after the type
1004 // is created, but it works.
1005 tb.DefineNestedType ("Nest2");
1007 // According to the MSDN docs, a Sealed class can't contain nested
1008 // types, but this is not true
1009 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.Sealed);
1010 tb2.DefineNestedType ("AA");
1012 // According to the MSDN docs, interfaces can only contain interfaces,
1013 // but this is not true
1014 TypeBuilder tb3 = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
1016 tb3.DefineNestedType ("AA");
1018 // Check shorter versions
1020 TypeBuilder nested = tb.DefineNestedType ("N1");
1022 AssertEquals (nested.Name, "N1");
1023 AssertEquals (nested.BaseType, typeof (object));
1024 AssertEquals (nested.Attributes, TypeAttributes.NestedPrivate);
1025 AssertEquals (nested.GetInterfaces ().Length, 0);
1032 public void TestDefinePInvokeMethod () {
1033 TypeBuilder tb = module.DefineType (genTypeName ());
1035 tb.DefinePInvokeMethod ("A", "B", "C", 0, 0, null, null, 0, 0);
1037 // Try invalid parameters
1039 tb.DefinePInvokeMethod (null, "B", "C", 0, 0, null, null, 0, 0);
1042 catch (ArgumentNullException) {
1046 // Try invalid attributes
1048 tb.DefinePInvokeMethod ("A2", "B", "C", MethodAttributes.Abstract, 0, null, null, 0, 0);
1050 catch (ArgumentException) {
1053 // Try an interface parent
1054 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
1057 tb2.DefinePInvokeMethod ("A", "B", "C", 0, 0, null, null, 0, 0);
1059 catch (ArgumentException) {
1064 public void TestDefineProperty () {
1065 TypeBuilder tb = module.DefineType (genTypeName ());
1067 // Check null parameter types
1069 tb.DefineProperty ("A", 0, null, new Type[1]);
1071 catch (ArgumentNullException) {
1076 [ExpectedException (typeof(NotSupportedException))]
1077 [Category("NotWorking")]
1078 public void TestIsDefinedIncomplete () {
1079 TypeBuilder tb = module.DefineType (genTypeName ());
1080 tb.IsDefined (typeof (int), true);
1084 public void TestIsDefinedComplete () {
1085 TypeBuilder tb = module.DefineType (genTypeName ());
1087 ConstructorInfo obsoleteCtor = typeof(ObsoleteAttribute).GetConstructor(
1088 new Type[] {typeof(string)});
1090 CustomAttributeBuilder caBuilder = new CustomAttributeBuilder (obsoleteCtor,
1091 new object[] { "obsolete message" }, new FieldInfo[0], new object[0]);
1093 tb.SetCustomAttribute (caBuilder);
1095 AssertEquals (true, tb.IsDefined (typeof(ObsoleteAttribute), false));
1099 [ExpectedException (typeof(NotSupportedException))]
1100 public void TestGetCustomAttributesIncomplete ()
1102 TypeBuilder tb = module.DefineType (genTypeName ());
1103 tb.GetCustomAttributes (false);
1107 public void TestGetCustomAttributesComplete ()
1109 TypeBuilder tb = module.DefineType (genTypeName ());
1111 ConstructorInfo guidCtor = typeof(GuidAttribute).GetConstructor (
1112 new Type[] { typeof(string) });
1114 CustomAttributeBuilder caBuilder = new CustomAttributeBuilder (guidCtor,
1115 new object[] { Guid.NewGuid ().ToString ("D") }, new FieldInfo[0], new object[0]);
1117 tb.SetCustomAttribute (caBuilder);
1120 AssertEquals (1, tb.GetCustomAttributes (false).Length);
1124 [ExpectedException (typeof(NotSupportedException))]
1125 public void TestGetCustomAttributesOfTypeIncomplete ()
1127 TypeBuilder tb = module.DefineType (genTypeName ());
1128 tb.GetCustomAttributes (typeof(ObsoleteAttribute), false);
1132 public void TestGetCustomAttributesOfTypeComplete ()
1134 TypeBuilder tb = module.DefineType (genTypeName ());
1136 ConstructorInfo guidCtor = typeof(GuidAttribute).GetConstructor (
1137 new Type[] { typeof(string) });
1139 CustomAttributeBuilder caBuilder = new CustomAttributeBuilder (guidCtor,
1140 new object[] { Guid.NewGuid ().ToString ("D") }, new FieldInfo[0], new object[0]);
1142 tb.SetCustomAttribute (caBuilder);
1145 AssertEquals (1, tb.GetCustomAttributes (typeof(GuidAttribute), false).Length);
1146 AssertEquals (0, tb.GetCustomAttributes (typeof(ObsoleteAttribute), false).Length);
1150 [ExpectedException (typeof(ArgumentNullException))]
1151 public void TestGetCustomAttributesOfNullTypeComplete ()
1153 TypeBuilder tb = module.DefineType (genTypeName ());
1155 tb.GetCustomAttributes (null, false);
1159 [ExpectedException (typeof(NotSupportedException))]
1160 [Ignore("mcs depends on this")]
1161 public void TestGetEventsIncomplete () {
1162 TypeBuilder tb = module.DefineType (genTypeName ());
1167 public void TestGetEventsComplete () {
1168 TypeBuilder tb = module.DefineType (genTypeName ());
1170 MethodBuilder onclickMethod = tb.DefineMethod ("OnChange", MethodAttributes.Public,
1171 typeof(void), new Type[] { typeof(Object) });
1172 onclickMethod.GetILGenerator ().Emit (OpCodes.Ret);
1174 // create public event
1175 EventBuilder eventbuilder = tb.DefineEvent ("Change", EventAttributes.None,
1176 typeof(ResolveEventHandler));
1177 eventbuilder.SetRaiseMethod (onclickMethod);
1179 Type emittedType = tb.CreateType ();
1181 AssertEquals (1, tb.GetEvents ().Length);
1182 AssertEquals (tb.GetEvents ().Length, emittedType.GetEvents ().Length);
1187 [ExpectedException (typeof(NotSupportedException))]
1188 [Ignore("mcs depends on this")]
1189 public void TestGetEventsFlagsIncomplete () {
1190 TypeBuilder tb = module.DefineType (genTypeName ());
1191 tb.GetEvents (BindingFlags.Public);
1195 public void TestGetEventsFlagsComplete () {
1196 TypeBuilder tb = module.DefineType (genTypeName ());
1198 MethodBuilder onchangeMethod = tb.DefineMethod ("OnChange", MethodAttributes.Public,
1199 typeof(void), new Type[] { typeof(Object) });
1200 onchangeMethod.GetILGenerator ().Emit (OpCodes.Ret);
1202 // create public event
1203 EventBuilder changeEvent = tb.DefineEvent ("Change", EventAttributes.None,
1204 typeof(ResolveEventHandler));
1205 changeEvent.SetRaiseMethod (onchangeMethod);
1207 // create non-public event
1208 EventBuilder redoChangeEvent = tb.DefineEvent ("RedoChange", EventAttributes.None,
1209 typeof(ResolveEventHandler));
1211 Type emittedType = tb.CreateType ();
1213 AssertEquals (1, tb.GetEvents (BindingFlags.Instance | BindingFlags.Public).Length);
1214 AssertEquals (1, tb.GetEvents (BindingFlags.Instance | BindingFlags.NonPublic).Length);
1215 AssertEquals (2, tb.GetEvents (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Length);
1216 AssertEquals (tb.GetEvents (BindingFlags.Instance | BindingFlags.Public).Length,
1217 emittedType.GetEvents (BindingFlags.Instance | BindingFlags.Public).Length);
1218 AssertEquals (tb.GetEvents (BindingFlags.Instance | BindingFlags.NonPublic).Length,
1219 emittedType.GetEvents (BindingFlags.Instance | BindingFlags.NonPublic).Length);
1220 AssertEquals (tb.GetEvents (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Length,
1221 emittedType.GetEvents (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Length);
1225 [ExpectedException (typeof(NotSupportedException))]
1226 [Ignore("mcs depends on this")]
1227 public void TestGetEventIncomplete () {
1228 TypeBuilder tb = module.DefineType (genTypeName ());
1229 tb.GetEvent ("FOO");
1233 public void TestGetEventComplete () {
1234 TypeBuilder tb = module.DefineType (genTypeName ());
1236 MethodBuilder onclickMethod = tb.DefineMethod ("OnChange", MethodAttributes.Public,
1237 typeof(void), new Type[] { typeof(Object) });
1238 onclickMethod.GetILGenerator ().Emit (OpCodes.Ret);
1240 EventBuilder eventbuilder = tb.DefineEvent ("Change", EventAttributes.None,
1241 typeof(ResolveEventHandler));
1242 eventbuilder.SetRaiseMethod (onclickMethod);
1244 Type emittedType = tb.CreateType ();
1246 AssertNotNull (tb.GetEvent ("Change"));
1247 AssertEquals (tb.GetEvent ("Change"), emittedType.GetEvent ("Change"));
1248 AssertNull (tb.GetEvent ("NotChange"));
1249 AssertEquals (tb.GetEvent ("NotChange"), emittedType.GetEvent ("NotChange"));
1253 [ExpectedException (typeof(NotSupportedException))]
1254 [Ignore("mcs depends on this")]
1255 public void TestGetEventFlagsIncomplete () {
1256 TypeBuilder tb = module.DefineType (genTypeName ());
1257 tb.GetEvent ("FOO", BindingFlags.Public);
1261 public void TestGetEventFlagsComplete () {
1262 TypeBuilder tb = module.DefineType (genTypeName ());
1264 MethodBuilder onclickMethod = tb.DefineMethod ("OnChange", MethodAttributes.Public,
1265 typeof(void), new Type[] { typeof(Object) });
1266 onclickMethod.GetILGenerator ().Emit (OpCodes.Ret);
1268 EventBuilder eventbuilder = tb.DefineEvent ("Change", EventAttributes.None,
1269 typeof(ResolveEventHandler));
1270 eventbuilder.SetRaiseMethod (onclickMethod);
1272 Type emittedType = tb.CreateType ();
1274 AssertNotNull (tb.GetEvent ("Change", BindingFlags.Instance | BindingFlags.Public));
1275 AssertEquals (tb.GetEvent ("Change", BindingFlags.Instance | BindingFlags.Public),
1276 emittedType.GetEvent ("Change", BindingFlags.Instance | BindingFlags.Public));
1277 AssertNull (tb.GetEvent ("Change", BindingFlags.Instance | BindingFlags.NonPublic));
1278 AssertEquals (tb.GetEvent ("Change", BindingFlags.Instance | BindingFlags.NonPublic),
1279 emittedType.GetEvent ("Change", BindingFlags.Instance | BindingFlags.NonPublic));
1283 [ExpectedException (typeof(NotSupportedException))]
1284 [Ignore("mcs depends on this")]
1285 public void TestGetFieldsIncomplete () {
1286 TypeBuilder tb = module.DefineType (genTypeName ());
1291 public void TestGetFieldsComplete () {
1292 TypeBuilder tb = module.DefineType (genTypeName ());
1293 tb.DefineField ("TestField", typeof(int), FieldAttributes.Public);
1295 Type emittedType = tb.CreateType ();
1297 AssertEquals (1, tb.GetFields ().Length);
1298 AssertEquals (tb.GetFields ().Length, emittedType.GetFields().Length);
1302 [ExpectedException (typeof(NotSupportedException))]
1303 [Ignore("mcs depends on this")]
1304 public void TestGetFieldsFlagsIncomplete () {
1305 TypeBuilder tb = module.DefineType (genTypeName ());
1306 tb.GetFields (BindingFlags.Instance | BindingFlags.Public);
1310 public void TestGetFieldsFlagsComplete () {
1311 TypeBuilder tb = module.DefineType (genTypeName ());
1312 tb.DefineField ("TestField", typeof(int), FieldAttributes.Public);
1314 Type emittedType = tb.CreateType ();
1316 AssertEquals (1, tb.GetFields (BindingFlags.Instance | BindingFlags.Public).Length);
1317 AssertEquals (tb.GetFields (BindingFlags.Instance | BindingFlags.Public).Length,
1318 emittedType.GetFields (BindingFlags.Instance | BindingFlags.Public).Length);
1319 AssertEquals (0, tb.GetFields (BindingFlags.Instance | BindingFlags.NonPublic).Length);
1320 AssertEquals (tb.GetFields (BindingFlags.Instance | BindingFlags.NonPublic).Length,
1321 emittedType.GetFields (BindingFlags.Instance | BindingFlags.NonPublic).Length);
1325 [ExpectedException (typeof(NotSupportedException))]
1326 [Ignore("mcs depends on this")]
1327 public void TestGetFieldIncomplete () {
1328 TypeBuilder tb = module.DefineType (genTypeName ());
1329 tb.GetField ("test");
1333 public void TestGetFieldComplete () {
1334 TypeBuilder tb = module.DefineType (genTypeName ());
1335 tb.DefineField ("TestField", typeof(int), FieldAttributes.Public);
1337 Type emittedType = tb.CreateType ();
1339 AssertNotNull (tb.GetField ("TestField"));
1340 AssertEquals (tb.GetField ("TestField").Name, emittedType.GetField ("TestField").Name);
1341 //AssertNull (tb.GetField ("TestOtherField"));
1342 //AssertEquals (tb.GetField ("TestOtherField").Name,
1343 // emittedType.GetField ("TestOtherField").Name);
1347 [ExpectedException (typeof(NotSupportedException))]
1348 [Ignore("mcs depends on this")]
1349 public void TestGetFieldFlagsIncomplete () {
1350 TypeBuilder tb = module.DefineType (genTypeName ());
1351 tb.GetField ("test", BindingFlags.Public);
1355 public void TestGetFieldFlagsComplete () {
1356 TypeBuilder tb = module.DefineType (genTypeName ());
1357 tb.DefineField ("TestField", typeof(int), FieldAttributes.Public);
1359 Type emittedType = tb.CreateType ();
1361 AssertNotNull (tb.GetField ("TestField", BindingFlags.Instance | BindingFlags.Public));
1362 AssertEquals (tb.GetField ("TestField", BindingFlags.Instance | BindingFlags.Public).Name,
1363 emittedType.GetField ("TestField", BindingFlags.Instance | BindingFlags.Public).Name);
1364 AssertNull (tb.GetField ("TestField", BindingFlags.Instance | BindingFlags.NonPublic));
1365 AssertEquals (tb.GetField ("TestField", BindingFlags.Instance | BindingFlags.NonPublic),
1366 emittedType.GetField ("TestField", BindingFlags.Instance | BindingFlags.NonPublic));
1370 [ExpectedException (typeof(NotSupportedException))]
1371 [Ignore("mcs depends on this")]
1372 public void TestGetPropertiesIncomplete () {
1373 TypeBuilder tb = module.DefineType (genTypeName ());
1374 tb.GetProperties ();
1378 public void TestGetPropertiesComplete () {
1379 TypeBuilder tb = module.DefineType (genTypeName ());
1380 DefineStringProperty (tb, "CustomerName", "customerName", MethodAttributes.Public);
1382 Type emittedType = tb.CreateType ();
1384 AssertEquals (1, tb.GetProperties ().Length);
1385 AssertEquals (tb.GetProperties ().Length, emittedType.GetProperties ().Length);
1389 [ExpectedException (typeof(NotSupportedException))]
1390 [Ignore("mcs depends on this")]
1391 public void TestGetPropertiesFlagsIncomplete () {
1392 TypeBuilder tb = module.DefineType (genTypeName ());
1393 tb.GetProperties (BindingFlags.Public);
1397 public void TestGetPropertiesFlagsComplete () {
1398 TypeBuilder tb = module.DefineType (genTypeName ());
1399 DefineStringProperty (tb, "CustomerName", "customerName", MethodAttributes.Public);
1401 Type emittedType = tb.CreateType ();
1403 AssertEquals (1, tb.GetProperties (BindingFlags.Instance | BindingFlags.Public).Length);
1404 AssertEquals (tb.GetProperties (BindingFlags.Instance | BindingFlags.Public).Length,
1405 emittedType.GetProperties (BindingFlags.Instance | BindingFlags.Public).Length);
1406 AssertEquals (0, tb.GetProperties (BindingFlags.Instance | BindingFlags.NonPublic).Length);
1407 AssertEquals (tb.GetProperties (BindingFlags.Instance | BindingFlags.NonPublic).Length,
1408 emittedType.GetProperties (BindingFlags.Instance | BindingFlags.NonPublic).Length);
1412 [ExpectedException (typeof(NotSupportedException))]
1413 public void TestGetPropertyIncomplete () {
1414 TypeBuilder tb = module.DefineType (genTypeName ());
1415 tb.GetProperty ("test");
1419 public void TestGetPropertyComplete () {
1420 TypeBuilder tb = module.DefineType (genTypeName ());
1421 DefineStringProperty (tb, "CustomerName", "customerName", MethodAttributes.Public);
1423 Type emittedType = tb.CreateType ();
1425 AssertNotNull (emittedType.GetProperty ("CustomerName"));
1426 AssertNull (emittedType.GetProperty ("OtherCustomerName"));
1429 tb.GetProperty ("CustomerName");
1431 } catch (NotSupportedException) {}
1435 [ExpectedException (typeof(NotSupportedException))]
1436 public void TestGetPropertyFlagsIncomplete () {
1437 TypeBuilder tb = module.DefineType (genTypeName ());
1438 tb.GetProperty ("test", BindingFlags.Public);
1442 public void TestGetPropertyFlagsComplete () {
1443 TypeBuilder tb = module.DefineType (genTypeName ());
1444 DefineStringProperty (tb, "CustomerName", "customerName", MethodAttributes.Public);
1446 Type emittedType = tb.CreateType ();
1448 AssertNotNull (emittedType.GetProperty ("CustomerName", BindingFlags.Instance |
1449 BindingFlags.Public));
1450 AssertNull (emittedType.GetProperty ("CustomerName", BindingFlags.Instance |
1451 BindingFlags.NonPublic));
1454 tb.GetProperty ("CustomerName", BindingFlags.Instance | BindingFlags.Public);
1457 catch (NotSupportedException) { }
1461 [ExpectedException (typeof(NotSupportedException))]
1462 [Ignore("mcs depends on this")]
1463 public void TestGetMethodsIncomplete () {
1464 TypeBuilder tb = module.DefineType (genTypeName ());
1469 [Category("NotWorking")]
1470 public void TestGetMethodsComplete () {
1471 TypeBuilder tb = module.DefineType (genTypeName ());
1472 MethodBuilder helloMethod = tb.DefineMethod ("HelloMethod",
1473 MethodAttributes.Public, typeof(string), new Type[0]);
1474 ILGenerator helloMethodIL = helloMethod.GetILGenerator ();
1475 helloMethodIL.Emit (OpCodes.Ldstr, "Hi! ");
1476 helloMethodIL.Emit (OpCodes.Ldarg_1);
1477 MethodInfo infoMethod = typeof(string).GetMethod ("Concat",
1478 new Type[] { typeof(string), typeof(string) });
1479 helloMethodIL.Emit (OpCodes.Call, infoMethod);
1480 helloMethodIL.Emit (OpCodes.Ret);
1482 Type emittedType = tb.CreateType ();
1484 AssertEquals (typeof(object).GetMethods (BindingFlags.Public | BindingFlags.Instance).Length + 1,
1485 tb.GetMethods ().Length);
1486 AssertEquals (tb.GetMethods ().Length, emittedType.GetMethods ().Length);
1490 [ExpectedException (typeof(NotSupportedException))]
1491 [Ignore("mcs depends on this")]
1492 public void TestGetMethodsFlagsIncomplete () {
1493 TypeBuilder tb = module.DefineType (genTypeName ());
1494 tb.GetMethods (BindingFlags.Public);
1498 public void TestGetMethodsFlagsComplete () {
1499 TypeBuilder tb = module.DefineType (genTypeName ());
1500 MethodBuilder helloMethod = tb.DefineMethod ("HelloMethod",
1501 MethodAttributes.Public, typeof(string), new Type[0]);
1502 ILGenerator helloMethodIL = helloMethod.GetILGenerator ();
1503 helloMethodIL.Emit (OpCodes.Ldstr, "Hi! ");
1504 helloMethodIL.Emit (OpCodes.Ldarg_1);
1505 MethodInfo infoMethod = typeof(string).GetMethod ("Concat",
1506 new Type[] { typeof(string), typeof(string) });
1507 helloMethodIL.Emit (OpCodes.Call, infoMethod);
1508 helloMethodIL.Emit (OpCodes.Ret);
1510 Type emittedType = tb.CreateType ();
1512 AssertEquals (1, tb.GetMethods (BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly).Length);
1513 AssertEquals (tb.GetMethods (BindingFlags.Instance | BindingFlags.Public).Length,
1514 emittedType.GetMethods (BindingFlags.Instance | BindingFlags.Public).Length);
1515 AssertEquals (0, tb.GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly).Length);
1516 AssertEquals (tb.GetMethods (BindingFlags.Instance | BindingFlags.NonPublic).Length,
1517 emittedType.GetMethods (BindingFlags.Instance | BindingFlags.NonPublic).Length);
1521 [ExpectedException (typeof(NotSupportedException))]
1522 public void TestGetMemberIncomplete () {
1523 TypeBuilder tb = module.DefineType (genTypeName ());
1524 tb.GetMember ("FOO", MemberTypes.All, BindingFlags.Public);
1528 public void TestGetMemberComplete () {
1529 TypeBuilder tb = module.DefineType (genTypeName ());
1530 tb.DefineField ("FOO", typeof(int), FieldAttributes.Private);
1532 Type emittedType = tb.CreateType ();
1534 AssertEquals (1, tb.GetMember ("FOO", MemberTypes.Field, BindingFlags.Instance | BindingFlags.NonPublic).Length);
1535 AssertEquals (0, tb.GetMember ("FOO", MemberTypes.Field, BindingFlags.Instance | BindingFlags.Public).Length);
1539 [ExpectedException (typeof(NotSupportedException))]
1540 public void TestGetMembersIncomplete () {
1541 TypeBuilder tb = module.DefineType (genTypeName ());
1546 public void TestGetMembersComplete () {
1547 TypeBuilder tb = module.DefineType (genTypeName ());
1548 Type emittedType = tb.CreateType ();
1550 AssertEquals (tb.GetMembers ().Length, emittedType.GetMembers ().Length);
1554 [ExpectedException (typeof(NotSupportedException))]
1555 public void TestGetMembersFlagsIncomplete () {
1556 TypeBuilder tb = module.DefineType (genTypeName ());
1557 tb.GetMembers (BindingFlags.Public);
1561 public void TestGetMembersFlagsComplete () {
1562 TypeBuilder tb = module.DefineType (genTypeName ());
1563 tb.DefineField ("FOO", typeof(int), FieldAttributes.Public);
1565 Type emittedType = tb.CreateType ();
1567 Assert (tb.GetMembers (BindingFlags.Instance | BindingFlags.Public).Length != 0);
1568 AssertEquals (tb.GetMembers (BindingFlags.Instance | BindingFlags.Public).Length,
1569 emittedType.GetMembers (BindingFlags.Instance | BindingFlags.Public).Length);
1570 AssertEquals (tb.GetMembers (BindingFlags.Instance | BindingFlags.NonPublic).Length,
1571 emittedType.GetMembers (BindingFlags.Instance | BindingFlags.NonPublic).Length);
1575 [ExpectedException (typeof(NotSupportedException))]
1576 public void TestGetInterfaceIncomplete () {
1577 TypeBuilder tb = module.DefineType (genTypeName ());
1578 tb.GetInterface ("FOO", true);
1582 public void TestGetInterfaces () {
1583 TypeBuilder tb = module.DefineType (genTypeName ());
1584 Type[] interfaces = tb.GetInterfaces ();
1585 AssertEquals (0, interfaces.Length);
1587 TypeBuilder tbInterface = module.DefineType (genTypeName (), TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract);
1588 Type emittedInterface = tbInterface.CreateType ();
1590 tb = module.DefineType (genTypeName (), TypeAttributes.Public, typeof(object), new Type[] { emittedInterface });
1591 interfaces = tb.GetInterfaces ();
1592 AssertEquals (1, interfaces.Length);
1596 [ExpectedException (typeof (InvalidOperationException))]
1597 public void TestAddDeclarativeSecurityAlreadyCreated () {
1598 TypeBuilder tb = module.DefineType (genTypeName ());
1601 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
1602 tb.AddDeclarativeSecurity (SecurityAction.Demand, set);
1606 [ExpectedException (typeof (ArgumentNullException))]
1607 public void TestAddDeclarativeSecurityNullPermissionSet () {
1608 TypeBuilder tb = module.DefineType (genTypeName ());
1610 tb.AddDeclarativeSecurity (SecurityAction.Demand, null);
1614 public void TestAddDeclarativeSecurityInvalidAction () {
1615 TypeBuilder tb = module.DefineType (genTypeName ());
1617 SecurityAction[] actions = new SecurityAction [] {
1618 SecurityAction.RequestMinimum,
1619 SecurityAction.RequestOptional,
1620 SecurityAction.RequestRefuse };
1621 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
1623 foreach (SecurityAction action in actions) {
1625 tb.AddDeclarativeSecurity (action, set);
1628 catch (ArgumentException) {
1634 [ExpectedException (typeof (InvalidOperationException))]
1635 public void TestAddDeclarativeSecurityDuplicateAction () {
1636 TypeBuilder tb = module.DefineType (genTypeName ());
1638 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
1639 tb.AddDeclarativeSecurity (SecurityAction.Demand, set);
1640 tb.AddDeclarativeSecurity (SecurityAction.Demand, set);
1644 public void TestEnums () {
1645 TypeAttributes typeAttrs = TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Sealed;
1646 TypeBuilder enumToCreate = module.DefineType(genTypeName (), typeAttrs,
1648 enumToCreate.SetCustomAttribute (new CustomAttributeBuilder (typeof (FlagsAttribute).GetConstructors ()[0], new Type [0]));
1649 // add value__ field, see DefineEnum method of ModuleBuilder
1650 enumToCreate.DefineField("value__", typeof(Int32),
1651 FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RTSpecialName);
1654 FieldBuilder fb = enumToCreate.DefineField("A", enumToCreate,
1655 FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal);
1656 fb.SetConstant((Int32) 0);
1658 fb = enumToCreate.DefineField("B", enumToCreate,
1659 FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal);
1660 fb.SetConstant((Int32) 1);
1662 fb = enumToCreate.DefineField("C", enumToCreate,
1663 FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal);
1664 fb.SetConstant((Int32) 2);
1666 Type enumType = enumToCreate.CreateType();
1668 object enumVal = Enum.ToObject(enumType, (Int32) 3);
1670 AssertEquals ("B, C", enumVal.ToString ());
1671 AssertEquals (3, (Int32)enumVal);
1675 public void DefineEnum () {
1676 TypeBuilder typeBuilder = module.DefineType (genTypeName (),
1677 TypeAttributes.Public);
1678 EnumBuilder enumBuilder = module.DefineEnum (genTypeName (),
1679 TypeAttributes.Public, typeof(int));
1680 typeBuilder.DefineField ("myField", enumBuilder, FieldAttributes.Private);
1681 enumBuilder.CreateType();
1682 typeBuilder.CreateType();
1686 [ExpectedException(typeof(TypeLoadException))]
1687 [Category("NotWorking")]
1688 public void DefineEnumThrowIfTypeBuilderCalledBeforeEnumBuilder () {
1689 TypeBuilder typeBuilder = module.DefineType (genTypeName (),
1690 TypeAttributes.Public);
1691 EnumBuilder enumBuilder = module.DefineEnum (genTypeName (),
1692 TypeAttributes.Public, typeof(int));
1693 typeBuilder.DefineField ("myField", enumBuilder, FieldAttributes.Private);
1694 typeBuilder.CreateType();
1695 enumBuilder.CreateType();
1698 private void DefineStringProperty (TypeBuilder tb, string propertyName, string fieldName, MethodAttributes methodAttribs) {
1699 // define the field holding the property value
1700 FieldBuilder fieldBuilder = tb.DefineField (fieldName,
1701 typeof(string), FieldAttributes.Private);
1703 PropertyBuilder propertyBuilder = tb.DefineProperty (
1704 propertyName, PropertyAttributes.HasDefault, typeof(string),
1705 new Type[] { typeof(string) });
1707 // First, we'll define the behavior of the "get" property for CustomerName as a method.
1708 MethodBuilder getMethodBuilder = tb.DefineMethod ("Get" + propertyName,
1713 ILGenerator getIL = getMethodBuilder.GetILGenerator ();
1715 getIL.Emit (OpCodes.Ldarg_0);
1716 getIL.Emit (OpCodes.Ldfld, fieldBuilder);
1717 getIL.Emit (OpCodes.Ret);
1719 // Now, we'll define the behavior of the "set" property for CustomerName.
1720 MethodBuilder setMethodBuilder = tb.DefineMethod ("Set" + propertyName,
1723 new Type[] { typeof(string) });
1725 ILGenerator setIL = setMethodBuilder.GetILGenerator ();
1727 setIL.Emit (OpCodes.Ldarg_0);
1728 setIL.Emit (OpCodes.Ldarg_1);
1729 setIL.Emit (OpCodes.Stfld, fieldBuilder);
1730 setIL.Emit (OpCodes.Ret);
1732 // Last, we must map the two methods created above to our PropertyBuilder to
1733 // their corresponding behaviors, "get" and "set" respectively.
1734 propertyBuilder.SetGetMethod (getMethodBuilder);
1735 propertyBuilder.SetSetMethod (setMethodBuilder);
1738 static int handler_called = 0;
1741 public void TestTypeResolve () {
1742 string typeName = genTypeName ();
1744 ResolveEventHandler handler = new ResolveEventHandler (TypeResolve);
1745 AppDomain.CurrentDomain.TypeResolve += handler;
1747 Type t = Type.GetType (typeName);
1748 AssertEquals (typeName, t.Name);
1749 AssertEquals (1, handler_called);
1750 AppDomain.CurrentDomain.TypeResolve -= handler;
1753 Assembly TypeResolve (object sender, ResolveEventArgs args) {
1754 TypeBuilder tb = module.DefineType (args.Name, TypeAttributes.Public);
1761 public void TestIsAssignableTo () {
1762 Type icomparable = typeof (IComparable);
1764 TypeBuilder tb = module.DefineType (genTypeName (),
1765 TypeAttributes.Public, null, new Type[] { icomparable, typeof (Bar) });
1767 Assert ("01", icomparable.IsAssignableFrom (tb));
1768 Assert ("02", !tb.IsAssignableFrom (icomparable));
1770 Assert ("03", typeof (Bar).IsAssignableFrom (tb));
1771 Assert ("04", !typeof (Baz).IsAssignableFrom (tb));
1773 Assert ("05", tb.IsAssignableFrom (tb));
1775 Assert ("06", !tb.IsAssignableFrom (typeof (IDisposable)));
1776 tb.AddInterfaceImplementation (typeof (IDisposable));
1780 [Category("NotDotNet")]
1781 public void TestIsAssignableTo_NotDotNet () {
1782 Type icomparable = typeof (IComparable);
1784 TypeBuilder tb = module.DefineType (genTypeName (),
1785 TypeAttributes.Public, null, new Type[] { icomparable, typeof (Bar) });
1787 Assert ("01", typeof (Foo).IsAssignableFrom (tb));
1789 tb.AddInterfaceImplementation (typeof (IDisposable));
1791 Assert ("02", tb.IsAssignableFrom (typeof (IDisposable)));
1794 // Assert ("07", typeof (Bar[]).IsAssignableFrom (module.GetType (tb.FullName + "[]")));