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