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