**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.Reflection.Emit / TypeBuilder.cs
blobb129a0419204f34ffa309af3a08543eab58abb6a
1 //
2 // System.Reflection.Emit/TypeBuilder.cs
3 //
4 // Author:
5 // Paolo Molaro (lupus@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
8 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System;
34 using System.Text;
35 using System.Reflection;
36 using System.Reflection.Emit;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.InteropServices;
39 using System.Globalization;
40 using System.Collections;
41 using System.Security;
42 using System.Security.Permissions;
44 namespace System.Reflection.Emit {
46 public sealed class TypeBuilder : Type {
47 #region Sync with reflection.h
48 private string tname;
49 private string nspace;
50 private Type parent;
51 private Type nesting_type;
52 private Type[] interfaces;
53 private int num_methods;
54 private MethodBuilder[] methods;
55 private ConstructorBuilder[] ctors;
56 private PropertyBuilder[] properties;
57 private int num_fields;
58 private FieldBuilder[] fields;
59 private EventBuilder[] events;
60 private CustomAttributeBuilder[] cattrs;
61 internal TypeBuilder[] subtypes;
62 internal TypeAttributes attrs;
63 private int table_idx;
64 private ModuleBuilder pmodule;
65 private int class_size;
66 private PackingSize packing_size;
67 private IntPtr generic_container;
68 #if NET_2_0 || BOOTSTRAP_NET_2_0
69 private GenericTypeParameterBuilder[] generic_params;
70 #else
71 private Object generic_params; /* so offsets don't change */
72 #endif
73 private RefEmitPermissionSet[] permissions;
74 private Type created;
75 #endregion
76 string fullname;
78 public const int UnspecifiedTypeSize = 0;
80 protected override TypeAttributes GetAttributeFlagsImpl () {
81 return attrs;
84 [MethodImplAttribute(MethodImplOptions.InternalCall)]
85 private extern void setup_internal_class (TypeBuilder tb);
87 [MethodImplAttribute(MethodImplOptions.InternalCall)]
88 private extern void create_internal_class (TypeBuilder tb);
90 [MethodImplAttribute(MethodImplOptions.InternalCall)]
91 private extern void setup_generic_class ();
93 [MethodImplAttribute(MethodImplOptions.InternalCall)]
94 private extern void create_generic_class ();
96 [MethodImplAttribute(MethodImplOptions.InternalCall)]
97 private extern EventInfo get_event_info (EventBuilder eb);
99 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr) {
100 this.parent = null;
101 this.attrs = attr;
102 this.class_size = UnspecifiedTypeSize;
103 this.table_idx = 1;
104 fullname = this.tname = "<Module>";
105 this.nspace = "";
106 pmodule = mb;
107 setup_internal_class (this);
110 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type) {
111 int sep_index;
112 this.parent = parent;
113 this.attrs = attr;
114 this.class_size = type_size;
115 this.packing_size = packing_size;
116 this.nesting_type = nesting_type;
117 sep_index = name.LastIndexOf('.');
118 if (sep_index != -1) {
119 this.tname = name.Substring (sep_index + 1);
120 this.nspace = name.Substring (0, sep_index);
121 } else {
122 this.tname = name;
123 this.nspace = "";
125 if (interfaces != null) {
126 this.interfaces = new Type[interfaces.Length];
127 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
129 pmodule = mb;
130 // skip .<Module> ?
131 table_idx = mb.get_next_table_index (this, 0x02, true);
132 setup_internal_class (this);
133 fullname = GetFullName ();
136 public override Assembly Assembly {
137 get {return pmodule.Assembly;}
140 public override string AssemblyQualifiedName {
141 get {
142 return fullname + ", " + Assembly.GetName().FullName;
145 public override Type BaseType {
146 get {
147 return parent;
150 public override Type DeclaringType {get {return nesting_type;}}
152 /* public override bool IsSubclassOf (Type c)
154 Type t;
155 if (c == null)
156 return false;
157 if (c == this)
158 return false;
159 t = parent;
160 while (t != null) {
161 if (c == t)
162 return true;
163 t = t.BaseType;
165 return false;
168 [MonoTODO]
169 public override Type UnderlyingSystemType {
170 get {
171 // This should return the type itself for non-enum types but
172 // that breaks mcs.
173 if (fields != null) {
174 foreach (FieldBuilder f in fields) {
175 if ((f != null) && (f.Attributes & FieldAttributes.Static) == 0)
176 return f.FieldType;
179 throw new InvalidOperationException ("Underlying type information on enumeration is not specified.");
183 string GetFullName () {
184 if (nesting_type != null)
185 return String.Concat (nesting_type.FullName, "+", tname);
186 if ((nspace != null) && (nspace.Length > 0))
187 return String.Concat (nspace, ".", tname);
188 return tname;
191 public override string FullName {
192 get {
193 return fullname;
197 public override Guid GUID {
198 get {
199 check_created ();
200 return created.GUID;
204 public override Module Module {
205 get {return pmodule;}
207 public override string Name {
208 get {return tname;}
210 public override string Namespace {
211 get {return nspace;}
213 public PackingSize PackingSize {
214 get {return packing_size;}
216 public int Size {
217 get { return class_size; }
219 public override Type ReflectedType {get {return nesting_type;}}
221 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
222 if (pset == null)
223 throw new ArgumentNullException ("pset");
224 if ((action == SecurityAction.RequestMinimum) ||
225 (action == SecurityAction.RequestOptional) ||
226 (action == SecurityAction.RequestRefuse))
227 throw new ArgumentException ("Request* values are not permitted", "action");
229 check_not_created ();
231 if (permissions != null) {
232 /* Check duplicate actions */
233 foreach (RefEmitPermissionSet set in permissions)
234 if (set.action == action)
235 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
237 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
238 permissions.CopyTo (new_array, 0);
239 permissions = new_array;
241 else
242 permissions = new RefEmitPermissionSet [1];
244 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
245 attrs |= TypeAttributes.HasSecurity;
248 public void AddInterfaceImplementation( Type interfaceType) {
249 if (interfaceType == null)
250 throw new ArgumentNullException ("interfaceType");
251 check_not_created ();
253 if (interfaces != null) {
254 // Check for duplicates
255 foreach (Type t in interfaces)
256 if (t == interfaceType)
257 return;
259 Type[] ifnew = new Type [interfaces.Length + 1];
260 interfaces.CopyTo (ifnew, 0);
261 ifnew [interfaces.Length] = interfaceType;
262 interfaces = ifnew;
263 } else {
264 interfaces = new Type [1];
265 interfaces [0] = interfaceType;
269 [MonoTODO]
270 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
271 CallingConventions callConvention, Type[] types,
272 ParameterModifier[] modifiers)
274 if (ctors == null)
275 return null;
277 ConstructorBuilder found = null;
278 int count = 0;
280 foreach (ConstructorBuilder cb in ctors){
281 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
282 continue;
283 found = cb;
284 count++;
287 if (count == 0)
288 return null;
289 if (types == null){
290 if (count > 1)
291 throw new AmbiguousMatchException ();
292 return found;
294 MethodBase[] match = new MethodBase [count];
295 if (count == 1)
296 match [0] = found;
297 else {
298 count = 0;
299 foreach (ConstructorInfo m in ctors) {
300 if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
301 continue;
302 match [count++] = m;
305 if (binder == null)
306 binder = Binder.DefaultBinder;
307 return (ConstructorInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
310 public override bool IsDefined( Type attributeType, bool inherit)
313 * MS throws NotSupported here, but we can't because some corlib
314 * classes make calls to IsDefined.
316 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
319 public override object[] GetCustomAttributes(bool inherit)
321 check_created ();
323 return created.GetCustomAttributes (inherit);
326 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
328 check_created ();
330 return created.GetCustomAttributes (attributeType, inherit);
333 public TypeBuilder DefineNestedType (string name) {
334 return DefineNestedType (name, TypeAttributes.NestedPrivate, pmodule.assemblyb.corlib_object_type, null);
337 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
338 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
341 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
342 return DefineNestedType (name, attr, parent, null);
345 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
346 PackingSize packsize, int typesize)
348 check_name ("name", name);
349 // Visibility must be NestedXXX
350 /* This breaks mcs
351 if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
352 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
353 throw new ArgumentException ("attr", "Bad type flags for nested type.");
355 if (interfaces != null)
356 foreach (Type iface in interfaces)
357 if (iface == null)
358 throw new ArgumentNullException ("interfaces");
360 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize, this);
361 res.fullname = res.GetFullName ();
362 pmodule.RegisterTypeName (res, res.fullname);
363 if (subtypes != null) {
364 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
365 System.Array.Copy (subtypes, new_types, subtypes.Length);
366 new_types [subtypes.Length] = res;
367 subtypes = new_types;
368 } else {
369 subtypes = new TypeBuilder [1];
370 subtypes [0] = res;
372 return res;
375 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
376 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
379 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
380 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
383 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
384 return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
387 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
388 return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
391 #if NET_2_0 || BOOTSTRAP_NET_2_0
392 public
393 #else
394 internal
395 #endif
396 ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
398 check_not_created ();
399 ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
400 if (ctors != null) {
401 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
402 System.Array.Copy (ctors, new_ctors, ctors.Length);
403 new_ctors [ctors.Length] = cb;
404 ctors = new_ctors;
405 } else {
406 ctors = new ConstructorBuilder [1];
407 ctors [0] = cb;
409 return cb;
412 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
414 Type parent_type;
416 if (parent != null)
417 parent_type = parent;
418 else
419 parent_type = pmodule.assemblyb.corlib_object_type;
421 ConstructorInfo parent_constructor =
422 parent_type.GetConstructor (
423 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
424 null, Type.EmptyTypes, null);
425 if (parent_constructor == null) {
426 throw new NotSupportedException ("Parent does"
427 + " not have a default constructor."
428 + " The default constructor must be"
429 + " explicitly defined.");
432 ConstructorBuilder cb = DefineConstructor (attributes,
433 CallingConventions.Standard, new Type[0]);
434 ILGenerator ig = cb.GetILGenerator ();
435 ig.Emit (OpCodes.Ldarg_0);
436 ig.Emit (OpCodes.Call, parent_constructor);
437 ig.Emit (OpCodes.Ret);
438 return cb;
441 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
442 return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
445 private void append_method (MethodBuilder mb) {
446 if (methods != null) {
447 if (methods.Length == num_methods) {
448 MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
449 System.Array.Copy (methods, new_methods, num_methods);
450 methods = new_methods;
452 } else {
453 methods = new MethodBuilder [1];
455 methods [num_methods] = mb;
456 num_methods ++;
459 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
460 return DefineMethod (name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
463 #if NET_2_0 || BOOTSTRAP_NET_2_0
464 public
465 #else
466 internal
467 #endif
468 MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) {
469 check_name ("name", name);
470 check_not_created ();
471 if (IsInterface && (
472 !((attributes & MethodAttributes.Abstract) != 0) ||
473 !((attributes & MethodAttributes.Virtual) != 0)))
474 throw new ArgumentException ("attributes", "Interface method must be abstract and virtual.");
476 if (returnType == null)
477 returnType = pmodule.assemblyb.corlib_void_type;
478 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
479 append_method (res);
480 return res;
483 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
484 return DefinePInvokeMethod (name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
487 #if NET_2_0 || BOOTSTRAP_NET_2_0
488 public
489 #else
490 internal
491 #endif
492 MethodBuilder DefinePInvokeMethod (
493 string name,
494 string dllName,
495 string entryName, MethodAttributes attributes,
496 CallingConventions callingConvention,
497 Type returnType,
498 Type[] returnTypeRequiredCustomModifiers,
499 Type[] returnTypeOptionalCustomModifiers,
500 Type[] parameterTypes,
501 Type[][] parameterTypeRequiredCustomModifiers,
502 Type[][] parameterTypeOptionalCustomModifiers,
503 CallingConvention nativeCallConv,
504 CharSet nativeCharSet) {
505 check_name ("name", name);
506 check_name ("dllName", dllName);
507 check_name ("entryName", entryName);
508 if ((attributes & MethodAttributes.Abstract) != 0)
509 throw new ArgumentException ("attributes", "PInvoke methods must be static and native and cannot be abstract.");
510 if (IsInterface)
511 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
512 check_not_created ();
514 MethodBuilder res
515 = new MethodBuilder (
516 this,
517 name,
518 attributes,
519 callingConvention,
520 returnType,
521 returnTypeRequiredCustomModifiers,
522 returnTypeOptionalCustomModifiers,
523 parameterTypes,
524 parameterTypeRequiredCustomModifiers,
525 parameterTypeOptionalCustomModifiers,
526 dllName,
527 entryName,
528 nativeCallConv,
529 nativeCharSet);
530 append_method (res);
531 return res;
534 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
535 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
536 nativeCallConv, nativeCharSet);
539 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
540 if (methodInfoBody == null)
541 throw new ArgumentNullException ("methodInfoBody");
542 if (methodInfoDeclaration == null)
543 throw new ArgumentNullException ("methodInfoDeclaration");
544 check_not_created ();
546 if (methodInfoBody is MethodBuilder) {
547 MethodBuilder mb = (MethodBuilder)methodInfoBody;
548 mb.set_override (methodInfoDeclaration);
552 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
553 return DefineField (fieldName, type, null, null, attributes);
556 #if NET_2_0 || BOOTSTRAP_NET_2_0
557 public
558 #else
559 internal
560 #endif
561 FieldBuilder DefineField (string fieldName, Type type, Type[] requiredCustomAttributes, Type[] optionalCustomAttributes, FieldAttributes attributes) {
562 check_name ("fieldName", fieldName);
563 if (type == typeof (void))
564 throw new ArgumentException ("type", "Bad field type in defining field.");
565 check_not_created ();
567 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomAttributes, optionalCustomAttributes);
568 if (fields != null) {
569 if (fields.Length == num_fields) {
570 FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
571 System.Array.Copy (fields, new_fields, num_fields);
572 fields = new_fields;
574 fields [num_fields] = res;
575 num_fields ++;
576 } else {
577 fields = new FieldBuilder [1];
578 fields [0] = res;
579 num_fields ++;
580 create_internal_class (this);
582 return res;
585 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
586 check_name ("name", name);
587 if (parameterTypes != null)
588 foreach (Type param in parameterTypes)
589 if (param == null)
590 throw new ArgumentNullException ("parameterTypes");
591 check_not_created ();
593 PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
595 if (properties != null) {
596 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
597 System.Array.Copy (properties, new_properties, properties.Length);
598 new_properties [properties.Length] = res;
599 properties = new_properties;
600 } else {
601 properties = new PropertyBuilder [1];
602 properties [0] = res;
604 return res;
607 public ConstructorBuilder DefineTypeInitializer() {
608 return DefineConstructor (MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, null);
611 [MethodImplAttribute(MethodImplOptions.InternalCall)]
612 private extern Type create_runtime_class (TypeBuilder tb);
614 private bool is_nested_in (Type t) {
615 while (t != null) {
616 if (t == this)
617 return true;
618 else
619 t = t.DeclaringType;
621 return false;
624 public Type CreateType() {
625 /* handle nesting_type */
626 check_not_created ();
628 // Fire TypeResolve events for fields whose type is an unfinished
629 // value type.
630 if (fields != null) {
631 foreach (FieldBuilder fb in fields) {
632 if (fb == null)
633 continue;
634 Type ft = fb.FieldType;
635 if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
636 TypeBuilder tb = (TypeBuilder)ft;
637 if (!tb.is_created) {
638 AppDomain.CurrentDomain.DoTypeResolve (tb);
639 if (!tb.is_created) {
640 // FIXME: We should throw an exception here,
641 // but mcs expects that the type is created
642 // even if the exception is thrown
643 //throw new TypeLoadException ("Could not load type " + tb);
650 if (methods != null) {
651 for (int i = 0; i < num_methods; ++i)
652 ((MethodBuilder)(methods[i])).fixup ();
656 // On classes, define a default constructor if not provided
658 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>") &&
659 (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed))
660 DefineDefaultConstructor (MethodAttributes.Public);
662 if (ctors != null){
663 foreach (ConstructorBuilder ctor in ctors)
664 ctor.fixup ();
667 created = create_runtime_class (this);
668 if (created != null)
669 return created;
670 return this;
673 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
675 if (ctors == null)
676 return new ConstructorInfo [0];
677 ArrayList l = new ArrayList ();
678 bool match;
679 MethodAttributes mattrs;
681 foreach (ConstructorBuilder c in ctors) {
682 match = false;
683 mattrs = c.Attributes;
684 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
685 if ((bindingAttr & BindingFlags.Public) != 0)
686 match = true;
687 } else {
688 if ((bindingAttr & BindingFlags.NonPublic) != 0)
689 match = true;
691 if (!match)
692 continue;
693 match = false;
694 if ((mattrs & MethodAttributes.Static) != 0) {
695 if ((bindingAttr & BindingFlags.Static) != 0)
696 match = true;
697 } else {
698 if ((bindingAttr & BindingFlags.Instance) != 0)
699 match = true;
701 if (!match)
702 continue;
703 l.Add (c);
705 ConstructorInfo[] result = new ConstructorInfo [l.Count];
706 l.CopyTo (result);
707 return result;
710 public override Type GetElementType () {
711 check_created ();
712 return created.GetElementType ();
715 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
716 check_created ();
717 return created.GetEvent (name, bindingAttr);
720 /* Needed to keep signature compatibility with MS.NET */
721 public override EventInfo[] GetEvents ()
723 return GetEvents (DefaultBindingFlags);
726 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
727 /* FIXME: mcs calls this
728 check_created ();
730 if (!is_created)
731 return new EventInfo [0];
732 else
733 return created.GetEvents (bindingAttr);
736 // This is only used from MonoGenericInst.initialize().
737 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
739 if (events == null)
740 return new EventInfo [0];
741 ArrayList l = new ArrayList ();
742 bool match;
743 MethodAttributes mattrs;
744 MethodInfo accessor;
746 foreach (EventBuilder eb in events) {
747 if (eb == null)
748 continue;
749 EventInfo c = get_event_info (eb);
750 match = false;
751 accessor = c.GetAddMethod (true);
752 if (accessor == null)
753 accessor = c.GetRemoveMethod (true);
754 if (accessor == null)
755 continue;
756 mattrs = accessor.Attributes;
757 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
758 if ((bindingAttr & BindingFlags.Public) != 0)
759 match = true;
760 } else {
761 if ((bindingAttr & BindingFlags.NonPublic) != 0)
762 match = true;
764 if (!match)
765 continue;
766 match = false;
767 if ((mattrs & MethodAttributes.Static) != 0) {
768 if ((bindingAttr & BindingFlags.Static) != 0)
769 match = true;
770 } else {
771 if ((bindingAttr & BindingFlags.Instance) != 0)
772 match = true;
774 if (!match)
775 continue;
776 l.Add (c);
778 EventInfo[] result = new EventInfo [l.Count];
779 l.CopyTo (result);
780 return result;
783 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
784 if (fields == null)
785 return null;
787 bool match;
788 FieldAttributes mattrs;
790 foreach (FieldInfo c in fields) {
791 if (c == null)
792 continue;
793 if (c.Name != name)
794 continue;
795 match = false;
796 mattrs = c.Attributes;
797 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
798 if ((bindingAttr & BindingFlags.Public) != 0)
799 match = true;
800 } else {
801 if ((bindingAttr & BindingFlags.NonPublic) != 0)
802 match = true;
804 if (!match)
805 continue;
806 match = false;
807 if ((mattrs & FieldAttributes.Static) != 0) {
808 if ((bindingAttr & BindingFlags.Static) != 0)
809 match = true;
810 } else {
811 if ((bindingAttr & BindingFlags.Instance) != 0)
812 match = true;
814 if (!match)
815 continue;
816 return c;
818 return null;
821 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
822 if (fields == null)
823 return new FieldInfo [0];
824 ArrayList l = new ArrayList ();
825 bool match;
826 FieldAttributes mattrs;
828 foreach (FieldInfo c in fields) {
829 if (c == null)
830 continue;
831 match = false;
832 mattrs = c.Attributes;
833 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
834 if ((bindingAttr & BindingFlags.Public) != 0)
835 match = true;
836 } else {
837 if ((bindingAttr & BindingFlags.NonPublic) != 0)
838 match = true;
840 if (!match)
841 continue;
842 match = false;
843 if ((mattrs & FieldAttributes.Static) != 0) {
844 if ((bindingAttr & BindingFlags.Static) != 0)
845 match = true;
846 } else {
847 if ((bindingAttr & BindingFlags.Instance) != 0)
848 match = true;
850 if (!match)
851 continue;
852 l.Add (c);
854 FieldInfo[] result = new FieldInfo [l.Count];
855 l.CopyTo (result);
856 return result;
859 public override Type GetInterface (string name, bool ignoreCase) {
860 check_created ();
861 return created.GetInterface (name, ignoreCase);
864 public override Type[] GetInterfaces () {
865 if (interfaces != null) {
866 Type[] ret = new Type [interfaces.Length];
867 interfaces.CopyTo (ret, 0);
868 return ret;
869 } else {
870 return Type.EmptyTypes;
874 public override MemberInfo[] GetMember (string name, MemberTypes type,
875 BindingFlags bindingAttr) {
876 check_created ();
877 return created.GetMember (name, type, bindingAttr);
880 public override MemberInfo[] GetMembers (BindingFlags bindingAttr) {
881 check_created ();
882 return created.GetMembers (bindingAttr);
885 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type) {
886 MethodInfo[] candidates;
887 if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
888 MethodInfo[] parent_methods = parent.GetMethods (bindingAttr);
889 if (methods == null)
890 candidates = parent_methods;
891 else {
892 candidates = new MethodInfo [methods.Length + parent_methods.Length];
893 parent_methods.CopyTo (candidates, 0);
894 methods.CopyTo (candidates, parent_methods.Length);
897 else
898 candidates = methods;
900 if (candidates == null)
901 return new MethodInfo [0];
903 ArrayList l = new ArrayList ();
904 bool match;
905 MethodAttributes mattrs;
907 foreach (MethodInfo c in candidates) {
908 if (c == null)
909 continue;
910 if (name != null) {
911 if (String.Compare (c.Name, name, ignoreCase) != 0)
912 continue;
914 match = false;
915 mattrs = c.Attributes;
916 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
917 if ((bindingAttr & BindingFlags.Public) != 0)
918 match = true;
919 } else {
920 if ((bindingAttr & BindingFlags.NonPublic) != 0)
921 match = true;
923 if (!match)
924 continue;
925 match = false;
926 if ((mattrs & MethodAttributes.Static) != 0) {
927 if ((bindingAttr & BindingFlags.Static) != 0)
928 match = true;
929 } else {
930 if ((bindingAttr & BindingFlags.Instance) != 0)
931 match = true;
933 if (!match)
934 continue;
935 l.Add (c);
938 MethodInfo[] result = new MethodInfo [l.Count];
939 l.CopyTo (result);
940 return result;
943 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
944 return GetMethodsByName (null, bindingAttr, false, this);
947 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
948 Binder binder,
949 CallingConventions callConvention,
950 Type[] types, ParameterModifier[] modifiers)
952 check_created ();
954 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
955 MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
956 MethodInfo found = null;
957 MethodBase[] match;
958 int typesLen = (types != null) ? types.Length : 0;
959 int count = 0;
961 foreach (MethodInfo m in methods) {
962 // Under MS.NET, Standard|HasThis matches Standard...
963 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
964 continue;
965 found = m;
966 count++;
969 if (count == 0)
970 return null;
972 if (count == 1 && typesLen == 0)
973 return found;
975 match = new MethodBase [count];
976 if (count == 1)
977 match [0] = found;
978 else {
979 count = 0;
980 foreach (MethodInfo m in methods) {
981 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
982 continue;
983 match [count++] = m;
987 if (types == null)
988 return (MethodInfo) Binder.FindMostDerivedMatch (match);
990 if (binder == null)
991 binder = Binder.DefaultBinder;
993 return (MethodInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
996 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
997 check_created ();
998 return created.GetNestedType (name, bindingAttr);
1001 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
1002 bool match;
1003 ArrayList result = new ArrayList ();
1005 if (subtypes == null)
1006 return Type.EmptyTypes;
1007 foreach (TypeBuilder t in subtypes) {
1008 match = false;
1009 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1010 if ((bindingAttr & BindingFlags.Public) != 0)
1011 match = true;
1012 } else {
1013 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1014 match = true;
1016 if (!match)
1017 continue;
1018 result.Add (t);
1020 Type[] r = new Type [result.Count];
1021 result.CopyTo (r);
1022 return r;
1025 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
1026 if (properties == null)
1027 return new PropertyInfo [0];
1028 ArrayList l = new ArrayList ();
1029 bool match;
1030 MethodAttributes mattrs;
1031 MethodInfo accessor;
1033 foreach (PropertyInfo c in properties) {
1034 match = false;
1035 accessor = c.GetGetMethod (true);
1036 if (accessor == null)
1037 accessor = c.GetSetMethod (true);
1038 if (accessor == null)
1039 continue;
1040 mattrs = accessor.Attributes;
1041 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1042 if ((bindingAttr & BindingFlags.Public) != 0)
1043 match = true;
1044 } else {
1045 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1046 match = true;
1048 if (!match)
1049 continue;
1050 match = false;
1051 if ((mattrs & MethodAttributes.Static) != 0) {
1052 if ((bindingAttr & BindingFlags.Static) != 0)
1053 match = true;
1054 } else {
1055 if ((bindingAttr & BindingFlags.Instance) != 0)
1056 match = true;
1058 if (!match)
1059 continue;
1060 l.Add (c);
1062 PropertyInfo[] result = new PropertyInfo [l.Count];
1063 l.CopyTo (result);
1064 return result;
1067 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
1068 throw not_supported ();
1071 protected override bool HasElementTypeImpl () {
1072 check_created ();
1073 return created.HasElementType;
1076 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
1077 check_created ();
1078 return created.InvokeMember (name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
1081 protected override bool IsArrayImpl ()
1083 return Type.IsArrayImpl (this);
1086 protected override bool IsByRefImpl () {
1087 // FIXME
1088 return false;
1090 protected override bool IsCOMObjectImpl () {
1091 return false;
1093 protected override bool IsPointerImpl () {
1094 // FIXME
1095 return false;
1097 protected override bool IsPrimitiveImpl () {
1098 // FIXME
1099 return false;
1101 protected override bool IsValueTypeImpl () {
1102 return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
1103 this != pmodule.assemblyb.corlib_value_type &&
1104 this != pmodule.assemblyb.corlib_enum_type);
1107 public override RuntimeTypeHandle TypeHandle {
1108 get {
1109 check_created ();
1110 return created.TypeHandle;
1114 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
1115 if (customBuilder == null)
1116 throw new ArgumentNullException ("customBuilder");
1118 string attrname = customBuilder.Ctor.ReflectedType.FullName;
1119 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1120 byte[] data = customBuilder.Data;
1121 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1122 layout_kind = (int)data [2];
1123 layout_kind |= ((int)data [3]) << 8;
1124 attrs &= ~TypeAttributes.LayoutMask;
1125 switch ((LayoutKind)layout_kind) {
1126 case LayoutKind.Auto:
1127 attrs |= TypeAttributes.AutoLayout;
1128 break;
1129 case LayoutKind.Explicit:
1130 attrs |= TypeAttributes.ExplicitLayout;
1131 break;
1132 case LayoutKind.Sequential:
1133 attrs |= TypeAttributes.SequentialLayout;
1134 break;
1135 default:
1136 // we should ignore it since it can be any value anyway...
1137 throw new Exception ("Error in customattr");
1139 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
1140 int pos = 6;
1141 if (first_type_name == "System.Int16")
1142 pos = 4;
1143 int nnamed = (int)data [pos++];
1144 nnamed |= ((int)data [pos++]) << 8;
1145 for (int i = 0; i < nnamed; ++i) {
1146 byte named_type = data [pos++];
1147 byte type = data [pos++];
1148 int len;
1149 string named_name;
1151 if (type == 0x55) {
1152 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1153 string named_typename = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1154 pos += len;
1155 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1156 // See related code/FIXME in mono/mono/metadata/reflection.c
1159 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1160 named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1161 pos += len;
1162 /* all the fields are integers in StructLayout */
1163 int value = (int)data [pos++];
1164 value |= ((int)data [pos++]) << 8;
1165 value |= ((int)data [pos++]) << 16;
1166 value |= ((int)data [pos++]) << 24;
1167 switch (named_name) {
1168 case "CharSet":
1169 switch ((CharSet)value) {
1170 case CharSet.None:
1171 case CharSet.Ansi:
1172 break;
1173 case CharSet.Unicode:
1174 attrs |= TypeAttributes.UnicodeClass;
1175 break;
1176 case CharSet.Auto:
1177 attrs |= TypeAttributes.AutoClass;
1178 break;
1179 default:
1180 break; // error out...
1182 break;
1183 case "Pack":
1184 packing_size = (PackingSize)value;
1185 break;
1186 case "Size":
1187 class_size = value;
1188 break;
1189 default:
1190 break; // error out...
1193 return;
1194 } else if (attrname == "System.SerializableAttribute") {
1195 attrs |= TypeAttributes.Serializable;
1196 return;
1198 if (cattrs != null) {
1199 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1200 cattrs.CopyTo (new_array, 0);
1201 new_array [cattrs.Length] = customBuilder;
1202 cattrs = new_array;
1203 } else {
1204 cattrs = new CustomAttributeBuilder [1];
1205 cattrs [0] = customBuilder;
1208 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
1209 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1212 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
1213 check_name ("name", name);
1214 if (eventtype == null)
1215 throw new ArgumentNullException ("eventtype");
1216 check_not_created ();
1218 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1219 if (events != null) {
1220 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1221 System.Array.Copy (events, new_events, events.Length);
1222 new_events [events.Length] = res;
1223 events = new_events;
1224 } else {
1225 events = new EventBuilder [1];
1226 events [0] = res;
1228 return res;
1231 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
1232 if (data == null)
1233 throw new ArgumentNullException ("data");
1234 if ((data.Length == 0) || (data.Length > 0x3f0000))
1235 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
1237 FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1238 res.SetRVAData (data);
1240 return res;
1243 static int UnmanagedDataCount = 0;
1245 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
1246 check_name ("name", name);
1247 if ((size <= 0) || (size > 0x3f0000))
1248 throw new ArgumentException ("size", "Data size must be > 0 and < 0x3f0000");
1249 check_not_created ();
1251 string s = "$ArrayType$"+UnmanagedDataCount.ToString();
1252 UnmanagedDataCount++;
1253 TypeBuilder datablobtype = DefineNestedType (s,
1254 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1255 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1256 datablobtype.CreateType ();
1257 return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1260 public TypeToken TypeToken {
1261 get {
1262 return new TypeToken (0x02000000 | table_idx);
1265 public void SetParent (Type parentType) {
1266 if (parentType == null)
1267 throw new ArgumentNullException ("parentType");
1268 check_not_created ();
1270 parent = parentType;
1271 // will just set the parent-related bits if called a second time
1272 setup_internal_class (this);
1274 internal int get_next_table_index (object obj, int table, bool inc) {
1275 return pmodule.get_next_table_index (obj, table, inc);
1278 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1280 if (created == null)
1281 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1283 return created.GetInterfaceMap (interfaceType);
1286 internal bool is_created {
1287 get {
1288 return created != null;
1292 private Exception not_supported ()
1294 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1297 private void check_not_created ()
1299 if (is_created)
1300 throw new InvalidOperationException ("Unable to change after type has been created.");
1303 private void check_created ()
1305 if (!is_created)
1306 throw not_supported ();
1309 private void check_name (string argName, string name)
1311 if (name == null)
1312 throw new ArgumentNullException (argName);
1313 if (name == "")
1314 throw new ArgumentException (argName, "Empty name is not legal.");
1315 if (name.IndexOf ((char)0) != -1)
1316 throw new ArgumentException (argName, "Illegal name.");
1319 public override String ToString ()
1321 return FullName;
1324 [MonoTODO]
1325 public override bool IsAssignableFrom (Type c)
1327 return base.IsAssignableFrom (c);
1330 [MonoTODO]
1331 public override bool IsSubclassOf (Type c)
1333 return base.IsSubclassOf (c);
1336 #if NET_2_0 || BOOTSTRAP_NET_2_0
1337 public bool IsCreated () {
1338 return is_created;
1341 public override Type[] GetGenericArguments ()
1343 if (generic_params != null)
1344 return generic_params;
1346 throw new InvalidOperationException ();
1349 public override Type GetGenericTypeDefinition ()
1351 create_generic_class ();
1353 return base.GetGenericTypeDefinition ();
1356 public override bool HasGenericArguments {
1357 get {
1358 throw new NotImplementedException ();
1362 public override bool ContainsGenericParameters {
1363 get {
1364 return generic_params != null;
1368 public extern override bool IsGenericParameter {
1369 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1370 get;
1373 public override int GenericParameterPosition {
1374 get {
1375 throw new NotImplementedException ();
1379 public override MethodInfo DeclaringMethod {
1380 get {
1381 throw new NotImplementedException ();
1385 public GenericTypeParameterBuilder[] DefineGenericParameters (string[] names)
1387 setup_generic_class ();
1389 generic_params = new GenericTypeParameterBuilder [names.Length];
1390 for (int i = 0; i < names.Length; i++)
1391 generic_params [i] = new GenericTypeParameterBuilder (
1392 this, null, names [i], i);
1394 return generic_params;
1397 public MethodBuilder DefineGenericMethod (string name, MethodAttributes attributes)
1399 return DefineMethod (name, attributes, CallingConventions.Standard, null, null);
1401 #endif