2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / EnumBuilder.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/EnumBuilder.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         [ComVisible (true)]
43         [ComDefaultInterface (typeof (_EnumBuilder))]
44         [ClassInterface (ClassInterfaceType.None)]
45         public sealed class EnumBuilder : Type, _EnumBuilder {
46                 private TypeBuilder _tb;
47                 private FieldBuilder _underlyingField;
48                 private Type _underlyingType;
49
50                 internal EnumBuilder (ModuleBuilder mb, string name, TypeAttributes visibility, Type underlyingType)
51                 {
52                         _tb = new TypeBuilder (mb, name, (visibility | TypeAttributes.Sealed), 
53                                 typeof(Enum), null, PackingSize.Unspecified, 0, null);
54                         _underlyingType = underlyingType;
55                         _underlyingField = _tb.DefineField ("value__", underlyingType,
56                                 (FieldAttributes.SpecialName | FieldAttributes.Private | FieldAttributes.RTSpecialName));
57                         setup_enum_type (_tb);
58                 }
59
60                 internal TypeBuilder GetTypeBuilder ()
61                 {
62                         return _tb;
63                 }
64
65                 internal override bool IsCompilerContext {
66                         get {
67                                 return _tb.IsCompilerContext;
68                         }
69                 }
70
71                 public override Assembly Assembly {
72                         get {
73                                 return _tb.Assembly;
74                         }
75                 }
76
77                 public override string AssemblyQualifiedName {
78                         get {
79                                 return _tb.AssemblyQualifiedName;
80                         }
81                 }
82
83                 public override Type BaseType {
84                         get {
85                                 return _tb.BaseType;
86                         }
87                 }
88
89                 public override Type DeclaringType {
90                         get {
91                                 return _tb.DeclaringType;
92                         }
93                 }
94
95                 public override string FullName {
96                         get {
97                                 return _tb.FullName;
98                         }
99                 }
100
101                 public override Guid GUID {
102                         get {
103                                 return _tb.GUID;
104                         }
105                 }
106
107                 public override Module Module {
108                         get {
109                                 return _tb.Module;
110                         }
111                 }
112
113                 public override string Name {
114                         get {
115                                 return _tb.Name;
116                         }
117                 }
118
119                 public override string Namespace {
120                         get { 
121                                 return _tb.Namespace;
122                         }
123                 }
124
125                 public override Type ReflectedType {
126                         get {
127                                 return _tb.ReflectedType;
128                         }
129                 }
130
131                 public override RuntimeTypeHandle TypeHandle {
132                         get {
133                                 return _tb.TypeHandle;
134                         }
135                 }
136
137                 public TypeToken TypeToken {
138                         get {
139                                 return _tb.TypeToken;
140                         }
141                 }
142
143                 public FieldBuilder UnderlyingField {
144                         get {
145                                 return _underlyingField;
146                         }
147                 }
148
149                 public override Type UnderlyingSystemType {
150                         get {
151                                 return _underlyingType;
152                         }
153                 }
154
155                 public Type CreateType ()
156                 {
157                         Type res = _tb.CreateType ();
158                         return res;
159                 }
160
161                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
162                 private extern void setup_enum_type (Type t);
163
164                 public FieldBuilder DefineLiteral (string literalName, object literalValue)
165                 {
166                         Type fieldType = this;
167                         FieldBuilder fieldBuilder = _tb.DefineField (literalName, 
168                                 fieldType, (FieldAttributes.Literal | 
169                                 (FieldAttributes.Static | FieldAttributes.Public)));
170                         fieldBuilder.SetConstant (literalValue);
171                         return fieldBuilder;
172                 }
173
174                 protected override TypeAttributes GetAttributeFlagsImpl ()
175                 {
176                         return _tb.attrs;
177                 }
178
179                 protected override ConstructorInfo GetConstructorImpl (
180                         BindingFlags bindingAttr, Binder binder, CallingConventions callConvention,
181                         Type[] types, ParameterModifier[] modifiers)
182                 {
183                         return _tb.GetConstructor (bindingAttr, binder, callConvention, types, 
184                                 modifiers);
185                 }
186
187                 [ComVisible (true)]
188                 public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
189                 {
190                         return _tb.GetConstructors (bindingAttr);
191                 }
192
193                 public override object[] GetCustomAttributes(bool inherit)
194                 {
195                         return _tb.GetCustomAttributes (inherit);
196                 }
197
198                 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
199                 {
200                         return _tb.GetCustomAttributes (attributeType, inherit);
201                 }
202
203                 public override Type GetElementType()
204                 {
205                         return _tb.GetElementType ();
206                 }
207
208                 public override EventInfo GetEvent( string name, BindingFlags bindingAttr)
209                 {
210                         return _tb.GetEvent (name, bindingAttr);
211                 }
212
213                 public override EventInfo[] GetEvents()
214                 {
215                         return _tb.GetEvents ();
216                 }
217
218                 public override EventInfo[] GetEvents( BindingFlags bindingAttr)
219                 {
220                         return _tb.GetEvents (bindingAttr);
221                 }
222
223                 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
224                 {
225                         return _tb.GetField (name, bindingAttr);
226                 }
227
228                 public override FieldInfo[] GetFields( BindingFlags bindingAttr)
229                 {
230                         return _tb.GetFields (bindingAttr);
231                 }
232
233                 public override Type GetInterface (string name, bool ignoreCase)
234                 {
235                         return _tb.GetInterface (name, ignoreCase);
236                 }
237
238                 [ComVisible (true)]
239                 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
240                 {
241                         return _tb.GetInterfaceMap (interfaceType);
242                 }
243
244                 public override Type[] GetInterfaces()
245                 {
246                         return _tb.GetInterfaces ();
247                 }
248
249                 public override MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
250                 {
251                         return _tb.GetMember (name, type, bindingAttr);
252                 }
253
254                 public override MemberInfo[] GetMembers(BindingFlags bindingAttr)
255                 {
256                         return _tb.GetMembers (bindingAttr);
257                 }
258
259                 protected override MethodInfo GetMethodImpl (
260                         string name, BindingFlags bindingAttr, Binder binder,
261                         CallingConventions callConvention, Type[] types,
262                         ParameterModifier[] modifiers)
263                 {
264                         if (types == null) {
265                                 return _tb.GetMethod (name, bindingAttr);
266                         }
267
268                         return _tb.GetMethod (name, bindingAttr, binder, 
269                                 callConvention, types, modifiers);
270                 }
271
272                 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
273                 {
274                         return _tb.GetMethods (bindingAttr);
275                 }
276
277                 public override Type GetNestedType (string name, BindingFlags bindingAttr)
278                 {
279                         return _tb.GetNestedType (name, bindingAttr);
280                 }
281
282                 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
283                 {
284                         return _tb.GetNestedTypes (bindingAttr);
285                 }
286
287                 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
288                 {
289                         return _tb.GetProperties (bindingAttr);
290                 }
291
292                 protected override PropertyInfo GetPropertyImpl (
293                         string name, BindingFlags bindingAttr, Binder binder,
294                         Type returnType, Type[] types,
295                         ParameterModifier[] modifiers)
296                 {
297                         throw CreateNotSupportedException ();
298                 }
299
300                 protected override bool HasElementTypeImpl ()
301                 {
302                         return _tb.HasElementType;
303                 }
304
305                 public override object InvokeMember (
306                         string name, BindingFlags invokeAttr, Binder binder,
307                         object target, object[] args,
308                         ParameterModifier[] modifiers, CultureInfo culture,
309                         string[] namedParameters)
310                 {
311                         return _tb.InvokeMember (name, invokeAttr, binder, target, 
312                                 args, modifiers, culture, namedParameters);
313                 }
314
315                 protected override bool IsArrayImpl()
316                 {
317                         return false;
318                 }
319
320                 protected override bool IsByRefImpl()
321                 {
322                         return false;
323                 }
324
325                 protected override bool IsCOMObjectImpl()
326                 {
327                         return false;
328                 }
329
330                 protected override bool IsPointerImpl()
331                 {
332                         return false;
333                 }
334
335                 protected override bool IsPrimitiveImpl()
336                 {
337                         return false;
338                 }
339
340                 protected override bool IsValueTypeImpl()
341                 {
342                         return true;
343                 }
344
345                 public override bool IsDefined (Type attributeType, bool inherit)
346                 {
347                         return _tb.IsDefined (attributeType, inherit);
348                 }
349
350                 public override Type MakeArrayType ()
351                 {
352                         return  new ArrayType (this, 0);
353                 }
354
355                 public override Type MakeArrayType (int rank)
356                 {
357                         if (rank < 1)
358                                 throw new IndexOutOfRangeException ();
359                         return new ArrayType (this, rank);
360                 }
361
362                 public override Type MakeByRefType ()
363                 {
364                         return new ByRefType (this);
365                 }
366
367                 public override Type MakePointerType ()
368                 {
369                         return new PointerType (this);
370                 }
371
372                 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
373                 {
374                         _tb.SetCustomAttribute (customBuilder);
375                 }
376
377                 [ComVisible (true)]
378                 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
379                 {
380                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
381                 }
382
383                 private Exception CreateNotSupportedException ()
384                 {
385                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
386                 }
387
388                 void _EnumBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
389                 {
390                         throw new NotImplementedException ();
391                 }
392
393                 void _EnumBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
394                 {
395                         throw new NotImplementedException ();
396                 }
397
398                 void _EnumBuilder.GetTypeInfoCount (out uint pcTInfo)
399                 {
400                         throw new NotImplementedException ();
401                 }
402
403                 void _EnumBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
404                 {
405                         throw new NotImplementedException ();
406                 }
407         }
408 }