New tests.
[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                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
168                 private extern void setup_enum_type (Type t);
169
170                 public FieldBuilder DefineLiteral (string literalName, object literalValue)
171                 {
172                         Type fieldType = this;
173                         FieldBuilder fieldBuilder = _tb.DefineField (literalName, 
174                                 fieldType, (FieldAttributes.Literal | 
175                                 (FieldAttributes.Static | FieldAttributes.Public)));
176                         fieldBuilder.SetConstant (literalValue);
177                         return fieldBuilder;
178                 }
179
180                 protected override TypeAttributes GetAttributeFlagsImpl ()
181                 {
182                         return _tb.attrs;
183                 }
184
185                 protected override ConstructorInfo GetConstructorImpl (
186                         BindingFlags bindingAttr, Binder binder, CallingConventions callConvention,
187                         Type[] types, ParameterModifier[] modifiers)
188                 {
189                         return _tb.GetConstructor (bindingAttr, binder, callConvention, types, 
190                                 modifiers);
191                 }
192
193                 [ComVisible (true)]
194                 public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
195                 {
196                         return _tb.GetConstructors (bindingAttr);
197                 }
198
199                 public override object[] GetCustomAttributes(bool inherit)
200                 {
201                         return _tb.GetCustomAttributes (inherit);
202                 }
203
204                 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
205                 {
206                         return _tb.GetCustomAttributes (attributeType, inherit);
207                 }
208
209                 public override Type GetElementType()
210                 {
211                         return _tb.GetElementType ();
212                 }
213
214                 public override EventInfo GetEvent( string name, BindingFlags bindingAttr)
215                 {
216                         return _tb.GetEvent (name, bindingAttr);
217                 }
218
219                 public override EventInfo[] GetEvents()
220                 {
221                         return _tb.GetEvents ();
222                 }
223
224                 public override EventInfo[] GetEvents( BindingFlags bindingAttr)
225                 {
226                         return _tb.GetEvents (bindingAttr);
227                 }
228
229                 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
230                 {
231                         return _tb.GetField (name, bindingAttr);
232                 }
233
234                 public override FieldInfo[] GetFields( BindingFlags bindingAttr)
235                 {
236                         return _tb.GetFields (bindingAttr);
237                 }
238
239                 public override Type GetInterface (string name, bool ignoreCase)
240                 {
241                         return _tb.GetInterface (name, ignoreCase);
242                 }
243
244                 [ComVisible (true)]
245                 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
246                 {
247                         return _tb.GetInterfaceMap (interfaceType);
248                 }
249
250                 public override Type[] GetInterfaces()
251                 {
252                         return _tb.GetInterfaces ();
253                 }
254
255                 public override MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
256                 {
257                         return _tb.GetMember (name, type, bindingAttr);
258                 }
259
260                 public override MemberInfo[] GetMembers(BindingFlags bindingAttr)
261                 {
262                         return _tb.GetMembers (bindingAttr);
263                 }
264
265                 protected override MethodInfo GetMethodImpl (
266                         string name, BindingFlags bindingAttr, Binder binder,
267                         CallingConventions callConvention, Type[] types,
268                         ParameterModifier[] modifiers)
269                 {
270                         if (types == null) {
271                                 return _tb.GetMethod (name, bindingAttr);
272                         }
273
274                         return _tb.GetMethod (name, bindingAttr, binder, 
275                                 callConvention, types, modifiers);
276                 }
277
278                 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
279                 {
280                         return _tb.GetMethods (bindingAttr);
281                 }
282
283                 public override Type GetNestedType (string name, BindingFlags bindingAttr)
284                 {
285                         return _tb.GetNestedType (name, bindingAttr);
286                 }
287
288                 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
289                 {
290                         return _tb.GetNestedTypes (bindingAttr);
291                 }
292
293                 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
294                 {
295                         return _tb.GetProperties (bindingAttr);
296                 }
297
298                 protected override PropertyInfo GetPropertyImpl (
299                         string name, BindingFlags bindingAttr, Binder binder,
300                         Type returnType, Type[] types,
301                         ParameterModifier[] modifiers)
302                 {
303                         throw CreateNotSupportedException ();
304                 }
305
306                 protected override bool HasElementTypeImpl ()
307                 {
308                         return _tb.HasElementType;
309                 }
310
311                 public override object InvokeMember (
312                         string name, BindingFlags invokeAttr, Binder binder,
313                         object target, object[] args,
314                         ParameterModifier[] modifiers, CultureInfo culture,
315                         string[] namedParameters)
316                 {
317                         return _tb.InvokeMember (name, invokeAttr, binder, target, 
318                                 args, modifiers, culture, namedParameters);
319                 }
320
321                 protected override bool IsArrayImpl()
322                 {
323                         return false;
324                 }
325
326                 protected override bool IsByRefImpl()
327                 {
328                         return false;
329                 }
330
331                 protected override bool IsCOMObjectImpl()
332                 {
333                         return false;
334                 }
335
336                 protected override bool IsPointerImpl()
337                 {
338                         return false;
339                 }
340
341                 protected override bool IsPrimitiveImpl()
342                 {
343                         return false;
344                 }
345
346                 protected override bool IsValueTypeImpl()
347                 {
348                         return true;
349                 }
350
351                 public override bool IsDefined (Type attributeType, bool inherit)
352                 {
353                         return _tb.IsDefined (attributeType, inherit);
354                 }
355
356                 public override Type MakeArrayType ()
357                 {
358                         return  new ArrayType (this, 0);
359                 }
360
361                 public override Type MakeArrayType (int rank)
362                 {
363                         if (rank < 1)
364                                 throw new IndexOutOfRangeException ();
365                         return new ArrayType (this, rank);
366                 }
367
368                 public override Type MakeByRefType ()
369                 {
370                         return new ByRefType (this);
371                 }
372
373                 public override Type MakePointerType ()
374                 {
375                         return new PointerType (this);
376                 }
377
378                 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
379                 {
380                         _tb.SetCustomAttribute (customBuilder);
381                 }
382
383                 [ComVisible (true)]
384                 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
385                 {
386                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
387                 }
388
389                 private Exception CreateNotSupportedException ()
390                 {
391                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
392                 }
393
394                 void _EnumBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
395                 {
396                         throw new NotImplementedException ();
397                 }
398
399                 void _EnumBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
400                 {
401                         throw new NotImplementedException ();
402                 }
403
404                 void _EnumBuilder.GetTypeInfoCount (out uint pcTInfo)
405                 {
406                         throw new NotImplementedException ();
407                 }
408
409                 void _EnumBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
410                 {
411                         throw new NotImplementedException ();
412                 }
413         }
414 }