2003-11-21 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / EventBuilder.cs
1
2 //
3 // System.Reflection.Emit/EventBuilder.cs
4 //
5 // Author:
6 //   Paolo Molaro (lupus@ximian.com)
7 //
8 // (C) 2001 Ximian, Inc.  http://www.ximian.com
9 //
10
11 using System;
12 using System.Reflection;
13 using System.Reflection.Emit;
14 using System.Globalization;
15 using System.Runtime.CompilerServices;
16 using System.Runtime.InteropServices;
17
18 namespace System.Reflection.Emit {
19         public sealed class EventBuilder {
20                 string name;
21                 Type type;
22                 TypeBuilder typeb;
23                 CustomAttributeBuilder[] cattrs;
24                 MethodBuilder add_method;
25                 MethodBuilder remove_method;
26                 MethodBuilder raise_method;
27                 MethodBuilder[] other_methods;
28                 EventAttributes attrs;
29                 int table_idx;
30
31                 internal EventBuilder (TypeBuilder tb, string eventName, EventAttributes eventAttrs, Type eventType) {
32                         name = eventName;
33                         attrs = eventAttrs;
34                         type = eventType;
35                         typeb = tb;
36                         table_idx = get_next_table_index (this, 0x14, true);
37                 }
38
39                 internal int get_next_table_index (object obj, int table, bool inc) {
40                         return typeb.get_next_table_index (obj, table, inc);
41                 }
42
43                 public void AddOtherMethod( MethodBuilder mdBuilder) {
44                         if (mdBuilder == null)
45                                 throw new ArgumentNullException ("mdBuilder");
46                         RejectIfCreated ();
47                         if (other_methods != null) {
48                                 MethodBuilder[] newv = new MethodBuilder [other_methods.Length + 1];
49                                 other_methods.CopyTo (newv, 0);
50                                 other_methods = newv;
51                         } else {
52                                 other_methods = new MethodBuilder [1];
53                         }
54                         other_methods [other_methods.Length - 1] = mdBuilder;
55                 }
56                 
57                 public EventToken GetEventToken () {
58                         return new EventToken (0x14000000 | table_idx);
59                 }
60                 public void SetAddOnMethod( MethodBuilder mdBuilder) {
61                         if (mdBuilder == null)
62                                 throw new ArgumentNullException ("mdBuilder");
63                         RejectIfCreated ();
64                         add_method = mdBuilder;
65                 }
66                 public void SetRaiseMethod( MethodBuilder mdBuilder) {
67                         if (mdBuilder == null)
68                                 throw new ArgumentNullException ("mdBuilder");
69                         RejectIfCreated ();
70                         raise_method = mdBuilder;
71                 }
72                 public void SetRemoveOnMethod( MethodBuilder mdBuilder) {
73                         if (mdBuilder == null)
74                                 throw new ArgumentNullException ("mdBuilder");
75                         RejectIfCreated ();
76                         remove_method = mdBuilder;
77                 }
78
79                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
80                         if (customBuilder == null)
81                                 throw new ArgumentNullException ("customBuilder");
82                         RejectIfCreated ();
83                         if (cattrs != null) {
84                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
85                                 cattrs.CopyTo (new_array, 0);
86                                 new_array [cattrs.Length] = customBuilder;
87                                 cattrs = new_array;
88                         } else {
89                                 cattrs = new CustomAttributeBuilder [1];
90                                 cattrs [0] = customBuilder;
91                         }
92                 }
93                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
94                         if (con == null)
95                                 throw new ArgumentNullException ("con");
96                         if (binaryAttribute == null)
97                                 throw new ArgumentNullException ("binaryAttribute");
98                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
99                 }
100
101                 private void RejectIfCreated () {
102                         if (typeb.is_created)
103                                 throw new InvalidOperationException ("Type definition of the method is complete.");
104                 }
105         }
106 }
107