Set svn:eol-style=native, delete svn:executable.
[mono.git] / mcs / class / corlib / System.Reflection.Emit / EventBuilder.cs
1
2 //
3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24
25 //
26 // System.Reflection.Emit/EventBuilder.cs
27 //
28 // Author:
29 //   Paolo Molaro (lupus@ximian.com)
30 //
31 // (C) 2001 Ximian, Inc.  http://www.ximian.com
32 //
33
34 using System;
35 using System.Reflection;
36 using System.Reflection.Emit;
37 using System.Globalization;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
40
41 namespace System.Reflection.Emit {
42         public sealed class EventBuilder {
43                 string name;
44                 Type type;
45                 TypeBuilder typeb;
46                 CustomAttributeBuilder[] cattrs;
47                 MethodBuilder add_method;
48                 MethodBuilder remove_method;
49                 MethodBuilder raise_method;
50                 MethodBuilder[] other_methods;
51                 EventAttributes attrs;
52                 int table_idx;
53
54                 internal EventBuilder (TypeBuilder tb, string eventName, EventAttributes eventAttrs, Type eventType) {
55                         name = eventName;
56                         attrs = eventAttrs;
57                         type = eventType;
58                         typeb = tb;
59                         table_idx = get_next_table_index (this, 0x14, true);
60                 }
61
62                 internal int get_next_table_index (object obj, int table, bool inc) {
63                         return typeb.get_next_table_index (obj, table, inc);
64                 }
65
66                 public void AddOtherMethod( MethodBuilder mdBuilder) {
67                         if (mdBuilder == null)
68                                 throw new ArgumentNullException ("mdBuilder");
69                         RejectIfCreated ();
70                         if (other_methods != null) {
71                                 MethodBuilder[] newv = new MethodBuilder [other_methods.Length + 1];
72                                 other_methods.CopyTo (newv, 0);
73                                 other_methods = newv;
74                         } else {
75                                 other_methods = new MethodBuilder [1];
76                         }
77                         other_methods [other_methods.Length - 1] = mdBuilder;
78                 }
79                 
80                 public EventToken GetEventToken () {
81                         return new EventToken (0x14000000 | table_idx);
82                 }
83                 public void SetAddOnMethod( MethodBuilder mdBuilder) {
84                         if (mdBuilder == null)
85                                 throw new ArgumentNullException ("mdBuilder");
86                         RejectIfCreated ();
87                         add_method = mdBuilder;
88                 }
89                 public void SetRaiseMethod( MethodBuilder mdBuilder) {
90                         if (mdBuilder == null)
91                                 throw new ArgumentNullException ("mdBuilder");
92                         RejectIfCreated ();
93                         raise_method = mdBuilder;
94                 }
95                 public void SetRemoveOnMethod( MethodBuilder mdBuilder) {
96                         if (mdBuilder == null)
97                                 throw new ArgumentNullException ("mdBuilder");
98                         RejectIfCreated ();
99                         remove_method = mdBuilder;
100                 }
101
102                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
103                         if (customBuilder == null)
104                                 throw new ArgumentNullException ("customBuilder");
105                         RejectIfCreated ();
106                         if (cattrs != null) {
107                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
108                                 cattrs.CopyTo (new_array, 0);
109                                 new_array [cattrs.Length] = customBuilder;
110                                 cattrs = new_array;
111                         } else {
112                                 cattrs = new CustomAttributeBuilder [1];
113                                 cattrs [0] = customBuilder;
114                         }
115                 }
116                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
117                         if (con == null)
118                                 throw new ArgumentNullException ("con");
119                         if (binaryAttribute == null)
120                                 throw new ArgumentNullException ("binaryAttribute");
121                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
122                 }
123
124                 private void RejectIfCreated () {
125                         if (typeb.is_created)
126                                 throw new InvalidOperationException ("Type definition of the method is complete.");
127                 }
128         }
129 }
130