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
;
21 using System
.Security
;
22 using System
.Security
.Permissions
;
23 using System
.Runtime
.InteropServices
;
24 using NUnit
.Framework
;
26 namespace MonoTests
.System
.Reflection
.Emit
30 public class TypeBuilderTest
: Assertion
32 private interface AnInterface
{
35 private AssemblyBuilder assembly
;
37 private ModuleBuilder module
;
39 static string ASSEMBLY_NAME
= "MonoTests.System.Reflection.Emit.TypeBuilderTest";
42 protected void SetUp () {
43 AssemblyName assemblyName
= new AssemblyName();
44 assemblyName
.Name
= ASSEMBLY_NAME
;
47 Thread
.GetDomain().DefineDynamicAssembly(
48 assemblyName
, AssemblyBuilderAccess
.RunAndSave
, "c:\\");
50 module
= assembly
.DefineDynamicModule("module1");
53 static int typeIndexer
= 0;
55 // Return a unique type name
56 private string genTypeName () {
57 return "t" + (typeIndexer
++);
60 private string nullName () {
61 return String
.Format ("{0}", (char)0);
65 public void TestAssembly () {
66 TypeBuilder tb
= module
.DefineType (genTypeName (), TypeAttributes
.Public
);
67 AssertEquals ("Assembly works",
68 tb
.Assembly
, assembly
);
72 public void TestAssemblyQualifiedName () {
73 TypeBuilder tb
= module
.DefineType ("A.B.C.D", TypeAttributes
.Public
);
75 AssertEquals ("AssemblyQualifiedName works",
76 tb
.AssemblyQualifiedName
, "A.B.C.D, " + assembly
.GetName ().FullName
);
80 public void TestAttributes () {
81 TypeAttributes attrs
= TypeAttributes
.Public
| TypeAttributes
.BeforeFieldInit
;
82 TypeBuilder tb
= module
.DefineType (genTypeName (), attrs
);
84 AssertEquals ("Attributes works",
85 tb
.Attributes
, attrs
);
89 public void TestBaseTypeClass () {
90 TypeAttributes attrs
= TypeAttributes
.Public
;
91 TypeBuilder tb
= module
.DefineType (genTypeName (), attrs
);
92 AssertEquals ("BaseType defaults to Object",
93 tb
.BaseType
, typeof (object));
95 TypeBuilder tb2
= module
.DefineType (genTypeName (), attrs
, tb
);
96 AssertEquals ("BaseType works",
101 public void TestBaseTypeInterface ()
103 TypeBuilder tb3
= module
.DefineType (genTypeName (), TypeAttributes
.Interface
| TypeAttributes
.Abstract
);
104 AssertEquals ("Interfaces should default to no base type", null, tb3
.BaseType
);
108 public void TestDeclaringType () {
109 TypeAttributes attrs
= 0;
110 TypeBuilder tb
= module
.DefineType (genTypeName (), attrs
);
112 AssertEquals ("Has no declaring type",
113 null, tb
.DeclaringType
);
115 attrs
= TypeAttributes
.NestedPublic
;
116 TypeBuilder tb2
= tb
.DefineNestedType (genTypeName (), attrs
);
117 TypeBuilder tb3
= tb2
.DefineNestedType (genTypeName (), attrs
);
118 AssertEquals ("DeclaringType works",
119 tb
, tb3
.DeclaringType
.DeclaringType
);
123 public void TestFullName () {
124 string name
= genTypeName ();
125 TypeAttributes attrs
= 0;
126 TypeBuilder tb
= module
.DefineType (name
, attrs
);
127 AssertEquals ("FullName works",
130 string name2
= genTypeName ();
131 attrs
= TypeAttributes
.NestedPublic
;
132 TypeBuilder tb2
= tb
.DefineNestedType (name2
, attrs
);
134 string name3
= genTypeName ();
135 attrs
= TypeAttributes
.NestedPublic
;
136 TypeBuilder tb3
= tb2
.DefineNestedType (name3
, attrs
);
138 AssertEquals ("FullName works on nested types",
139 name
+ "+" + name2
+ "+" + name3
, tb3
.FullName
);
143 [ExpectedException (typeof(NotSupportedException
))]
144 public void TestGUIDIncomplete () {
145 TypeBuilder tb
= module
.DefineType (genTypeName ());
150 public void TestGUIDComplete ()
152 TypeBuilder tb
= module
.DefineType (genTypeName ());
154 Assert(tb
.GUID
!= Guid
.Empty
);
158 public void TestFixedGUIDComplete ()
160 TypeBuilder tb
= module
.DefineType (genTypeName ());
162 Guid guid
= Guid
.NewGuid ();
164 ConstructorInfo guidCtor
= typeof(GuidAttribute
).GetConstructor(
165 new Type
[] {typeof(string)}
);
167 CustomAttributeBuilder caBuilder
= new CustomAttributeBuilder (guidCtor
,
168 new object[] { guid.ToString("D") }
, new FieldInfo
[0], new object[0]);
170 tb
.SetCustomAttribute (caBuilder
);
172 AssertEquals (guid
, tb
.GUID
);
176 [ExpectedException (typeof(NotSupportedException
))]
177 public void TestHasElementType () {
178 // According to the MSDN docs, this member works, but in reality, it
179 // returns a NotSupportedException
180 TypeBuilder tb
= module
.DefineType (genTypeName ());
181 bool b
= tb
.HasElementType
;
185 public void TestIsAbstract () {
186 TypeBuilder tb
= module
.DefineType (genTypeName ());
188 false, tb
.IsAbstract
);
190 TypeBuilder tb2
= module
.DefineType (genTypeName (), TypeAttributes
.Abstract
);
192 true, tb2
.IsAbstract
);
196 public void TestIsAnsiClass () {
197 TypeBuilder tb
= module
.DefineType (genTypeName ());
199 true, tb
.IsAnsiClass
);
201 TypeBuilder tb2
= module
.DefineType (genTypeName (), TypeAttributes
.UnicodeClass
);
203 false, tb2
.IsAnsiClass
);
207 public void TestIsArray () {
208 // How can a TypeBuilder be an array ?
209 string name
= genTypeName ();
210 TypeBuilder tb
= module
.DefineType (name
);
211 AssertEquals ("IsArray works",
216 public void TestIsAutoClass () {
217 TypeBuilder tb
= module
.DefineType (genTypeName ());
219 false, tb
.IsAutoClass
);
221 TypeBuilder tb2
= module
.DefineType (genTypeName (), TypeAttributes
.AutoClass
);
223 true, tb2
.IsAutoClass
);
227 public void TestIsAutoLayout () {
228 TypeBuilder tb
= module
.DefineType (genTypeName ());
229 AssertEquals ("AutoLayout defaults to true",
230 true, tb
.IsAutoLayout
);
232 TypeBuilder tb2
= module
.DefineType (genTypeName (), TypeAttributes
.ExplicitLayout
);
234 false, tb2
.IsAutoLayout
);
238 public void TestIsByRef () {
239 // How can a TypeBuilder be ByRef ?
240 TypeBuilder tb
= module
.DefineType (genTypeName ());
241 AssertEquals ("IsByRef works",
246 public void TestIsClass () {
247 TypeBuilder tb
= module
.DefineType (genTypeName ());
248 AssertEquals ("Most types are classes",
251 TypeBuilder tb2
= module
.DefineType (genTypeName (), TypeAttributes
.Interface
| TypeAttributes
.Abstract
);
252 AssertEquals ("Interfaces are not classes",
255 TypeBuilder tb3
= module
.DefineType (genTypeName (), 0, typeof (ValueType
));
256 AssertEquals ("value types are not classes",
259 TypeBuilder tb4
= module
.DefineType (genTypeName (), 0, typeof (Enum
));
260 AssertEquals ("enums are not classes",
265 public void TestIsCOMObject () {
266 TypeBuilder tb
= module
.DefineType (genTypeName ());
267 AssertEquals ("Probably not", false, tb
.IsCOMObject
);
269 tb
= module
.DefineType (genTypeName (), TypeAttributes
.Import
);
270 AssertEquals ("type with Import attribute is COM object",
271 true, tb
.IsCOMObject
);
275 public void TestIsContextful () {
276 TypeBuilder tb
= module
.DefineType (genTypeName ());
277 AssertEquals (false, tb
.IsContextful
);
279 TypeBuilder tb2
= module
.DefineType (genTypeName (), 0, typeof (ContextBoundObject
));
280 AssertEquals (true, tb2
.IsContextful
);
284 public void TestIsEnum () {
285 TypeBuilder tb
= module
.DefineType (genTypeName ());
286 AssertEquals (false, tb
.IsEnum
);
288 // This returns true under both mono and MS .NET ???
289 TypeBuilder tb2
= module
.DefineType (genTypeName (), 0, typeof (ValueType
));
290 AssertEquals ("value types are not necessary enums",
293 TypeBuilder tb3
= module
.DefineType (genTypeName (), 0, typeof (Enum
));
294 AssertEquals ("enums are enums", true, tb3
.IsEnum
);
298 public void TestIsExplicitLayout () {
299 TypeBuilder tb
= module
.DefineType (genTypeName ());
300 AssertEquals ("ExplicitLayout defaults to false",
301 false, tb
.IsExplicitLayout
);
303 TypeBuilder tb2
= module
.DefineType (genTypeName (), TypeAttributes
.ExplicitLayout
);
304 AssertEquals (true, tb2
.IsExplicitLayout
);
308 public void TestIsImport () {
309 // How can this be true ?
310 TypeBuilder tb
= module
.DefineType (genTypeName ());
311 AssertEquals (false, tb
.IsImport
);
315 public void TestIsInterface () {
316 TypeBuilder tb
= module
.DefineType (genTypeName ());
317 AssertEquals ("Most types are not interfaces",
318 false, tb
.IsInterface
);
320 TypeBuilder tb2
= module
.DefineType (genTypeName (), TypeAttributes
.Interface
| TypeAttributes
.Abstract
);
321 AssertEquals ("Interfaces are interfaces",
322 true, tb2
.IsInterface
);
324 TypeBuilder tb3
= module
.DefineType (genTypeName (), 0, typeof (ValueType
));
325 AssertEquals ("value types are not interfaces",
326 false, tb3
.IsInterface
);
330 public void TestIsLayoutSequential () {
331 TypeBuilder tb
= module
.DefineType (genTypeName ());
332 AssertEquals ("SequentialLayout defaults to false",
333 false, tb
.IsLayoutSequential
);
335 TypeBuilder tb2
= module
.DefineType (genTypeName (), TypeAttributes
.SequentialLayout
);
336 AssertEquals (true, tb2
.IsLayoutSequential
);
340 public void TestIsMarshalByRef () {
341 TypeBuilder tb
= module
.DefineType (genTypeName ());
342 AssertEquals (false, tb
.IsMarshalByRef
);
344 TypeBuilder tb2
= module
.DefineType (genTypeName (), 0, typeof (MarshalByRefObject
));
345 AssertEquals (true, tb2
.IsMarshalByRef
);
347 TypeBuilder tb3
= module
.DefineType (genTypeName (), 0, typeof (ContextBoundObject
));
348 AssertEquals (true, tb3
.IsMarshalByRef
);
351 // TODO: Visibility properties
354 public void TestIsPointer () {
355 // How can this be true?
356 TypeBuilder tb
= module
.DefineType (genTypeName ());
357 AssertEquals (false, tb
.IsPointer
);
361 public void TestIsPrimitive () {
362 TypeBuilder tb
= module
.DefineType ("int");
363 AssertEquals (false, tb
.IsPrimitive
);
367 public void IsSealed () {
368 TypeBuilder tb
= module
.DefineType (genTypeName ());
369 AssertEquals ("Sealed defaults to false",
372 TypeBuilder tb2
= module
.DefineType (genTypeName (), TypeAttributes
.Sealed
);
373 AssertEquals ("IsSealed works", true, tb2
.IsSealed
);
377 public void IsSerializable () {
378 TypeBuilder tb
= module
.DefineType (genTypeName ());
379 AssertEquals (false, tb
.IsSerializable
);
381 ConstructorInfo
[] ctors
= typeof (SerializableAttribute
).GetConstructors (BindingFlags
.Instance
| BindingFlags
.Public
);
382 Assert ("SerializableAttribute should have more than 0 public instance ctors",
385 tb
.SetCustomAttribute (new CustomAttributeBuilder (ctors
[0], new object[0]));
386 Type createdType
= tb
.CreateType ();
388 assembly
.Save ("TestAssembly.dll");
389 AssertEquals (true, createdType
.IsSerializable
);
393 public void TestIsSpecialName () {
394 TypeBuilder tb
= module
.DefineType (genTypeName ());
395 AssertEquals ("SpecialName defaults to false",
396 false, tb
.IsSpecialName
);
398 TypeBuilder tb2
= module
.DefineType (genTypeName (), TypeAttributes
.SpecialName
);
399 AssertEquals ("IsSpecialName works",
400 true, tb2
.IsSpecialName
);
404 public void TestIsUnicodeClass () {
405 TypeBuilder tb
= module
.DefineType (genTypeName ());
406 AssertEquals (false, tb
.IsUnicodeClass
);
408 TypeBuilder tb2
= module
.DefineType (genTypeName (), TypeAttributes
.UnicodeClass
);
409 AssertEquals (true, tb2
.IsUnicodeClass
);
413 public void TestIsValueType () {
414 TypeBuilder tb
= module
.DefineType (genTypeName ());
415 AssertEquals ("Most types are not value types",
416 false, tb
.IsValueType
);
418 TypeBuilder tb2
= module
.DefineType (genTypeName (), TypeAttributes
.Interface
| TypeAttributes
.Abstract
);
419 AssertEquals ("Interfaces are not value types",
420 false, tb2
.IsValueType
);
422 TypeBuilder tb3
= module
.DefineType (genTypeName (), 0, typeof (ValueType
));
423 AssertEquals ("value types are value types",
424 true, tb3
.IsValueType
);
426 TypeBuilder tb4
= module
.DefineType (genTypeName (), 0, typeof (Enum
));
427 AssertEquals ("enums are value types",
428 true, tb4
.IsValueType
);
432 public void TestMemberType () {
433 TypeBuilder tb
= module
.DefineType (genTypeName ());
434 AssertEquals ("A type is a type",
435 MemberTypes
.TypeInfo
, tb
.MemberType
);
439 public void TestModule () {
440 TypeBuilder tb
= module
.DefineType (genTypeName ());
441 AssertEquals ("Module works", module
, tb
.Module
);
445 public void TestName () {
446 TypeBuilder tb
= module
.DefineType ("A");
447 AssertEquals ("A", tb
.Name
);
449 TypeBuilder tb2
= module
.DefineType ("A.B.C.D.E");
450 AssertEquals ("E", tb2
.Name
);
452 TypeBuilder tb3
= tb2
.DefineNestedType ("A");
453 AssertEquals ("A", tb3
.Name
);
455 /* Is .E a valid name ?
456 TypeBuilder tb4 = module.DefineType (".E");
463 public void TestNamespace () {
464 TypeBuilder tb
= module
.DefineType ("A");
465 AssertEquals ("", tb
.Namespace
);
467 TypeBuilder tb2
= module
.DefineType ("A.B.C.D.E");
468 AssertEquals ("A.B.C.D", tb2
.Namespace
);
470 TypeBuilder tb3
= tb2
.DefineNestedType ("A");
471 AssertEquals ("", tb3
.Namespace
);
473 /* Is .E a valid name ?
474 TypeBuilder tb4 = module.DefineType (".E");
481 public void TestPackingSize () {
482 TypeBuilder tb
= module
.DefineType (genTypeName ());
483 AssertEquals (PackingSize
.Unspecified
, tb
.PackingSize
);
485 TypeBuilder tb2
= module
.DefineType (genTypeName (), 0, typeof (object),
486 PackingSize
.Size16
, 16);
487 AssertEquals (PackingSize
.Size16
, tb2
.PackingSize
);
491 public void TestReflectedType () {
492 // It is the same as DeclaringType, but why?
493 TypeBuilder tb
= module
.DefineType (genTypeName ());
494 AssertEquals (null, tb
.ReflectedType
);
496 TypeBuilder tb2
= tb
.DefineNestedType (genTypeName ());
497 AssertEquals (tb
, tb2
.ReflectedType
);
501 [ExpectedException (typeof(ArgumentNullException
))]
502 public void TestSetParentNull ()
504 TypeBuilder tb
= module
.DefineType (genTypeName ());
509 public void TestSetParentIncomplete ()
511 TypeBuilder tb
= module
.DefineType (genTypeName ());
512 tb
.SetParent (typeof(Attribute
));
513 AssertEquals (typeof(Attribute
), tb
.BaseType
);
517 [ExpectedException (typeof(InvalidOperationException
))]
518 public void TestSetParentComplete ()
520 TypeBuilder tb
= module
.DefineType (genTypeName ());
522 tb
.SetParent (typeof(Attribute
));
526 public void TestSize () {
528 TypeBuilder tb
= module
.DefineType (genTypeName ());
529 AssertEquals (0, tb
.Size
);
531 AssertEquals (0, tb
.Size
);
535 TypeBuilder tb
= module
.DefineType (genTypeName (), 0, typeof (object),
536 PackingSize
.Size16
, 32);
537 AssertEquals (32, tb
.Size
);
542 [ExpectedException (typeof(NotSupportedException
))]
543 public void TestTypeHandle () {
544 TypeBuilder tb
= module
.DefineType (genTypeName ());
545 RuntimeTypeHandle handle
= tb
.TypeHandle
;
549 [ExpectedException (typeof(NotSupportedException
))]
550 public void TestTypeInitializerIncomplete ()
552 TypeBuilder tb
= module
.DefineType (genTypeName ());
553 ConstructorInfo cb
= tb
.TypeInitializer
;
557 public void TestTypeInitializerComplete ()
559 TypeBuilder tb
= module
.DefineType (genTypeName ());
561 ConstructorInfo cb
= tb
.TypeInitializer
;
565 public void TestTypeToken () {
566 TypeBuilder tb
= module
.DefineType (genTypeName ());
567 TypeToken token
= tb
.TypeToken
;
571 public void TestUnderlyingSystemType () {
573 TypeBuilder tb
= module
.DefineType (genTypeName ());
574 AssertEquals ("For non-enums this equals itself",
575 tb
, tb
.UnderlyingSystemType
);
578 TypeBuilder tb
= module
.DefineType (genTypeName (), TypeAttributes
.Interface
| TypeAttributes
.Abstract
);
579 AssertEquals (tb
, tb
.UnderlyingSystemType
);
582 TypeBuilder tb
= module
.DefineType (genTypeName (), 0, typeof (ValueType
));
583 AssertEquals (tb
, tb
.UnderlyingSystemType
);
587 TypeBuilder tb
= module
.DefineType (genTypeName (), 0, typeof (Enum
));
589 Type t
= tb
.UnderlyingSystemType
;
592 catch (InvalidOperationException
) {
595 tb
.DefineField ("val", typeof (int), 0);
596 AssertEquals (typeof (int), tb
.UnderlyingSystemType
);
601 public void TestAddInterfaceImplementation () {
602 TypeBuilder tb
= module
.DefineType (genTypeName ());
604 tb
.AddInterfaceImplementation (null);
607 catch (ArgumentNullException
) {
610 tb
.AddInterfaceImplementation (typeof (AnInterface
));
611 tb
.AddInterfaceImplementation (typeof (AnInterface
));
613 Type t
= tb
.CreateType ();
614 AssertEquals ("Should merge identical interfaces",
615 tb
.GetInterfaces ().Length
, 1);
617 // Can not be called on a created type
619 tb
.AddInterfaceImplementation (typeof (AnInterface
));
622 catch (InvalidOperationException
) {
627 public void TestCreateType () {
628 // TODO: LOTS OF TEST SHOULD GO THERE
629 TypeBuilder tb
= module
.DefineType (genTypeName ());
632 // Can not be called on a created type
637 catch (InvalidOperationException
) {
642 public void TestDefineConstructor () {
643 TypeBuilder tb
= module
.DefineType (genTypeName ());
645 ConstructorBuilder cb
= tb
.DefineConstructor (0, 0, null);
646 cb
.GetILGenerator ().Emit (OpCodes
.Ret
);
649 // Can not be called on a created type
651 tb
.DefineConstructor (0, 0, null);
654 catch (InvalidOperationException
) {
659 public void TestDefineDefaultConstructor () {
660 TypeBuilder tb
= module
.DefineType (genTypeName ());
661 tb
.DefineDefaultConstructor (0);
664 // Can not be called on a created type, altough the MSDN docs does not mention this
666 tb
.DefineDefaultConstructor (0);
669 catch (InvalidOperationException
) {
674 [ExpectedException (typeof(NotSupportedException
))]
675 public void TestDefineDefaultConstructorParent () {
676 TypeBuilder tb
= module
.DefineType (genTypeName ());
677 tb
.DefineConstructor (MethodAttributes
.Public
,
678 CallingConventions
.Standard
,
679 new Type
[] { typeof(string) }
);
680 Type type
= tb
.CreateType ();
682 // create TypeBuilder for type that derived from the
683 // previously created type (which has no default ctor)
684 tb
= module
.DefineType (genTypeName (), TypeAttributes
.Class
685 | TypeAttributes
.Public
, type
);
687 // you cannot create a type with a default ctor that
688 // derives from a type without a default ctor
693 public void TestDefineEvent () {
694 TypeBuilder tb
= module
.DefineType (genTypeName ());
696 // Test invalid arguments
698 tb
.DefineEvent (null, 0, typeof (int));
701 catch (ArgumentNullException
) {
705 tb
.DefineEvent ("FOO", 0, null);
708 catch (ArgumentNullException
) {
712 tb
.DefineEvent ("", 0, typeof (int));
715 catch (ArgumentException
) {
719 // Can not be called on a created type
721 tb
.DefineEvent ("BAR", 0, typeof (int));
724 catch (InvalidOperationException
) {
729 public void TestDefineField () {
730 TypeBuilder tb
= module
.DefineType (genTypeName ());
732 // Check invalid arguments
734 tb
.DefineField (null, typeof (int), 0);
737 catch (ArgumentNullException
) {
741 tb
.DefineField ("", typeof (int), 0);
744 catch (ArgumentException
) {
748 // Strangely, 'A<NULL>' is accepted...
749 string name
= String
.Format ("{0}", (char)0);
750 tb
.DefineField (name
, typeof (int), 0);
751 Fail ("Names with embedded nulls should be rejected");
753 catch (ArgumentException
) {
757 tb
.DefineField ("A", typeof (void), 0);
760 catch (ArgumentException
) {
764 // Can not be called on a created type
766 tb
.DefineField ("B", typeof (int), 0);
769 catch (InvalidOperationException
) {
774 public void TestDefineInitializedData () {
775 TypeBuilder tb
= module
.DefineType (genTypeName ());
777 // Check invalid arguments
779 tb
.DefineInitializedData (null, new byte[1], 0);
782 catch (ArgumentNullException
) {
786 tb
.DefineInitializedData ("FOO", null, 0);
789 catch (ArgumentNullException
) {
793 tb
.DefineInitializedData ("", new byte[1], 0);
796 catch (ArgumentException
) {
799 // The size of the data is less than or equal to zero ???
801 tb
.DefineInitializedData ("BAR", new byte[0], 0);
804 catch (ArgumentException
) {
808 string name
= String
.Format ("{0}", (char)0);
809 tb
.DefineInitializedData (name
, new byte[1], 0);
810 Fail ("Names with embedded nulls should be rejected");
812 catch (ArgumentException
) {
817 // Can not be called on a created type, altough the MSDN docs does not mention this
819 tb
.DefineInitializedData ("BAR2", new byte[1], 0);
822 catch (InvalidOperationException
) {
827 public void DefineUninitializedDataInvalidArgs () {
828 TypeBuilder tb
= module
.DefineType (genTypeName ());
831 tb
.DefineUninitializedData (null, 1, 0);
834 catch (ArgumentNullException
) {
838 tb
.DefineUninitializedData ("", 1, 0);
841 catch (ArgumentException
) {
844 // The size of the data is less than or equal to zero ???
846 tb
.DefineUninitializedData ("BAR", 0, 0);
849 catch (ArgumentException
) {
853 string name
= String
.Format ("{0}", (char)0);
854 tb
.DefineUninitializedData (name
, 1, 0);
855 Fail ("Names with embedded nulls should be rejected");
857 catch (ArgumentException
) {
862 [ExpectedException (typeof (InvalidOperationException
))]
863 public void DefineUninitializedDataAlreadyCreated () {
864 TypeBuilder tb
= module
.DefineType (genTypeName ());
867 tb
.DefineUninitializedData ("BAR2", 1, 0);
871 public void DefineUninitializedData () {
872 TypeBuilder tb
= module
.DefineType (genTypeName ());
874 tb
.DefineUninitializedData ("foo", 4, FieldAttributes
.Public
);
876 Type t
= tb
.CreateType ();
878 object o
= Activator
.CreateInstance (t
);
880 FieldInfo fi
= t
.GetField ("foo");
882 object fieldVal
= fi
.GetValue (o
);
884 IntPtr ptr
= Marshal
.AllocHGlobal (4);
885 Marshal
.StructureToPtr (fieldVal
, ptr
, true);
886 Marshal
.FreeHGlobal (ptr
);
890 public void TestDefineMethod () {
891 TypeBuilder tb
= module
.DefineType (genTypeName ());
893 // Check invalid arguments
895 tb
.DefineMethod (null, 0, null, null);
898 catch (ArgumentNullException
) {
902 tb
.DefineMethod ("", 0, null, null);
905 catch (ArgumentException
) {
908 // Check non-virtual methods on an interface
909 TypeBuilder tb2
= module
.DefineType (genTypeName (), TypeAttributes
.Interface
| TypeAttributes
.Abstract
);
911 tb2
.DefineMethod ("FOO", MethodAttributes
.Abstract
, null, null);
914 catch (ArgumentException
) {
918 // Can not be called on a created type
920 tb
.DefineMethod ("bar", 0, null, null);
923 catch (InvalidOperationException
) {
927 // TODO: DefineMethodOverride
930 public void TestDefineNestedType () {
931 TypeBuilder tb
= module
.DefineType (genTypeName ());
933 // Check invalid arguments
935 tb
.DefineNestedType (null);
936 Fail ("Should reject null name");
938 catch (ArgumentNullException
) {
942 tb
.DefineNestedType ("");
943 Fail ("Should reject empty name");
945 catch (ArgumentException
) {
949 tb
.DefineNestedType (nullName ());
950 Fail ("Should reject name with embedded 0s");
952 catch (ArgumentException
) {
955 // If I fix the code so this works then mcs breaks -> how can mcs
956 // works under MS .NET in the first place ???
959 tb.DefineNestedType ("AA", TypeAttributes.Public, null, null);
960 Fail ("Nested visibility must be specified.");
962 catch (ArgumentException) {
967 tb
.DefineNestedType ("BB", TypeAttributes
.NestedPublic
, null,
969 Fail ("Should reject empty interface");
971 catch (ArgumentException
) {
974 // I think this should reject non-interfaces, but it does not
975 tb
.DefineNestedType ("BB", TypeAttributes
.NestedPublic
, null,
976 new Type
[1] { typeof (object) }
);
979 tb
.DefineNestedType ("Nest");
983 // According to the MSDN docs, this cannnot be called after the type
984 // is created, but it works.
985 tb
.DefineNestedType ("Nest2");
987 // According to the MSDN docs, a Sealed class can't contain nested
988 // types, but this is not true
989 TypeBuilder tb2
= module
.DefineType (genTypeName (), TypeAttributes
.Sealed
);
990 tb2
.DefineNestedType ("AA");
992 // According to the MSDN docs, interfaces can only contain interfaces,
993 // but this is not true
994 TypeBuilder tb3
= module
.DefineType (genTypeName (), TypeAttributes
.Interface
| TypeAttributes
.Abstract
);
996 tb3
.DefineNestedType ("AA");
998 // Check shorter versions
1000 TypeBuilder nested
= tb
.DefineNestedType ("N1");
1002 AssertEquals (nested
.Name
, "N1");
1003 AssertEquals (nested
.BaseType
, typeof (object));
1004 AssertEquals (nested
.Attributes
, TypeAttributes
.NestedPrivate
);
1005 AssertEquals (nested
.GetInterfaces ().Length
, 0);
1012 public void TestDefinePInvokeMethod () {
1013 TypeBuilder tb
= module
.DefineType (genTypeName ());
1015 tb
.DefinePInvokeMethod ("A", "B", "C", 0, 0, null, null, 0, 0);
1017 // Try invalid parameters
1019 tb
.DefinePInvokeMethod (null, "B", "C", 0, 0, null, null, 0, 0);
1022 catch (ArgumentNullException
) {
1026 // Try invalid attributes
1028 tb
.DefinePInvokeMethod ("A2", "B", "C", MethodAttributes
.Abstract
, 0, null, null, 0, 0);
1030 catch (ArgumentException
) {
1033 // Try an interface parent
1034 TypeBuilder tb2
= module
.DefineType (genTypeName (), TypeAttributes
.Interface
| TypeAttributes
.Abstract
);
1037 tb2
.DefinePInvokeMethod ("A", "B", "C", 0, 0, null, null, 0, 0);
1039 catch (ArgumentException
) {
1044 public void TestDefineProperty () {
1045 TypeBuilder tb
= module
.DefineType (genTypeName ());
1047 // Check null parameter types
1049 tb
.DefineProperty ("A", 0, null, new Type
[1]);
1051 catch (ArgumentNullException
) {
1056 [ExpectedException (typeof(NotSupportedException
))]
1057 public void TestIsDefinedIncomplete () {
1058 TypeBuilder tb
= module
.DefineType (genTypeName ());
1059 tb
.IsDefined (typeof (int), true);
1063 public void TestIsDefinedComplete () {
1064 TypeBuilder tb
= module
.DefineType (genTypeName ());
1066 ConstructorInfo obsoleteCtor
= typeof(ObsoleteAttribute
).GetConstructor(
1067 new Type
[] {typeof(string)}
);
1069 CustomAttributeBuilder caBuilder
= new CustomAttributeBuilder (obsoleteCtor
,
1070 new object[] { "obsolete message" }
, new FieldInfo
[0], new object[0]);
1072 tb
.SetCustomAttribute (caBuilder
);
1074 AssertEquals (true, tb
.IsDefined (typeof(ObsoleteAttribute
), false));
1078 [ExpectedException (typeof(NotSupportedException
))]
1079 public void TestGetCustomAttributesIncomplete ()
1081 TypeBuilder tb
= module
.DefineType (genTypeName ());
1082 tb
.GetCustomAttributes (false);
1086 public void TestGetCustomAttributesComplete ()
1088 TypeBuilder tb
= module
.DefineType (genTypeName ());
1090 ConstructorInfo guidCtor
= typeof(GuidAttribute
).GetConstructor (
1091 new Type
[] { typeof(string) }
);
1093 CustomAttributeBuilder caBuilder
= new CustomAttributeBuilder (guidCtor
,
1094 new object[] { Guid.NewGuid ().ToString ("D") }
, new FieldInfo
[0], new object[0]);
1096 tb
.SetCustomAttribute (caBuilder
);
1099 AssertEquals (1, tb
.GetCustomAttributes (false).Length
);
1103 [ExpectedException (typeof(NotSupportedException
))]
1104 public void TestGetCustomAttributesOfTypeIncomplete ()
1106 TypeBuilder tb
= module
.DefineType (genTypeName ());
1107 tb
.GetCustomAttributes (typeof(ObsoleteAttribute
), false);
1111 public void TestGetCustomAttributesOfTypeComplete ()
1113 TypeBuilder tb
= module
.DefineType (genTypeName ());
1115 ConstructorInfo guidCtor
= typeof(GuidAttribute
).GetConstructor (
1116 new Type
[] { typeof(string) }
);
1118 CustomAttributeBuilder caBuilder
= new CustomAttributeBuilder (guidCtor
,
1119 new object[] { Guid.NewGuid ().ToString ("D") }
, new FieldInfo
[0], new object[0]);
1121 tb
.SetCustomAttribute (caBuilder
);
1124 AssertEquals (1, tb
.GetCustomAttributes (typeof(GuidAttribute
), false).Length
);
1125 AssertEquals (0, tb
.GetCustomAttributes (typeof(ObsoleteAttribute
), false).Length
);
1129 [ExpectedException (typeof(ArgumentNullException
))]
1130 public void TestGetCustomAttributesOfNullTypeComplete ()
1132 TypeBuilder tb
= module
.DefineType (genTypeName ());
1134 tb
.GetCustomAttributes (null, false);
1138 [ExpectedException (typeof(NotSupportedException
))]
1139 [Ignore("mcs depends on this")]
1140 public void TestGetEventsIncomplete () {
1141 TypeBuilder tb
= module
.DefineType (genTypeName ());
1146 public void TestGetEventsComplete () {
1147 TypeBuilder tb
= module
.DefineType (genTypeName ());
1149 MethodBuilder onclickMethod
= tb
.DefineMethod ("OnChange", MethodAttributes
.Public
,
1150 typeof(void), new Type
[] { typeof(Object) }
);
1151 onclickMethod
.GetILGenerator ().Emit (OpCodes
.Ret
);
1153 // create public event
1154 EventBuilder eventbuilder
= tb
.DefineEvent ("Change", EventAttributes
.None
,
1155 typeof(ResolveEventHandler
));
1156 eventbuilder
.SetRaiseMethod (onclickMethod
);
1158 Type emittedType
= tb
.CreateType ();
1160 AssertEquals (1, tb
.GetEvents ().Length
);
1161 AssertEquals (tb
.GetEvents ().Length
, emittedType
.GetEvents ().Length
);
1166 [ExpectedException (typeof(NotSupportedException
))]
1167 [Ignore("mcs depends on this")]
1168 public void TestGetEventsFlagsIncomplete () {
1169 TypeBuilder tb
= module
.DefineType (genTypeName ());
1170 tb
.GetEvents (BindingFlags
.Public
);
1174 public void TestGetEventsFlagsComplete () {
1175 TypeBuilder tb
= module
.DefineType (genTypeName ());
1177 MethodBuilder onchangeMethod
= tb
.DefineMethod ("OnChange", MethodAttributes
.Public
,
1178 typeof(void), new Type
[] { typeof(Object) }
);
1179 onchangeMethod
.GetILGenerator ().Emit (OpCodes
.Ret
);
1181 // create public event
1182 EventBuilder changeEvent
= tb
.DefineEvent ("Change", EventAttributes
.None
,
1183 typeof(ResolveEventHandler
));
1184 changeEvent
.SetRaiseMethod (onchangeMethod
);
1186 // create non-public event
1187 EventBuilder redoChangeEvent
= tb
.DefineEvent ("RedoChange", EventAttributes
.None
,
1188 typeof(ResolveEventHandler
));
1190 Type emittedType
= tb
.CreateType ();
1192 AssertEquals (1, tb
.GetEvents (BindingFlags
.Instance
| BindingFlags
.Public
).Length
);
1193 AssertEquals (1, tb
.GetEvents (BindingFlags
.Instance
| BindingFlags
.NonPublic
).Length
);
1194 AssertEquals (2, tb
.GetEvents (BindingFlags
.Instance
| BindingFlags
.Public
| BindingFlags
.NonPublic
).Length
);
1195 AssertEquals (tb
.GetEvents (BindingFlags
.Instance
| BindingFlags
.Public
).Length
,
1196 emittedType
.GetEvents (BindingFlags
.Instance
| BindingFlags
.Public
).Length
);
1197 AssertEquals (tb
.GetEvents (BindingFlags
.Instance
| BindingFlags
.NonPublic
).Length
,
1198 emittedType
.GetEvents (BindingFlags
.Instance
| BindingFlags
.NonPublic
).Length
);
1199 AssertEquals (tb
.GetEvents (BindingFlags
.Instance
| BindingFlags
.Public
| BindingFlags
.NonPublic
).Length
,
1200 emittedType
.GetEvents (BindingFlags
.Instance
| BindingFlags
.Public
| BindingFlags
.NonPublic
).Length
);
1204 [ExpectedException (typeof(NotSupportedException
))]
1205 [Ignore("mcs depends on this")]
1206 public void TestGetEventIncomplete () {
1207 TypeBuilder tb
= module
.DefineType (genTypeName ());
1208 tb
.GetEvent ("FOO");
1212 public void TestGetEventComplete () {
1213 TypeBuilder tb
= module
.DefineType (genTypeName ());
1215 MethodBuilder onclickMethod
= tb
.DefineMethod ("OnChange", MethodAttributes
.Public
,
1216 typeof(void), new Type
[] { typeof(Object) }
);
1217 onclickMethod
.GetILGenerator ().Emit (OpCodes
.Ret
);
1219 EventBuilder eventbuilder
= tb
.DefineEvent ("Change", EventAttributes
.None
,
1220 typeof(ResolveEventHandler
));
1221 eventbuilder
.SetRaiseMethod (onclickMethod
);
1223 Type emittedType
= tb
.CreateType ();
1225 AssertNotNull (tb
.GetEvent ("Change"));
1226 AssertEquals (tb
.GetEvent ("Change"), emittedType
.GetEvent ("Change"));
1227 AssertNull (tb
.GetEvent ("NotChange"));
1228 AssertEquals (tb
.GetEvent ("NotChange"), emittedType
.GetEvent ("NotChange"));
1232 [ExpectedException (typeof(NotSupportedException
))]
1233 [Ignore("mcs depends on this")]
1234 public void TestGetEventFlagsIncomplete () {
1235 TypeBuilder tb
= module
.DefineType (genTypeName ());
1236 tb
.GetEvent ("FOO", BindingFlags
.Public
);
1240 public void TestGetEventFlagsComplete () {
1241 TypeBuilder tb
= module
.DefineType (genTypeName ());
1243 MethodBuilder onclickMethod
= tb
.DefineMethod ("OnChange", MethodAttributes
.Public
,
1244 typeof(void), new Type
[] { typeof(Object) }
);
1245 onclickMethod
.GetILGenerator ().Emit (OpCodes
.Ret
);
1247 EventBuilder eventbuilder
= tb
.DefineEvent ("Change", EventAttributes
.None
,
1248 typeof(ResolveEventHandler
));
1249 eventbuilder
.SetRaiseMethod (onclickMethod
);
1251 Type emittedType
= tb
.CreateType ();
1253 AssertNotNull (tb
.GetEvent ("Change", BindingFlags
.Instance
| BindingFlags
.Public
));
1254 AssertEquals (tb
.GetEvent ("Change", BindingFlags
.Instance
| BindingFlags
.Public
),
1255 emittedType
.GetEvent ("Change", BindingFlags
.Instance
| BindingFlags
.Public
));
1256 AssertNull (tb
.GetEvent ("Change", BindingFlags
.Instance
| BindingFlags
.NonPublic
));
1257 AssertEquals (tb
.GetEvent ("Change", BindingFlags
.Instance
| BindingFlags
.NonPublic
),
1258 emittedType
.GetEvent ("Change", BindingFlags
.Instance
| BindingFlags
.NonPublic
));
1262 [ExpectedException (typeof(NotSupportedException
))]
1263 [Ignore("mcs depends on this")]
1264 public void TestGetFieldsIncomplete () {
1265 TypeBuilder tb
= module
.DefineType (genTypeName ());
1270 public void TestGetFieldsComplete () {
1271 TypeBuilder tb
= module
.DefineType (genTypeName ());
1272 tb
.DefineField ("TestField", typeof(int), FieldAttributes
.Public
);
1274 Type emittedType
= tb
.CreateType ();
1276 AssertEquals (1, tb
.GetFields ().Length
);
1277 AssertEquals (tb
.GetFields ().Length
, emittedType
.GetFields().Length
);
1281 [ExpectedException (typeof(NotSupportedException
))]
1282 [Ignore("mcs depends on this")]
1283 public void TestGetFieldsFlagsIncomplete () {
1284 TypeBuilder tb
= module
.DefineType (genTypeName ());
1285 tb
.GetFields (BindingFlags
.Instance
| BindingFlags
.Public
);
1289 public void TestGetFieldsFlagsComplete () {
1290 TypeBuilder tb
= module
.DefineType (genTypeName ());
1291 tb
.DefineField ("TestField", typeof(int), FieldAttributes
.Public
);
1293 Type emittedType
= tb
.CreateType ();
1295 AssertEquals (1, tb
.GetFields (BindingFlags
.Instance
| BindingFlags
.Public
).Length
);
1296 AssertEquals (tb
.GetFields (BindingFlags
.Instance
| BindingFlags
.Public
).Length
,
1297 emittedType
.GetFields (BindingFlags
.Instance
| BindingFlags
.Public
).Length
);
1298 AssertEquals (0, tb
.GetFields (BindingFlags
.Instance
| BindingFlags
.NonPublic
).Length
);
1299 AssertEquals (tb
.GetFields (BindingFlags
.Instance
| BindingFlags
.NonPublic
).Length
,
1300 emittedType
.GetFields (BindingFlags
.Instance
| BindingFlags
.NonPublic
).Length
);
1304 [ExpectedException (typeof(NotSupportedException
))]
1305 [Ignore("mcs depends on this")]
1306 public void TestGetFieldIncomplete () {
1307 TypeBuilder tb
= module
.DefineType (genTypeName ());
1308 tb
.GetField ("test");
1312 public void TestGetFieldComplete () {
1313 TypeBuilder tb
= module
.DefineType (genTypeName ());
1314 tb
.DefineField ("TestField", typeof(int), FieldAttributes
.Public
);
1316 Type emittedType
= tb
.CreateType ();
1318 AssertNotNull (tb
.GetField ("TestField"));
1319 AssertEquals (tb
.GetField ("TestField").Name
, emittedType
.GetField ("TestField").Name
);
1320 AssertNull (tb
.GetField ("TestOtherField"));
1321 AssertEquals (tb
.GetField ("TestOtherField").Name
,
1322 emittedType
.GetField ("TestOtherField").Name
);
1326 [ExpectedException (typeof(NotSupportedException
))]
1327 [Ignore("mcs depends on this")]
1328 public void TestGetFieldFlagsIncomplete () {
1329 TypeBuilder tb
= module
.DefineType (genTypeName ());
1330 tb
.GetField ("test", BindingFlags
.Public
);
1334 public void TestGetFieldFlagsComplete () {
1335 TypeBuilder tb
= module
.DefineType (genTypeName ());
1336 tb
.DefineField ("TestField", typeof(int), FieldAttributes
.Public
);
1338 Type emittedType
= tb
.CreateType ();
1340 AssertNotNull (tb
.GetField ("TestField", BindingFlags
.Instance
| BindingFlags
.Public
));
1341 AssertEquals (tb
.GetField ("TestField", BindingFlags
.Instance
| BindingFlags
.Public
).Name
,
1342 emittedType
.GetField ("TestField", BindingFlags
.Instance
| BindingFlags
.Public
).Name
);
1343 AssertNull (tb
.GetField ("TestField", BindingFlags
.Instance
| BindingFlags
.NonPublic
));
1344 AssertEquals (tb
.GetField ("TestField", BindingFlags
.Instance
| BindingFlags
.NonPublic
),
1345 emittedType
.GetField ("TestField", BindingFlags
.Instance
| BindingFlags
.NonPublic
));
1349 [ExpectedException (typeof(NotSupportedException
))]
1350 [Ignore("mcs depends on this")]
1351 public void TestGetPropertiesIncomplete () {
1352 TypeBuilder tb
= module
.DefineType (genTypeName ());
1353 tb
.GetProperties ();
1357 public void TestGetPropertiesComplete () {
1358 TypeBuilder tb
= module
.DefineType (genTypeName ());
1359 DefineStringProperty (tb
, "CustomerName", "customerName", MethodAttributes
.Public
);
1361 Type emittedType
= tb
.CreateType ();
1363 AssertEquals (1, tb
.GetProperties ().Length
);
1364 AssertEquals (tb
.GetProperties ().Length
, emittedType
.GetProperties ().Length
);
1368 [ExpectedException (typeof(NotSupportedException
))]
1369 [Ignore("mcs depends on this")]
1370 public void TestGetPropertiesFlagsIncomplete () {
1371 TypeBuilder tb
= module
.DefineType (genTypeName ());
1372 tb
.GetProperties (BindingFlags
.Public
);
1376 public void TestGetPropertiesFlagsComplete () {
1377 TypeBuilder tb
= module
.DefineType (genTypeName ());
1378 DefineStringProperty (tb
, "CustomerName", "customerName", MethodAttributes
.Public
);
1380 Type emittedType
= tb
.CreateType ();
1382 AssertEquals (1, tb
.GetProperties (BindingFlags
.Instance
| BindingFlags
.Public
).Length
);
1383 AssertEquals (tb
.GetProperties (BindingFlags
.Instance
| BindingFlags
.Public
).Length
,
1384 emittedType
.GetProperties (BindingFlags
.Instance
| BindingFlags
.Public
).Length
);
1385 AssertEquals (0, tb
.GetProperties (BindingFlags
.Instance
| BindingFlags
.NonPublic
).Length
);
1386 AssertEquals (tb
.GetProperties (BindingFlags
.Instance
| BindingFlags
.NonPublic
).Length
,
1387 emittedType
.GetProperties (BindingFlags
.Instance
| BindingFlags
.NonPublic
).Length
);
1391 [ExpectedException (typeof(NotSupportedException
))]
1392 public void TestGetPropertyIncomplete () {
1393 TypeBuilder tb
= module
.DefineType (genTypeName ());
1394 tb
.GetProperty ("test");
1398 public void TestGetPropertyComplete () {
1399 TypeBuilder tb
= module
.DefineType (genTypeName ());
1400 DefineStringProperty (tb
, "CustomerName", "customerName", MethodAttributes
.Public
);
1402 Type emittedType
= tb
.CreateType ();
1404 AssertNotNull (emittedType
.GetProperty ("CustomerName"));
1405 AssertNull (emittedType
.GetProperty ("OtherCustomerName"));
1408 tb
.GetProperty ("CustomerName");
1410 } catch (NotSupportedException
) {}
1414 [ExpectedException (typeof(NotSupportedException
))]
1415 public void TestGetPropertyFlagsIncomplete () {
1416 TypeBuilder tb
= module
.DefineType (genTypeName ());
1417 tb
.GetProperty ("test", BindingFlags
.Public
);
1421 public void TestGetPropertyFlagsComplete () {
1422 TypeBuilder tb
= module
.DefineType (genTypeName ());
1423 DefineStringProperty (tb
, "CustomerName", "customerName", MethodAttributes
.Public
);
1425 Type emittedType
= tb
.CreateType ();
1427 AssertNotNull (emittedType
.GetProperty ("CustomerName", BindingFlags
.Instance
|
1428 BindingFlags
.Public
));
1429 AssertNull (emittedType
.GetProperty ("CustomerName", BindingFlags
.Instance
|
1430 BindingFlags
.NonPublic
));
1433 tb
.GetProperty ("CustomerName", BindingFlags
.Instance
| BindingFlags
.Public
);
1436 catch (NotSupportedException
) { }
1440 [ExpectedException (typeof(NotSupportedException
))]
1441 [Ignore("mcs depends on this")]
1442 public void TestGetMethodsIncomplete () {
1443 TypeBuilder tb
= module
.DefineType (genTypeName ());
1448 public void TestGetMethodsComplete () {
1449 TypeBuilder tb
= module
.DefineType (genTypeName ());
1450 MethodBuilder helloMethod
= tb
.DefineMethod ("HelloMethod",
1451 MethodAttributes
.Public
, typeof(string), new Type
[0]);
1452 ILGenerator helloMethodIL
= helloMethod
.GetILGenerator ();
1453 helloMethodIL
.Emit (OpCodes
.Ldstr
, "Hi! ");
1454 helloMethodIL
.Emit (OpCodes
.Ldarg_1
);
1455 MethodInfo infoMethod
= typeof(string).GetMethod ("Concat",
1456 new Type
[] { typeof(string), typeof(string) }
);
1457 helloMethodIL
.Emit (OpCodes
.Call
, infoMethod
);
1458 helloMethodIL
.Emit (OpCodes
.Ret
);
1460 Type emittedType
= tb
.CreateType ();
1462 AssertEquals (typeof(object).GetMethods (BindingFlags
.Public
| BindingFlags
.Instance
).Length
+ 1,
1463 tb
.GetMethods ().Length
);
1464 AssertEquals (tb
.GetMethods ().Length
, emittedType
.GetMethods ().Length
);
1468 [ExpectedException (typeof(NotSupportedException
))]
1469 [Ignore("mcs depends on this")]
1470 public void TestGetMethodsFlagsIncomplete () {
1471 TypeBuilder tb
= module
.DefineType (genTypeName ());
1472 tb
.GetMethods (BindingFlags
.Public
);
1476 public void TestGetMethodsFlagsComplete () {
1477 TypeBuilder tb
= module
.DefineType (genTypeName ());
1478 MethodBuilder helloMethod
= tb
.DefineMethod ("HelloMethod",
1479 MethodAttributes
.Public
, typeof(string), new Type
[0]);
1480 ILGenerator helloMethodIL
= helloMethod
.GetILGenerator ();
1481 helloMethodIL
.Emit (OpCodes
.Ldstr
, "Hi! ");
1482 helloMethodIL
.Emit (OpCodes
.Ldarg_1
);
1483 MethodInfo infoMethod
= typeof(string).GetMethod ("Concat",
1484 new Type
[] { typeof(string), typeof(string) }
);
1485 helloMethodIL
.Emit (OpCodes
.Call
, infoMethod
);
1486 helloMethodIL
.Emit (OpCodes
.Ret
);
1488 Type emittedType
= tb
.CreateType ();
1490 AssertEquals (1, tb
.GetMethods (BindingFlags
.Instance
| BindingFlags
.Public
| BindingFlags
.DeclaredOnly
).Length
);
1491 AssertEquals (tb
.GetMethods (BindingFlags
.Instance
| BindingFlags
.Public
).Length
,
1492 emittedType
.GetMethods (BindingFlags
.Instance
| BindingFlags
.Public
).Length
);
1493 AssertEquals (0, tb
.GetMethods (BindingFlags
.Instance
| BindingFlags
.NonPublic
| BindingFlags
.DeclaredOnly
).Length
);
1494 AssertEquals (tb
.GetMethods (BindingFlags
.Instance
| BindingFlags
.NonPublic
).Length
,
1495 emittedType
.GetMethods (BindingFlags
.Instance
| BindingFlags
.NonPublic
).Length
);
1499 [ExpectedException (typeof(NotSupportedException
))]
1500 public void TestGetMemberIncomplete () {
1501 TypeBuilder tb
= module
.DefineType (genTypeName ());
1502 tb
.GetMember ("FOO", MemberTypes
.All
, BindingFlags
.Public
);
1506 public void TestGetMemberComplete () {
1507 TypeBuilder tb
= module
.DefineType (genTypeName ());
1508 tb
.DefineField ("FOO", typeof(int), FieldAttributes
.Private
);
1510 Type emittedType
= tb
.CreateType ();
1512 AssertEquals (1, tb
.GetMember ("FOO", MemberTypes
.Field
, BindingFlags
.Instance
| BindingFlags
.NonPublic
).Length
);
1513 AssertEquals (0, tb
.GetMember ("FOO", MemberTypes
.Field
, BindingFlags
.Instance
| BindingFlags
.Public
).Length
);
1517 [ExpectedException (typeof(NotSupportedException
))]
1518 public void TestGetMembersIncomplete () {
1519 TypeBuilder tb
= module
.DefineType (genTypeName ());
1524 public void TestGetMembersComplete () {
1525 TypeBuilder tb
= module
.DefineType (genTypeName ());
1526 Type emittedType
= tb
.CreateType ();
1528 AssertEquals (tb
.GetMembers ().Length
, emittedType
.GetMembers ().Length
);
1532 [ExpectedException (typeof(NotSupportedException
))]
1533 public void TestGetMembersFlagsIncomplete () {
1534 TypeBuilder tb
= module
.DefineType (genTypeName ());
1535 tb
.GetMembers (BindingFlags
.Public
);
1539 public void TestGetMembersFlagsComplete () {
1540 TypeBuilder tb
= module
.DefineType (genTypeName ());
1541 tb
.DefineField ("FOO", typeof(int), FieldAttributes
.Public
);
1543 Type emittedType
= tb
.CreateType ();
1545 Assert (tb
.GetMembers (BindingFlags
.Instance
| BindingFlags
.Public
).Length
!= 0);
1546 AssertEquals (tb
.GetMembers (BindingFlags
.Instance
| BindingFlags
.Public
).Length
,
1547 emittedType
.GetMembers (BindingFlags
.Instance
| BindingFlags
.Public
).Length
);
1548 AssertEquals (tb
.GetMembers (BindingFlags
.Instance
| BindingFlags
.NonPublic
).Length
,
1549 emittedType
.GetMembers (BindingFlags
.Instance
| BindingFlags
.NonPublic
).Length
);
1553 [ExpectedException (typeof(NotSupportedException
))]
1554 public void TestGetInterfaceIncomplete () {
1555 TypeBuilder tb
= module
.DefineType (genTypeName ());
1556 tb
.GetInterface ("FOO", true);
1560 public void TestGetInterfaces () {
1561 TypeBuilder tb
= module
.DefineType (genTypeName ());
1562 Type
[] interfaces
= tb
.GetInterfaces ();
1563 AssertEquals (0, interfaces
.Length
);
1565 TypeBuilder tbInterface
= module
.DefineType (genTypeName (), TypeAttributes
.Public
| TypeAttributes
.Interface
| TypeAttributes
.Abstract
);
1566 Type emittedInterface
= tbInterface
.CreateType ();
1568 tb
= module
.DefineType (genTypeName (), TypeAttributes
.Public
, typeof(object), new Type
[] { emittedInterface }
);
1569 interfaces
= tb
.GetInterfaces ();
1570 AssertEquals (1, interfaces
.Length
);
1574 [ExpectedException (typeof (InvalidOperationException
))]
1575 public void TestAddDeclarativeSecurityAlreadyCreated () {
1576 TypeBuilder tb
= module
.DefineType (genTypeName ());
1579 PermissionSet
set = new PermissionSet (PermissionState
.Unrestricted
);
1580 tb
.AddDeclarativeSecurity (SecurityAction
.Demand
, set);
1584 [ExpectedException (typeof (ArgumentNullException
))]
1585 public void TestAddDeclarativeSecurityNullPermissionSet () {
1586 TypeBuilder tb
= module
.DefineType (genTypeName ());
1588 tb
.AddDeclarativeSecurity (SecurityAction
.Demand
, null);
1592 public void TestAddDeclarativeSecurityInvalidAction () {
1593 TypeBuilder tb
= module
.DefineType (genTypeName ());
1595 SecurityAction
[] actions
= new SecurityAction
[] {
1596 SecurityAction
.RequestMinimum
,
1597 SecurityAction
.RequestOptional
,
1598 SecurityAction
.RequestRefuse
};
1599 PermissionSet
set = new PermissionSet (PermissionState
.Unrestricted
);
1601 foreach (SecurityAction action
in actions
) {
1603 tb
.AddDeclarativeSecurity (action
, set);
1606 catch (ArgumentException
) {
1612 [ExpectedException (typeof (InvalidOperationException
))]
1613 public void TestAddDeclarativeSecurityDuplicateAction () {
1614 TypeBuilder tb
= module
.DefineType (genTypeName ());
1616 PermissionSet
set = new PermissionSet (PermissionState
.Unrestricted
);
1617 tb
.AddDeclarativeSecurity (SecurityAction
.Demand
, set);
1618 tb
.AddDeclarativeSecurity (SecurityAction
.Demand
, set);
1622 public void TestEnums () {
1623 TypeAttributes typeAttrs
= TypeAttributes
.Class
| TypeAttributes
.Public
| TypeAttributes
.Sealed
;
1624 TypeBuilder enumToCreate
= module
.DefineType(genTypeName (), typeAttrs
,
1626 enumToCreate
.SetCustomAttribute (new CustomAttributeBuilder (typeof (FlagsAttribute
).GetConstructors ()[0], new Type
[0]));
1627 // add value__ field, see DefineEnum method of ModuleBuilder
1628 enumToCreate
.DefineField("value__", typeof(Int32
),
1629 FieldAttributes
.Public
| FieldAttributes
.SpecialName
| FieldAttributes
.RTSpecialName
);
1632 FieldBuilder fb
= enumToCreate
.DefineField("A", enumToCreate
,
1633 FieldAttributes
.Public
| FieldAttributes
.Static
| FieldAttributes
.Literal
);
1634 fb
.SetConstant((Int32
) 0);
1636 fb
= enumToCreate
.DefineField("B", enumToCreate
,
1637 FieldAttributes
.Public
| FieldAttributes
.Static
| FieldAttributes
.Literal
);
1638 fb
.SetConstant((Int32
) 1);
1640 fb
= enumToCreate
.DefineField("C", enumToCreate
,
1641 FieldAttributes
.Public
| FieldAttributes
.Static
| FieldAttributes
.Literal
);
1642 fb
.SetConstant((Int32
) 2);
1644 Type enumType
= enumToCreate
.CreateType();
1646 object enumVal
= Enum
.ToObject(enumType
, (Int32
) 3);
1648 AssertEquals ("B, C", enumVal
.ToString ());
1649 AssertEquals (3, (Int32
)enumVal
);
1653 public void DefineEnum () {
1654 TypeBuilder typeBuilder
= module
.DefineType (genTypeName (),
1655 TypeAttributes
.Public
);
1656 EnumBuilder enumBuilder
= module
.DefineEnum (genTypeName (),
1657 TypeAttributes
.Public
, typeof(int));
1658 typeBuilder
.DefineField ("myField", enumBuilder
, FieldAttributes
.Private
);
1659 enumBuilder
.CreateType();
1660 typeBuilder
.CreateType();
1664 [ExpectedException(typeof(TypeLoadException
))]
1665 public void DefineEnumThrowIfTypeBuilderCalledBeforeEnumBuilder () {
1666 TypeBuilder typeBuilder
= module
.DefineType (genTypeName (),
1667 TypeAttributes
.Public
);
1668 EnumBuilder enumBuilder
= module
.DefineEnum (genTypeName (),
1669 TypeAttributes
.Public
, typeof(int));
1670 typeBuilder
.DefineField ("myField", enumBuilder
, FieldAttributes
.Private
);
1671 typeBuilder
.CreateType();
1672 enumBuilder
.CreateType();
1674 private void DefineStringProperty (TypeBuilder tb
, string propertyName
, string fieldName
, MethodAttributes methodAttribs
) {
1675 // define the field holding the property value
1676 FieldBuilder fieldBuilder
= tb
.DefineField (fieldName
,
1677 typeof(string), FieldAttributes
.Private
);
1679 PropertyBuilder propertyBuilder
= tb
.DefineProperty (
1680 propertyName
, PropertyAttributes
.HasDefault
, typeof(string),
1681 new Type
[] { typeof(string) }
);
1683 // First, we'll define the behavior of the "get" property for CustomerName as a method.
1684 MethodBuilder getMethodBuilder
= tb
.DefineMethod ("Get" + propertyName
,
1689 ILGenerator getIL
= getMethodBuilder
.GetILGenerator ();
1691 getIL
.Emit (OpCodes
.Ldarg_0
);
1692 getIL
.Emit (OpCodes
.Ldfld
, fieldBuilder
);
1693 getIL
.Emit (OpCodes
.Ret
);
1695 // Now, we'll define the behavior of the "set" property for CustomerName.
1696 MethodBuilder setMethodBuilder
= tb
.DefineMethod ("Set" + propertyName
,
1699 new Type
[] { typeof(string) }
);
1701 ILGenerator setIL
= setMethodBuilder
.GetILGenerator ();
1703 setIL
.Emit (OpCodes
.Ldarg_0
);
1704 setIL
.Emit (OpCodes
.Ldarg_1
);
1705 setIL
.Emit (OpCodes
.Stfld
, fieldBuilder
);
1706 setIL
.Emit (OpCodes
.Ret
);
1708 // Last, we must map the two methods created above to our PropertyBuilder to
1709 // their corresponding behaviors, "get" and "set" respectively.
1710 propertyBuilder
.SetGetMethod (getMethodBuilder
);
1711 propertyBuilder
.SetSetMethod (setMethodBuilder
);
1714 static int handler_called
= 0;
1717 public void TestTypeResolve () {
1718 string typeName
= genTypeName ();
1720 ResolveEventHandler handler
= new ResolveEventHandler (TypeResolve
);
1721 AppDomain
.CurrentDomain
.TypeResolve
+= handler
;
1723 Type t
= Type
.GetType (typeName
);
1724 AssertEquals (typeName
, t
.Name
);
1725 AssertEquals (1, handler_called
);
1726 AppDomain
.CurrentDomain
.TypeResolve
-= handler
;
1729 Assembly
TypeResolve (object sender
, ResolveEventArgs args
) {
1730 TypeBuilder tb
= module
.DefineType (args
.Name
, TypeAttributes
.Public
);