New test.
[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 #if NET_2_0
43         [ComVisible (true)]
44         [ComDefaultInterface (typeof (_EventBuilder))]
45 #endif
46         [ClassInterface (ClassInterfaceType.None)]
47         public sealed class EventBuilder : _EventBuilder {
48                 string name;
49                 Type type;
50                 TypeBuilder typeb;
51                 CustomAttributeBuilder[] cattrs;
52                 MethodBuilder add_method;
53                 MethodBuilder remove_method;
54                 MethodBuilder raise_method;
55                 MethodBuilder[] other_methods;
56                 EventAttributes attrs;
57                 int table_idx;
58
59                 internal EventBuilder (TypeBuilder tb, string eventName, EventAttributes eventAttrs, Type eventType) {
60                         name = eventName;
61                         attrs = eventAttrs;
62                         type = eventType;
63                         typeb = tb;
64                         table_idx = get_next_table_index (this, 0x14, true);
65                 }
66
67                 internal int get_next_table_index (object obj, int table, bool inc) {
68                         return typeb.get_next_table_index (obj, table, inc);
69                 }
70
71                 public void AddOtherMethod( MethodBuilder mdBuilder) {
72                         if (mdBuilder == null)
73                                 throw new ArgumentNullException ("mdBuilder");
74                         RejectIfCreated ();
75                         if (other_methods != null) {
76                                 MethodBuilder[] newv = new MethodBuilder [other_methods.Length + 1];
77                                 other_methods.CopyTo (newv, 0);
78                                 other_methods = newv;
79                         } else {
80                                 other_methods = new MethodBuilder [1];
81                         }
82                         other_methods [other_methods.Length - 1] = mdBuilder;
83                 }
84                 
85                 public EventToken GetEventToken () {
86                         return new EventToken (0x14000000 | table_idx);
87                 }
88                 public void SetAddOnMethod( MethodBuilder mdBuilder) {
89                         if (mdBuilder == null)
90                                 throw new ArgumentNullException ("mdBuilder");
91                         RejectIfCreated ();
92                         add_method = mdBuilder;
93                 }
94                 public void SetRaiseMethod( MethodBuilder mdBuilder) {
95                         if (mdBuilder == null)
96                                 throw new ArgumentNullException ("mdBuilder");
97                         RejectIfCreated ();
98                         raise_method = mdBuilder;
99                 }
100                 public void SetRemoveOnMethod( MethodBuilder mdBuilder) {
101                         if (mdBuilder == null)
102                                 throw new ArgumentNullException ("mdBuilder");
103                         RejectIfCreated ();
104                         remove_method = mdBuilder;
105                 }
106
107                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
108                         if (customBuilder == null)
109                                 throw new ArgumentNullException ("customBuilder");
110                         RejectIfCreated ();
111 #if NET_2_0
112                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
113                         if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
114                                 attrs |= EventAttributes.SpecialName;
115                                 return;
116                         }
117 #endif
118                         if (cattrs != null) {
119                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
120                                 cattrs.CopyTo (new_array, 0);
121                                 new_array [cattrs.Length] = customBuilder;
122                                 cattrs = new_array;
123                         } else {
124                                 cattrs = new CustomAttributeBuilder [1];
125                                 cattrs [0] = customBuilder;
126                         }
127                 }
128
129 #if NET_2_0
130                 [ComVisible (true)]
131 #endif
132                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
133                         if (con == null)
134                                 throw new ArgumentNullException ("con");
135                         if (binaryAttribute == null)
136                                 throw new ArgumentNullException ("binaryAttribute");
137                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
138                 }
139
140                 private void RejectIfCreated () {
141                         if (typeb.is_created)
142                                 throw new InvalidOperationException ("Type definition of the method is complete.");
143                 }
144
145                 void _EventBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
146                 {
147                         throw new NotImplementedException ();
148                 }
149
150                 void _EventBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
151                 {
152                         throw new NotImplementedException ();
153                 }
154
155                 void _EventBuilder.GetTypeInfoCount (out uint pcTInfo)
156                 {
157                         throw new NotImplementedException ();
158                 }
159
160                 void _EventBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
161                 {
162                         throw new NotImplementedException ();
163                 }
164         }
165 }
166