2 // EventBuilder.cs - NUnit Test Cases for the EventBuilder class
4 // Zoltan Varga (vargaz@freemail.hu)
6 // (C) Ximian, Inc. http://www.ximian.com
9 using System.Threading;
10 using System.Reflection;
11 using System.Reflection.Emit;
12 using System.Runtime.CompilerServices;
14 using NUnit.Framework;
16 namespace MonoTests.System.Reflection.Emit
20 public class EventBuilderTest : Assertion
22 public delegate void AnEvent (object o);
24 private TypeBuilder tb;
26 private ModuleBuilder module;
28 private EventBuilder eb;
30 private MethodBuilder mb;
33 protected void SetUp () {
34 AssemblyName assemblyName = new AssemblyName();
35 assemblyName.Name = GetType().FullName;
37 AssemblyBuilder assembly
38 = Thread.GetDomain().DefineDynamicAssembly(
39 assemblyName, AssemblyBuilderAccess.Run);
41 module = assembly.DefineDynamicModule("module1");
43 tb = module.DefineType("class1",
44 TypeAttributes.Public);
46 eb = tb.DefineEvent ("event1", EventAttributes.None, typeof (AnEvent));
48 tb.DefineMethod ("OnAnEvent",
49 MethodAttributes.Public, typeof (void),
50 new Type [] { typeof (AnEvent) });
51 ILGenerator ilgen = mb.GetILGenerator();
52 ilgen.Emit (OpCodes.Ret);
54 // These two are required
55 eb.SetAddOnMethod (mb);
56 eb.SetRemoveOnMethod (mb);
60 [ExpectedException (typeof (ArgumentNullException))]
61 public void TestSetAddOnMethod1 () {
62 eb.SetAddOnMethod (null);
66 [ExpectedException (typeof (InvalidOperationException))]
67 public void TestSetAddOnMethod2 () {
70 eb.SetAddOnMethod (mb);
74 [ExpectedException (typeof (ArgumentNullException))]
75 public void TestSetRaiseMethod1 () {
76 eb.SetRaiseMethod (null);
80 [ExpectedException (typeof (InvalidOperationException))]
81 public void TestSetRaiseMethod2 () {
84 eb.SetRaiseMethod (mb);
88 [ExpectedException (typeof (ArgumentNullException))]
89 public void TestSetRemoveAddOnMethod1 () {
90 eb.SetRemoveOnMethod (null);
94 [ExpectedException (typeof (InvalidOperationException))]
95 public void TestSetRemoveAddOnMethod2 () {
98 eb.SetRemoveOnMethod (mb);
102 [ExpectedException (typeof (ArgumentNullException))]
103 public void TestAddOtherMethod1 () {
104 eb.AddOtherMethod (null);
108 [ExpectedException (typeof (InvalidOperationException))]
109 public void TestAddOtherMethod2 () {
112 eb.AddOtherMethod (mb);
116 [ExpectedException (typeof (ArgumentNullException))]
117 public void TestSetCustomAttribute1 () {
118 eb.SetCustomAttribute (null);
122 [ExpectedException (typeof (ArgumentNullException))]
123 public void TestSetCustomAttribute2 () {
124 eb.SetCustomAttribute (null, new byte [1]);
128 [ExpectedException (typeof (ArgumentNullException))]
129 public void TestSetCustomAttribute3 () {
130 ConstructorInfo con = typeof (String).GetConstructors ()[0];
131 eb.SetCustomAttribute (con, null);
135 [ExpectedException (typeof (InvalidOperationException))]
136 public void TestSetCustomAttribute4 () {
139 byte[] custAttrData = { 1, 0, 0, 0, 0};
140 Type attrType = Type.GetType
141 ("System.Reflection.AssemblyKeyNameAttribute");
142 Type[] paramTypes = new Type[1];
143 paramTypes[0] = typeof(String);
144 ConstructorInfo ctorInfo =
145 attrType.GetConstructor(paramTypes);
147 eb.SetCustomAttribute (ctorInfo, custAttrData);
151 [ExpectedException (typeof (InvalidOperationException))]
152 public void TestSetCustomAttribute5 () {
155 eb.SetCustomAttribute (new CustomAttributeBuilder (typeof (MethodImplAttribute).GetConstructor (new Type[1] { typeof (short) }), new object[1] {(short)MethodImplAttributes.Synchronized}));
159 public void TestCreation () {
160 eb = tb.DefineEvent ("event2", EventAttributes.SpecialName, typeof (AnEvent));
162 eb.SetRaiseMethod (mb);
163 eb.SetAddOnMethod (mb);
164 eb.SetRemoveOnMethod (mb);
165 eb.AddOtherMethod (mb);
166 eb.AddOtherMethod (mb);
168 Type t = tb.CreateType ();
170 MethodInfo mi = t.GetMethod ("OnAnEvent");
172 EventInfo[] events = t.GetEvents ();
173 AssertEquals (2, events.Length);
176 EventInfo ev = t.GetEvent ("event1");
177 AssertEquals ("event1", ev.Name);
178 AssertEquals (EventAttributes.None, ev.Attributes);
179 AssertEquals (t, ev.DeclaringType);
181 AssertEquals (typeof (AnEvent), ev.EventHandlerType);
182 AssertEquals (true, ev.IsMulticast);
183 AssertEquals (false, ev.IsSpecialName);
185 AssertEquals (mi, ev.GetAddMethod ());
186 AssertEquals (null, ev.GetRaiseMethod ());
187 AssertEquals (mi, ev.GetRemoveMethod ());
191 EventInfo ev = t.GetEvent ("event2");
192 AssertEquals ("event2", ev.Name);
193 AssertEquals (EventAttributes.SpecialName, ev.Attributes);
194 AssertEquals (t, ev.DeclaringType);
196 AssertEquals (typeof (AnEvent), ev.EventHandlerType);
197 AssertEquals (true, ev.IsMulticast);
198 AssertEquals (true, ev.IsSpecialName);
200 AssertEquals (mi, ev.GetAddMethod ());
201 AssertEquals (mi, ev.GetRaiseMethod ());
202 AssertEquals (mi, ev.GetRemoveMethod ());