2 // System.Reflection.Emit.TypeBuilder.cs
5 // Paolo Molaro (lupus@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // (C) 2001 Ximian, Inc. http://www.ximian.com
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System
.Reflection
;
37 using System
.Reflection
.Emit
;
38 using System
.Runtime
.CompilerServices
;
39 using System
.Runtime
.InteropServices
;
40 using System
.Globalization
;
41 using System
.Collections
;
42 using System
.Security
;
43 using System
.Security
.Permissions
;
44 using System
.Diagnostics
.SymbolStore
;
46 namespace System
.Reflection
.Emit
49 [ComDefaultInterface (typeof (_TypeBuilder
))]
50 [ClassInterface (ClassInterfaceType
.None
)]
51 public sealed class TypeBuilder
: Type
, _TypeBuilder
53 #pragma warning disable 169
54 #region Sync with reflection.h
56 private string nspace
;
58 private Type nesting_type
;
59 internal Type
[] interfaces
;
60 internal int num_methods
;
61 internal MethodBuilder
[] methods
;
62 internal ConstructorBuilder
[] ctors
;
63 internal PropertyBuilder
[] properties
;
64 internal int num_fields
;
65 internal FieldBuilder
[] fields
;
66 internal EventBuilder
[] events
;
67 private CustomAttributeBuilder
[] cattrs
;
68 internal TypeBuilder
[] subtypes
;
69 internal TypeAttributes attrs
;
70 private int table_idx
;
71 private ModuleBuilder pmodule
;
72 private int class_size
;
73 private PackingSize packing_size
;
74 private IntPtr generic_container
;
75 private GenericTypeParameterBuilder
[] generic_params
;
76 private RefEmitPermissionSet
[] permissions
;
79 #pragma warning restore 169
82 bool createTypeCalled
;
83 private Type underlying_type
;
85 public const int UnspecifiedTypeSize
= 0;
87 protected override TypeAttributes
GetAttributeFlagsImpl ()
92 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
93 private extern void setup_internal_class (TypeBuilder tb
);
95 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
96 private extern void create_internal_class (TypeBuilder tb
);
98 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
99 private extern void setup_generic_class ();
101 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
102 private extern void create_generic_class ();
104 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
105 private extern EventInfo
get_event_info (EventBuilder eb
);
107 internal TypeBuilder (ModuleBuilder mb
, TypeAttributes attr
, int table_idx
)
111 this.class_size
= UnspecifiedTypeSize
;
112 this.table_idx
= table_idx
;
113 fullname
= this.tname
= table_idx
== 1 ? "<Module>" : "type_" + table_idx
.ToString ();
114 this.nspace
= String
.Empty
;
116 setup_internal_class (this);
119 internal TypeBuilder (ModuleBuilder mb
, string name
, TypeAttributes attr
, Type parent
, Type
[] interfaces
, PackingSize packing_size
, int type_size
, Type nesting_type
)
122 this.parent
= parent
;
124 this.class_size
= type_size
;
125 this.packing_size
= packing_size
;
126 this.nesting_type
= nesting_type
;
128 check_name ("fullname", name
);
130 if (parent
== null && (attr
& TypeAttributes
.Interface
) != 0 && (attr
& TypeAttributes
.Abstract
) == 0)
131 throw new InvalidOperationException ("Interface must be declared abstract.");
133 sep_index
= name
.LastIndexOf('.');
134 if (sep_index
!= -1) {
135 this.tname
= name
.Substring (sep_index
+ 1);
136 this.nspace
= name
.Substring (0, sep_index
);
139 this.nspace
= String
.Empty
;
141 if (interfaces
!= null) {
142 this.interfaces
= new Type
[interfaces
.Length
];
143 System
.Array
.Copy (interfaces
, this.interfaces
, interfaces
.Length
);
147 if (((attr
& TypeAttributes
.Interface
) == 0) && (parent
== null) && !IsCompilerContext
)
148 this.parent
= typeof (object);
151 table_idx
= mb
.get_next_table_index (this, 0x02, true);
152 setup_internal_class (this);
153 fullname
= GetFullName ();
156 public override Assembly Assembly
{
157 get {return pmodule.Assembly;}
160 public override string AssemblyQualifiedName
{
162 return fullname
+ ", " + Assembly
.FullName
;
166 public override Type BaseType
{
172 public override Type DeclaringType
{
173 get { return nesting_type; }
177 public override bool IsSubclassOf (Type c
)
193 public override Type UnderlyingSystemType
{
196 return created
.UnderlyingSystemType
;
198 if (!IsCompilerContext
&& IsEnum
) {
199 if (underlying_type
!= null)
200 return underlying_type
;
201 throw new InvalidOperationException (
202 "Enumeration type is not defined.");
209 string GetFullName ()
211 if (nesting_type
!= null)
212 return String
.Concat (nesting_type
.FullName
, "+", tname
);
213 if ((nspace
!= null) && (nspace
.Length
> 0))
214 return String
.Concat (nspace
, ".", tname
);
218 public override string FullName
{
224 public override Guid GUID
{
231 public override Module Module
{
232 get {return pmodule;}
235 public override string Name
{
239 public override string Namespace
{
243 public PackingSize PackingSize
{
244 get {return packing_size;}
248 get { return class_size; }
251 public override Type ReflectedType
{
252 get { return nesting_type; }
255 public void AddDeclarativeSecurity (SecurityAction action
, PermissionSet pset
)
259 throw new ArgumentNullException ("pset");
260 if ((action
== SecurityAction
.RequestMinimum
) ||
261 (action
== SecurityAction
.RequestOptional
) ||
262 (action
== SecurityAction
.RequestRefuse
))
263 throw new ArgumentOutOfRangeException ("Request* values are not permitted", "action");
265 check_not_created ();
267 if (permissions
!= null) {
268 /* Check duplicate actions */
269 foreach (RefEmitPermissionSet
set in permissions
)
270 if (set.action
== action
)
271 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
273 RefEmitPermissionSet
[] new_array
= new RefEmitPermissionSet
[permissions
.Length
+ 1];
274 permissions
.CopyTo (new_array
, 0);
275 permissions
= new_array
;
278 permissions
= new RefEmitPermissionSet
[1];
280 permissions
[permissions
.Length
- 1] = new RefEmitPermissionSet (action
, pset
.ToXml ().ToString ());
281 attrs
|= TypeAttributes
.HasSecurity
;
286 public void AddInterfaceImplementation (Type interfaceType
)
288 if (interfaceType
== null)
289 throw new ArgumentNullException ("interfaceType");
290 check_not_created ();
292 if (interfaces
!= null) {
293 // Check for duplicates
294 foreach (Type t
in interfaces
)
295 if (t
== interfaceType
)
298 Type
[] ifnew
= new Type
[interfaces
.Length
+ 1];
299 interfaces
.CopyTo (ifnew
, 0);
300 ifnew
[interfaces
.Length
] = interfaceType
;
303 interfaces
= new Type
[1];
304 interfaces
[0] = interfaceType
;
308 protected override ConstructorInfo
GetConstructorImpl (BindingFlags bindingAttr
, Binder binder
,
309 CallingConventions callConvention
, Type
[] types
,
310 ParameterModifier
[] modifiers
)
314 if (created
== typeof (object)) {
316 * This happens when building corlib. Calling created.GetConstructor
317 * would return constructors from the real mscorlib, instead of the
324 ConstructorBuilder found
= null;
327 foreach (ConstructorBuilder cb
in ctors
) {
328 if (callConvention
!= CallingConventions
.Any
&& cb
.CallingConvention
!= callConvention
)
338 throw new AmbiguousMatchException ();
341 MethodBase
[] match
= new MethodBase
[count
];
346 foreach (ConstructorInfo m
in ctors
) {
347 if (callConvention
!= CallingConventions
.Any
&& m
.CallingConvention
!= callConvention
)
353 binder
= Binder
.DefaultBinder
;
354 return (ConstructorInfo
) binder
.SelectMethod (bindingAttr
, match
,
358 return created
.GetConstructor (bindingAttr
, binder
,
359 callConvention
, types
, modifiers
);
362 public override bool IsDefined (Type attributeType
, bool inherit
)
364 if (!is_created
&& !IsCompilerContext
)
365 throw new NotSupportedException ();
367 * MS throws NotSupported here, but we can't because some corlib
368 * classes make calls to IsDefined.
370 return MonoCustomAttrs
.IsDefined (this, attributeType
, inherit
);
373 public override object[] GetCustomAttributes(bool inherit
)
377 return created
.GetCustomAttributes (inherit
);
380 public override object[] GetCustomAttributes(Type attributeType
, bool inherit
)
384 return created
.GetCustomAttributes (attributeType
, inherit
);
387 public TypeBuilder
DefineNestedType (string name
)
389 return DefineNestedType (name
, TypeAttributes
.NestedPrivate
,
390 pmodule
.assemblyb
.corlib_object_type
, null);
393 public TypeBuilder
DefineNestedType (string name
, TypeAttributes attr
)
395 return DefineNestedType (name
, attr
, pmodule
.assemblyb
.corlib_object_type
, null);
398 public TypeBuilder
DefineNestedType (string name
, TypeAttributes attr
, Type parent
)
400 return DefineNestedType (name
, attr
, parent
, null);
403 private TypeBuilder
DefineNestedType (string name
, TypeAttributes attr
, Type parent
, Type
[] interfaces
,
404 PackingSize packSize
, int typeSize
)
406 // Visibility must be NestedXXX
408 if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
409 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
410 throw new ArgumentException ("attr", "Bad type flags for nested type.");
412 if (interfaces
!= null)
413 foreach (Type iface
in interfaces
)
415 throw new ArgumentNullException ("interfaces");
417 TypeBuilder res
= new TypeBuilder (pmodule
, name
, attr
, parent
, interfaces
, packSize
, typeSize
, this);
418 res
.fullname
= res
.GetFullName ();
419 pmodule
.RegisterTypeName (res
, res
.fullname
);
420 if (subtypes
!= null) {
421 TypeBuilder
[] new_types
= new TypeBuilder
[subtypes
.Length
+ 1];
422 System
.Array
.Copy (subtypes
, new_types
, subtypes
.Length
);
423 new_types
[subtypes
.Length
] = res
;
424 subtypes
= new_types
;
426 subtypes
= new TypeBuilder
[1];
433 public TypeBuilder
DefineNestedType (string name
, TypeAttributes attr
, Type parent
, Type
[] interfaces
)
435 return DefineNestedType (name
, attr
, parent
, interfaces
, PackingSize
.Unspecified
, UnspecifiedTypeSize
);
438 public TypeBuilder
DefineNestedType (string name
, TypeAttributes attr
, Type parent
, int typeSize
)
440 return DefineNestedType (name
, attr
, parent
, null, PackingSize
.Unspecified
, typeSize
);
443 public TypeBuilder
DefineNestedType (string name
, TypeAttributes attr
, Type parent
, PackingSize packSize
)
445 return DefineNestedType (name
, attr
, parent
, null, packSize
, UnspecifiedTypeSize
);
449 public ConstructorBuilder
DefineConstructor (MethodAttributes attributes
, CallingConventions callingConvention
, Type
[] parameterTypes
)
451 return DefineConstructor (attributes
, callingConvention
, parameterTypes
, null, null);
455 public ConstructorBuilder
DefineConstructor (MethodAttributes attributes
, CallingConventions callingConvention
, Type
[] parameterTypes
, Type
[][] requiredCustomModifiers
, Type
[][] optionalCustomModifiers
)
457 check_not_created ();
458 ConstructorBuilder cb
= new ConstructorBuilder (this, attributes
,
459 callingConvention
, parameterTypes
, requiredCustomModifiers
,
460 optionalCustomModifiers
);
462 ConstructorBuilder
[] new_ctors
= new ConstructorBuilder
[ctors
.Length
+1];
463 System
.Array
.Copy (ctors
, new_ctors
, ctors
.Length
);
464 new_ctors
[ctors
.Length
] = cb
;
467 ctors
= new ConstructorBuilder
[1];
474 public ConstructorBuilder
DefineDefaultConstructor (MethodAttributes attributes
)
479 parent_type
= parent
;
481 parent_type
= pmodule
.assemblyb
.corlib_object_type
;
483 parent_type
= parent_type
.InternalResolve ();
484 ConstructorInfo parent_constructor
=
485 parent_type
.GetConstructor (
486 BindingFlags
.Public
| BindingFlags
.NonPublic
| BindingFlags
.Instance
,
487 null, Type
.EmptyTypes
, null);
488 if (parent_constructor
== null) {
489 throw new NotSupportedException ("Parent does"
490 + " not have a default constructor."
491 + " The default constructor must be"
492 + " explicitly defined.");
495 ConstructorBuilder cb
= DefineConstructor (attributes
,
496 CallingConventions
.Standard
, Type
.EmptyTypes
);
497 ILGenerator ig
= cb
.GetILGenerator ();
498 ig
.Emit (OpCodes
.Ldarg_0
);
499 ig
.Emit (OpCodes
.Call
, parent_constructor
);
500 ig
.Emit (OpCodes
.Ret
);
504 private void append_method (MethodBuilder mb
)
506 if (methods
!= null) {
507 if (methods
.Length
== num_methods
) {
508 MethodBuilder
[] new_methods
= new MethodBuilder
[methods
.Length
* 2];
509 System
.Array
.Copy (methods
, new_methods
, num_methods
);
510 methods
= new_methods
;
513 methods
= new MethodBuilder
[1];
515 methods
[num_methods
] = mb
;
519 public MethodBuilder
DefineMethod (string name
, MethodAttributes attributes
, Type returnType
, Type
[] parameterTypes
)
521 return DefineMethod (name
, attributes
, CallingConventions
.Standard
,
522 returnType
, parameterTypes
);
525 public MethodBuilder
DefineMethod (string name
, MethodAttributes attributes
, CallingConventions callingConvention
, Type returnType
, Type
[] parameterTypes
)
527 return DefineMethod (name
, attributes
, callingConvention
, returnType
,
528 null, null, parameterTypes
, null, null);
531 public MethodBuilder
DefineMethod (string name
, MethodAttributes attributes
, CallingConventions callingConvention
, Type returnType
, Type
[] returnTypeRequiredCustomModifiers
, Type
[] returnTypeOptionalCustomModifiers
, Type
[] parameterTypes
, Type
[][] parameterTypeRequiredCustomModifiers
, Type
[][] parameterTypeOptionalCustomModifiers
)
533 check_name ("name", name
);
534 check_not_created ();
536 !((attributes
& MethodAttributes
.Abstract
) != 0) ||
537 !((attributes
& MethodAttributes
.Virtual
) != 0)) &&
538 !(((attributes
& MethodAttributes
.Static
) != 0)))
539 throw new ArgumentException ("Interface method must be abstract and virtual.");
541 if (returnType
== null)
542 returnType
= pmodule
.assemblyb
.corlib_void_type
;
543 MethodBuilder res
= new MethodBuilder (this, name
, attributes
,
544 callingConvention
, returnType
,
545 returnTypeRequiredCustomModifiers
,
546 returnTypeOptionalCustomModifiers
, parameterTypes
,
547 parameterTypeRequiredCustomModifiers
,
548 parameterTypeOptionalCustomModifiers
);
553 public MethodBuilder
DefinePInvokeMethod (string name
, string dllName
, string entryName
, MethodAttributes attributes
, CallingConventions callingConvention
, Type returnType
, Type
[] parameterTypes
, CallingConvention nativeCallConv
, CharSet nativeCharSet
)
555 return DefinePInvokeMethod (name
, dllName
, entryName
, attributes
,
556 callingConvention
, returnType
, null, null, parameterTypes
,
557 null, null, nativeCallConv
, nativeCharSet
);
560 public MethodBuilder
DefinePInvokeMethod (
563 string entryName
, MethodAttributes attributes
,
564 CallingConventions callingConvention
,
566 Type
[] returnTypeRequiredCustomModifiers
,
567 Type
[] returnTypeOptionalCustomModifiers
,
568 Type
[] parameterTypes
,
569 Type
[][] parameterTypeRequiredCustomModifiers
,
570 Type
[][] parameterTypeOptionalCustomModifiers
,
571 CallingConvention nativeCallConv
,
572 CharSet nativeCharSet
)
574 check_name ("name", name
);
575 check_name ("dllName", dllName
);
576 check_name ("entryName", entryName
);
577 if ((attributes
& MethodAttributes
.Abstract
) != 0)
578 throw new ArgumentException ("PInvoke methods must be static and native and cannot be abstract.");
580 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
581 check_not_created ();
584 = new MethodBuilder (
590 returnTypeRequiredCustomModifiers
,
591 returnTypeOptionalCustomModifiers
,
593 parameterTypeRequiredCustomModifiers
,
594 parameterTypeOptionalCustomModifiers
,
603 public MethodBuilder
DefinePInvokeMethod (string name
, string dllName
, MethodAttributes attributes
, CallingConventions callingConvention
, Type returnType
, Type
[] parameterTypes
, CallingConvention nativeCallConv
, CharSet nativeCharSet
) {
604 return DefinePInvokeMethod (name
, dllName
, name
, attributes
, callingConvention
, returnType
, parameterTypes
,
605 nativeCallConv
, nativeCharSet
);
608 public MethodBuilder
DefineMethod (string name
, MethodAttributes attributes
)
610 return DefineMethod (name
, attributes
, CallingConventions
.Standard
);
613 public MethodBuilder
DefineMethod (string name
, MethodAttributes attributes
, CallingConventions callingConvention
)
615 return DefineMethod (name
, attributes
, callingConvention
, null, null);
618 public void DefineMethodOverride (MethodInfo methodInfoBody
, MethodInfo methodInfoDeclaration
)
620 if (methodInfoBody
== null)
621 throw new ArgumentNullException ("methodInfoBody");
622 if (methodInfoDeclaration
== null)
623 throw new ArgumentNullException ("methodInfoDeclaration");
624 check_not_created ();
625 if (methodInfoBody
.DeclaringType
!= this)
626 throw new ArgumentException ("method body must belong to this type");
628 if (methodInfoBody
is MethodBuilder
) {
629 MethodBuilder mb
= (MethodBuilder
)methodInfoBody
;
630 mb
.set_override (methodInfoDeclaration
);
634 public FieldBuilder
DefineField (string fieldName
, Type type
, FieldAttributes attributes
)
636 return DefineField (fieldName
, type
, null, null, attributes
);
639 public FieldBuilder
DefineField (string fieldName
, Type type
, Type
[] requiredCustomModifiers
, Type
[] optionalCustomModifiers
, FieldAttributes attributes
)
641 check_name ("fieldName", fieldName
);
642 if (type
== typeof (void))
643 throw new ArgumentException ("Bad field type in defining field.");
644 check_not_created ();
646 FieldBuilder res
= new FieldBuilder (this, fieldName
, type
, attributes
, requiredCustomModifiers
, optionalCustomModifiers
);
647 if (fields
!= null) {
648 if (fields
.Length
== num_fields
) {
649 FieldBuilder
[] new_fields
= new FieldBuilder
[fields
.Length
* 2];
650 System
.Array
.Copy (fields
, new_fields
, num_fields
);
653 fields
[num_fields
] = res
;
656 fields
= new FieldBuilder
[1];
659 create_internal_class (this);
662 if (IsEnum
&& !IsCompilerContext
) {
663 if (underlying_type
== null && (attributes
& FieldAttributes
.Static
) == 0)
664 underlying_type
= type
;
670 public PropertyBuilder
DefineProperty (string name
, PropertyAttributes attributes
, Type returnType
, Type
[] parameterTypes
)
672 return DefineProperty (name
, attributes
, 0, returnType
, null, null, parameterTypes
, null, null);
676 public PropertyBuilder
DefineProperty (string name
, PropertyAttributes attributes
, CallingConventions callingConvention
, Type returnType
, Type
[] parameterTypes
)
678 return DefineProperty (name
, attributes
, callingConvention
, returnType
, null, null, parameterTypes
, null, null);
682 public PropertyBuilder
DefineProperty (string name
, PropertyAttributes attributes
, Type returnType
, Type
[] returnTypeRequiredCustomModifiers
, Type
[] returnTypeOptionalCustomModifiers
, Type
[] parameterTypes
, Type
[][] parameterTypeRequiredCustomModifiers
, Type
[][] parameterTypeOptionalCustomModifiers
)
684 return DefineProperty (name
, attributes
, 0, returnType
, returnTypeRequiredCustomModifiers
, returnTypeOptionalCustomModifiers
, parameterTypes
, parameterTypeRequiredCustomModifiers
, parameterTypeOptionalCustomModifiers
);
687 public PropertyBuilder
DefineProperty (string name
, PropertyAttributes attributes
, CallingConventions callingConvention
, Type returnType
, Type
[] returnTypeRequiredCustomModifiers
, Type
[] returnTypeOptionalCustomModifiers
, Type
[] parameterTypes
, Type
[][] parameterTypeRequiredCustomModifiers
, Type
[][] parameterTypeOptionalCustomModifiers
)
689 check_name ("name", name
);
690 if (parameterTypes
!= null)
691 foreach (Type param
in parameterTypes
)
693 throw new ArgumentNullException ("parameterTypes");
694 check_not_created ();
696 PropertyBuilder res
= new PropertyBuilder (this, name
, attributes
, callingConvention
, returnType
, returnTypeRequiredCustomModifiers
, returnTypeOptionalCustomModifiers
, parameterTypes
, parameterTypeRequiredCustomModifiers
, parameterTypeOptionalCustomModifiers
);
698 if (properties
!= null) {
699 Array
.Resize (ref properties
, properties
.Length
+ 1);
700 properties
[properties
.Length
- 1] = res
;
702 properties
= new PropertyBuilder
[1] { res }
;
708 public ConstructorBuilder
DefineTypeInitializer()
710 return DefineConstructor (MethodAttributes
.Public
|
711 MethodAttributes
.Static
| MethodAttributes
.SpecialName
|
712 MethodAttributes
.RTSpecialName
, CallingConventions
.Standard
,
716 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
717 private extern Type
create_runtime_class (TypeBuilder tb
);
719 private bool is_nested_in (Type t
)
730 // Return whenever this type has a ctor defined using DefineMethod ()
731 private bool has_ctor_method () {
732 MethodAttributes ctor_attrs
= MethodAttributes
.SpecialName
| MethodAttributes
.RTSpecialName
;
734 for (int i
= 0; i
< num_methods
; ++i
) {
735 MethodBuilder mb
= (MethodBuilder
)(methods
[i
]);
737 if (mb
.Name
== ConstructorInfo
.ConstructorName
&& (mb
.Attributes
& ctor_attrs
) == ctor_attrs
)
744 public Type
CreateType()
746 /* handle nesting_type */
747 if (createTypeCalled
)
750 if (!IsInterface
&& (parent
== null) && (this != pmodule
.assemblyb
.corlib_object_type
) && (FullName
!= "<Module>")) {
751 SetParent (pmodule
.assemblyb
.corlib_object_type
);
754 create_generic_class ();
756 // Fire TypeResolve events for fields whose type is an unfinished
758 if (fields
!= null) {
759 foreach (FieldBuilder fb
in fields
) {
762 Type ft
= fb
.FieldType
;
763 if (!fb
.IsStatic
&& (ft
is TypeBuilder
) && ft
.IsValueType
&& (ft
!= this) && is_nested_in (ft
)) {
764 TypeBuilder tb
= (TypeBuilder
)ft
;
765 if (!tb
.is_created
) {
766 AppDomain
.CurrentDomain
.DoTypeResolve (tb
);
767 if (!tb
.is_created
) {
768 // FIXME: We should throw an exception here,
769 // but mcs expects that the type is created
770 // even if the exception is thrown
771 //throw new TypeLoadException ("Could not load type " + tb);
779 // On classes, define a default constructor if not provided
781 if (!(IsInterface
|| IsValueType
) && (ctors
== null) && (tname
!= "<Module>") &&
782 (GetAttributeFlagsImpl () & TypeAttributes
.Abstract
| TypeAttributes
.Sealed
) != (TypeAttributes
.Abstract
| TypeAttributes
.Sealed
) && !has_ctor_method ())
783 DefineDefaultConstructor (MethodAttributes
.Public
);
785 createTypeCalled
= true;
787 if ((parent
!= null) && parent
.IsSealed
)
788 throw new TypeLoadException ("Could not load type '" + FullName
+ "' from assembly '" + Assembly
+ "' because the parent type is sealed.");
790 if (parent
== pmodule
.assemblyb
.corlib_enum_type
&& methods
!= null)
791 throw new TypeLoadException ("Could not load type '" + FullName
+ "' from assembly '" + Assembly
+ "' because it is an enum with methods.");
793 if (methods
!= null) {
794 bool is_concrete
= !IsAbstract
;
795 for (int i
= 0; i
< num_methods
; ++i
) {
796 MethodBuilder mb
= (MethodBuilder
)(methods
[i
]);
797 if (is_concrete
&& mb
.IsAbstract
)
798 throw new InvalidOperationException ("Type is concrete but has abstract method " + mb
);
799 mb
.check_override ();
805 foreach (ConstructorBuilder ctor
in ctors
)
809 created
= create_runtime_class (this);
815 internal void GenerateDebugInfo (ISymbolWriter symbolWriter
)
817 symbolWriter
.OpenNamespace (this.Namespace
);
819 if (methods
!= null) {
820 for (int i
= 0; i
< num_methods
; ++i
) {
821 MethodBuilder metb
= (MethodBuilder
) methods
[i
];
822 metb
.GenerateDebugInfo (symbolWriter
);
827 foreach (ConstructorBuilder ctor
in ctors
)
828 ctor
.GenerateDebugInfo (symbolWriter
);
831 symbolWriter
.CloseNamespace ();
833 if (subtypes
!= null) {
834 for (int i
= 0; i
< subtypes
.Length
; ++i
)
835 subtypes
[i
].GenerateDebugInfo (symbolWriter
);
840 public override ConstructorInfo
[] GetConstructors (BindingFlags bindingAttr
)
843 return created
.GetConstructors (bindingAttr
);
845 if (!IsCompilerContext
)
846 throw new NotSupportedException ();
848 return GetConstructorsInternal (bindingAttr
);
851 internal ConstructorInfo
[] GetConstructorsInternal (BindingFlags bindingAttr
)
854 return new ConstructorInfo
[0];
855 ArrayList l
= new ArrayList ();
857 MethodAttributes mattrs
;
859 foreach (ConstructorBuilder c
in ctors
) {
861 mattrs
= c
.Attributes
;
862 if ((mattrs
& MethodAttributes
.MemberAccessMask
) == MethodAttributes
.Public
) {
863 if ((bindingAttr
& BindingFlags
.Public
) != 0)
866 if ((bindingAttr
& BindingFlags
.NonPublic
) != 0)
872 if ((mattrs
& MethodAttributes
.Static
) != 0) {
873 if ((bindingAttr
& BindingFlags
.Static
) != 0)
876 if ((bindingAttr
& BindingFlags
.Instance
) != 0)
883 ConstructorInfo
[] result
= new ConstructorInfo
[l
.Count
];
888 public override Type
GetElementType ()
890 throw new NotSupportedException ();
893 public override EventInfo
GetEvent (string name
, BindingFlags bindingAttr
)
896 return created
.GetEvent (name
, bindingAttr
);
899 /* Needed to keep signature compatibility with MS.NET */
900 public override EventInfo
[] GetEvents ()
902 return GetEvents (DefaultBindingFlags
);
905 public override EventInfo
[] GetEvents (BindingFlags bindingAttr
)
908 return created
.GetEvents (bindingAttr
);
909 if (!IsCompilerContext
)
910 throw new NotSupportedException ();
911 return new EventInfo
[0]; /*FIXME shouldn't we return the events here?*/
914 // This is only used from MonoGenericInst.initialize().
915 internal EventInfo
[] GetEvents_internal (BindingFlags bindingAttr
)
918 return new EventInfo
[0];
919 ArrayList l
= new ArrayList ();
921 MethodAttributes mattrs
;
924 foreach (EventBuilder eb
in events
) {
927 EventInfo c
= get_event_info (eb
);
929 accessor
= c
.GetAddMethod (true);
930 if (accessor
== null)
931 accessor
= c
.GetRemoveMethod (true);
932 if (accessor
== null)
934 mattrs
= accessor
.Attributes
;
935 if ((mattrs
& MethodAttributes
.MemberAccessMask
) == MethodAttributes
.Public
) {
936 if ((bindingAttr
& BindingFlags
.Public
) != 0)
939 if ((bindingAttr
& BindingFlags
.NonPublic
) != 0)
945 if ((mattrs
& MethodAttributes
.Static
) != 0) {
946 if ((bindingAttr
& BindingFlags
.Static
) != 0)
949 if ((bindingAttr
& BindingFlags
.Instance
) != 0)
956 EventInfo
[] result
= new EventInfo
[l
.Count
];
961 public override FieldInfo
GetField (string name
, BindingFlags bindingAttr
)
964 return created
.GetField (name
, bindingAttr
);
970 FieldAttributes mattrs
;
972 foreach (FieldInfo c
in fields
) {
978 mattrs
= c
.Attributes
;
979 if ((mattrs
& FieldAttributes
.FieldAccessMask
) == FieldAttributes
.Public
) {
980 if ((bindingAttr
& BindingFlags
.Public
) != 0)
983 if ((bindingAttr
& BindingFlags
.NonPublic
) != 0)
989 if ((mattrs
& FieldAttributes
.Static
) != 0) {
990 if ((bindingAttr
& BindingFlags
.Static
) != 0)
993 if ((bindingAttr
& BindingFlags
.Instance
) != 0)
1003 public override FieldInfo
[] GetFields (BindingFlags bindingAttr
)
1005 if (created
!= null)
1006 return created
.GetFields (bindingAttr
);
1009 return new FieldInfo
[0];
1010 ArrayList l
= new ArrayList ();
1012 FieldAttributes mattrs
;
1014 foreach (FieldInfo c
in fields
) {
1018 mattrs
= c
.Attributes
;
1019 if ((mattrs
& FieldAttributes
.FieldAccessMask
) == FieldAttributes
.Public
) {
1020 if ((bindingAttr
& BindingFlags
.Public
) != 0)
1023 if ((bindingAttr
& BindingFlags
.NonPublic
) != 0)
1029 if ((mattrs
& FieldAttributes
.Static
) != 0) {
1030 if ((bindingAttr
& BindingFlags
.Static
) != 0)
1033 if ((bindingAttr
& BindingFlags
.Instance
) != 0)
1040 FieldInfo
[] result
= new FieldInfo
[l
.Count
];
1045 public override Type
GetInterface (string name
, bool ignoreCase
)
1048 return created
.GetInterface (name
, ignoreCase
);
1051 public override Type
[] GetInterfaces ()
1054 return created
.GetInterfaces ();
1056 if (interfaces
!= null) {
1057 Type
[] ret
= new Type
[interfaces
.Length
];
1058 interfaces
.CopyTo (ret
, 0);
1061 return Type
.EmptyTypes
;
1065 public override MemberInfo
[] GetMember (string name
, MemberTypes type
,
1066 BindingFlags bindingAttr
)
1069 return created
.GetMember (name
, type
, bindingAttr
);
1072 public override MemberInfo
[] GetMembers (BindingFlags bindingAttr
)
1075 return created
.GetMembers (bindingAttr
);
1078 private MethodInfo
[] GetMethodsByName (string name
, BindingFlags bindingAttr
, bool ignoreCase
, Type reflected_type
)
1080 MethodInfo
[] candidates
;
1082 MethodAttributes mattrs
;
1084 if (((bindingAttr
& BindingFlags
.DeclaredOnly
) == 0) && (parent
!= null)) {
1085 MethodInfo
[] parent_methods
= parent
.GetMethods (bindingAttr
);
1086 ArrayList parent_candidates
= new ArrayList (parent_methods
.Length
);
1088 bool flatten
= (bindingAttr
& BindingFlags
.FlattenHierarchy
) != 0;
1090 for (int i
= 0; i
< parent_methods
.Length
; i
++) {
1091 MethodInfo m
= parent_methods
[i
];
1093 mattrs
= m
.Attributes
;
1095 if (m
.IsStatic
&& !flatten
)
1098 switch (mattrs
& MethodAttributes
.MemberAccessMask
) {
1099 case MethodAttributes
.Public
:
1100 match
= (bindingAttr
& BindingFlags
.Public
) != 0;
1102 case MethodAttributes
.Assembly
:
1103 match
= (bindingAttr
& BindingFlags
.NonPublic
) != 0;
1105 case MethodAttributes
.Private
:
1109 match
= (bindingAttr
& BindingFlags
.NonPublic
) != 0;
1114 parent_candidates
.Add (m
);
1117 if (methods
== null) {
1118 candidates
= new MethodInfo
[parent_candidates
.Count
];
1119 parent_candidates
.CopyTo (candidates
);
1121 candidates
= new MethodInfo
[methods
.Length
+ parent_candidates
.Count
];
1122 parent_candidates
.CopyTo (candidates
, 0);
1123 methods
.CopyTo (candidates
, parent_candidates
.Count
);
1127 candidates
= methods
;
1129 if (candidates
== null)
1130 return new MethodInfo
[0];
1132 ArrayList l
= new ArrayList ();
1134 foreach (MethodInfo c
in candidates
) {
1138 if (String
.Compare (c
.Name
, name
, ignoreCase
) != 0)
1142 mattrs
= c
.Attributes
;
1143 if ((mattrs
& MethodAttributes
.MemberAccessMask
) == MethodAttributes
.Public
) {
1144 if ((bindingAttr
& BindingFlags
.Public
) != 0)
1147 if ((bindingAttr
& BindingFlags
.NonPublic
) != 0)
1153 if ((mattrs
& MethodAttributes
.Static
) != 0) {
1154 if ((bindingAttr
& BindingFlags
.Static
) != 0)
1157 if ((bindingAttr
& BindingFlags
.Instance
) != 0)
1165 MethodInfo
[] result
= new MethodInfo
[l
.Count
];
1170 public override MethodInfo
[] GetMethods (BindingFlags bindingAttr
)
1172 return GetMethodsByName (null, bindingAttr
, false, this);
1175 protected override MethodInfo
GetMethodImpl (string name
, BindingFlags bindingAttr
,
1177 CallingConventions callConvention
,
1178 Type
[] types
, ParameterModifier
[] modifiers
)
1182 bool ignoreCase
= ((bindingAttr
& BindingFlags
.IgnoreCase
) != 0);
1183 MethodInfo
[] methods
= GetMethodsByName (name
, bindingAttr
, ignoreCase
, this);
1184 MethodInfo found
= null;
1186 int typesLen
= (types
!= null) ? types
.Length
: 0;
1189 foreach (MethodInfo m
in methods
) {
1190 // Under MS.NET, Standard|HasThis matches Standard...
1191 if (callConvention
!= CallingConventions
.Any
&& ((m
.CallingConvention
& callConvention
) != callConvention
))
1200 if (count
== 1 && typesLen
== 0)
1203 match
= new MethodBase
[count
];
1208 foreach (MethodInfo m
in methods
) {
1209 if (callConvention
!= CallingConventions
.Any
&& ((m
.CallingConvention
& callConvention
) != callConvention
))
1211 match
[count
++] = m
;
1216 return (MethodInfo
) Binder
.FindMostDerivedMatch (match
);
1219 binder
= Binder
.DefaultBinder
;
1221 return (MethodInfo
)binder
.SelectMethod (bindingAttr
, match
, types
, modifiers
);
1224 public override Type
GetNestedType (string name
, BindingFlags bindingAttr
)
1228 if (subtypes
== null)
1231 foreach (TypeBuilder t
in subtypes
) {
1234 if ((t
.attrs
& TypeAttributes
.VisibilityMask
) == TypeAttributes
.NestedPublic
) {
1235 if ((bindingAttr
& BindingFlags
.Public
) == 0)
1238 if ((bindingAttr
& BindingFlags
.NonPublic
) == 0)
1248 public override Type
[] GetNestedTypes (BindingFlags bindingAttr
)
1250 if (!is_created
&& !IsCompilerContext
)
1251 throw new NotSupportedException ();
1254 ArrayList result
= new ArrayList ();
1256 if (subtypes
== null)
1257 return Type
.EmptyTypes
;
1258 foreach (TypeBuilder t
in subtypes
) {
1260 if ((t
.attrs
& TypeAttributes
.VisibilityMask
) == TypeAttributes
.NestedPublic
) {
1261 if ((bindingAttr
& BindingFlags
.Public
) != 0)
1264 if ((bindingAttr
& BindingFlags
.NonPublic
) != 0)
1271 Type
[] r
= new Type
[result
.Count
];
1276 public override PropertyInfo
[] GetProperties (BindingFlags bindingAttr
)
1279 return created
.GetProperties (bindingAttr
);
1281 if (properties
== null)
1282 return new PropertyInfo
[0];
1283 ArrayList l
= new ArrayList ();
1285 MethodAttributes mattrs
;
1286 MethodInfo accessor
;
1288 foreach (PropertyInfo c
in properties
) {
1290 accessor
= c
.GetGetMethod (true);
1291 if (accessor
== null)
1292 accessor
= c
.GetSetMethod (true);
1293 if (accessor
== null)
1295 mattrs
= accessor
.Attributes
;
1296 if ((mattrs
& MethodAttributes
.MemberAccessMask
) == MethodAttributes
.Public
) {
1297 if ((bindingAttr
& BindingFlags
.Public
) != 0)
1300 if ((bindingAttr
& BindingFlags
.NonPublic
) != 0)
1306 if ((mattrs
& MethodAttributes
.Static
) != 0) {
1307 if ((bindingAttr
& BindingFlags
.Static
) != 0)
1310 if ((bindingAttr
& BindingFlags
.Instance
) != 0)
1317 PropertyInfo
[] result
= new PropertyInfo
[l
.Count
];
1322 protected override PropertyInfo
GetPropertyImpl (string name
, BindingFlags bindingAttr
, Binder binder
, Type returnType
, Type
[] types
, ParameterModifier
[] modifiers
)
1324 throw not_supported ();
1327 protected override bool HasElementTypeImpl ()
1329 // a TypeBuilder can never represent an array, pointer
1333 return created
.HasElementType
;
1336 public override object InvokeMember (string name
, BindingFlags invokeAttr
, Binder binder
, object target
, object[] args
, ParameterModifier
[] modifiers
, CultureInfo culture
, string[] namedParameters
)
1339 return created
.InvokeMember (name
, invokeAttr
, binder
, target
, args
, modifiers
, culture
, namedParameters
);
1342 protected override bool IsArrayImpl ()
1344 return false; /*A TypeBuilder never represents a non typedef type.*/
1347 protected override bool IsByRefImpl ()
1349 return false; /*A TypeBuilder never represents a non typedef type.*/
1352 protected override bool IsCOMObjectImpl ()
1354 return ((GetAttributeFlagsImpl () & TypeAttributes
.Import
) != 0);
1357 protected override bool IsPointerImpl ()
1359 return false; /*A TypeBuilder never represents a non typedef type.*/
1362 protected override bool IsPrimitiveImpl ()
1368 // FIXME: I doubt just removing this still works.
1369 protected override bool IsValueTypeImpl ()
1371 if (this == pmodule
.assemblyb
.corlib_value_type
|| this == pmodule
.assemblyb
.corlib_enum_type
)
1373 Type parent_type
= parent
;
1374 while (parent_type
!= null) {
1375 if (parent_type
== pmodule
.assemblyb
.corlib_value_type
)
1377 parent_type
= parent_type
.BaseType
;
1382 public override Type
MakeArrayType ()
1384 return new ArrayType (this, 0);
1387 public override Type
MakeArrayType (int rank
)
1390 throw new IndexOutOfRangeException ();
1391 return new ArrayType (this, rank
);
1394 public override Type
MakeByRefType ()
1396 return new ByRefType (this);
1399 public override Type
MakeGenericType (params Type
[] typeArguments
)
1401 //return base.MakeGenericType (typeArguments);
1403 if (!IsGenericTypeDefinition
)
1404 throw new InvalidOperationException ("not a generic type definition");
1405 if (typeArguments
== null)
1406 throw new ArgumentNullException ("typeArguments");
1408 if (generic_params
.Length
!= typeArguments
.Length
)
1409 throw new ArgumentException (String
.Format ("The type or method has {0} generic parameter(s) but {1} generic argument(s) where provided. A generic argument must be provided for each generic parameter.", generic_params
.Length
, typeArguments
.Length
), "typeArguments");
1411 foreach (Type t
in typeArguments
) {
1413 throw new ArgumentNullException ("typeArguments");
1416 Type
[] copy
= new Type
[typeArguments
.Length
];
1417 typeArguments
.CopyTo (copy
, 0);
1418 return pmodule
.assemblyb
.MakeGenericType (this, copy
);
1421 public override Type
MakePointerType ()
1423 return new PointerType (this);
1426 public override RuntimeTypeHandle TypeHandle
{
1429 return created
.TypeHandle
;
1434 // Used internally by mcs only
1436 internal void SetCharSet (TypeAttributes ta
)
1441 public void SetCustomAttribute (CustomAttributeBuilder customBuilder
)
1443 if (customBuilder
== null)
1444 throw new ArgumentNullException ("customBuilder");
1446 string attrname
= customBuilder
.Ctor
.ReflectedType
.FullName
;
1447 if (attrname
== "System.Runtime.InteropServices.StructLayoutAttribute") {
1448 byte[] data
= customBuilder
.Data
;
1449 int layout_kind
; /* the (stupid) ctor takes a short or an int ... */
1450 layout_kind
= (int)data
[2];
1451 layout_kind
|= ((int)data
[3]) << 8;
1452 attrs
&= ~TypeAttributes
.LayoutMask
;
1453 switch ((LayoutKind
)layout_kind
) {
1454 case LayoutKind
.Auto
:
1455 attrs
|= TypeAttributes
.AutoLayout
;
1457 case LayoutKind
.Explicit
:
1458 attrs
|= TypeAttributes
.ExplicitLayout
;
1460 case LayoutKind
.Sequential
:
1461 attrs
|= TypeAttributes
.SequentialLayout
;
1464 // we should ignore it since it can be any value anyway...
1465 throw new Exception ("Error in customattr");
1468 var ctor_type
= customBuilder
.Ctor
is ConstructorBuilder
? ((ConstructorBuilder
)customBuilder
.Ctor
).parameters
[0] : customBuilder
.Ctor
.GetParameters()[0].ParameterType
;
1470 if (ctor_type
.FullName
== "System.Int16")
1472 int nnamed
= (int)data
[pos
++];
1473 nnamed
|= ((int)data
[pos
++]) << 8;
1474 for (int i
= 0; i
< nnamed
; ++i
) {
1475 //byte named_type = data [pos++];
1477 byte type
= data
[pos
++];
1482 len
= CustomAttributeBuilder
.decode_len (data
, pos
, out pos
);
1483 //string named_typename =
1484 CustomAttributeBuilder
.string_from_bytes (data
, pos
, len
);
1486 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1487 // See related code/FIXME in mono/mono/metadata/reflection.c
1490 len
= CustomAttributeBuilder
.decode_len (data
, pos
, out pos
);
1491 named_name
= CustomAttributeBuilder
.string_from_bytes (data
, pos
, len
);
1493 /* all the fields are integers in StructLayout */
1494 int value = (int)data
[pos
++];
1495 value |= ((int)data
[pos
++]) << 8;
1496 value |= ((int)data
[pos
++]) << 16;
1497 value |= ((int)data
[pos
++]) << 24;
1498 switch (named_name
) {
1500 switch ((CharSet
)value) {
1503 attrs
&= ~
(TypeAttributes
.UnicodeClass
| TypeAttributes
.AutoClass
);
1505 case CharSet
.Unicode
:
1506 attrs
&= ~TypeAttributes
.AutoClass
;
1507 attrs
|= TypeAttributes
.UnicodeClass
;
1510 attrs
&= ~TypeAttributes
.UnicodeClass
;
1511 attrs
|= TypeAttributes
.AutoClass
;
1514 break; // error out...
1518 packing_size
= (PackingSize
)value;
1524 break; // error out...
1528 } else if (attrname
== "System.Runtime.CompilerServices.SpecialNameAttribute") {
1529 attrs
|= TypeAttributes
.SpecialName
;
1531 } else if (attrname
== "System.SerializableAttribute") {
1532 attrs
|= TypeAttributes
.Serializable
;
1534 } else if (attrname
== "System.Runtime.InteropServices.ComImportAttribute") {
1535 attrs
|= TypeAttributes
.Import
;
1537 } else if (attrname
== "System.Security.SuppressUnmanagedCodeSecurityAttribute") {
1538 attrs
|= TypeAttributes
.HasSecurity
;
1541 if (cattrs
!= null) {
1542 CustomAttributeBuilder
[] new_array
= new CustomAttributeBuilder
[cattrs
.Length
+ 1];
1543 cattrs
.CopyTo (new_array
, 0);
1544 new_array
[cattrs
.Length
] = customBuilder
;
1547 cattrs
= new CustomAttributeBuilder
[1];
1548 cattrs
[0] = customBuilder
;
1553 public void SetCustomAttribute (ConstructorInfo con
, byte[] binaryAttribute
)
1555 SetCustomAttribute (new CustomAttributeBuilder (con
, binaryAttribute
));
1558 public EventBuilder
DefineEvent (string name
, EventAttributes attributes
, Type eventtype
)
1560 check_name ("name", name
);
1561 if (eventtype
== null)
1562 throw new ArgumentNullException ("type");
1563 check_not_created ();
1565 EventBuilder res
= new EventBuilder (this, name
, attributes
, eventtype
);
1566 if (events
!= null) {
1567 EventBuilder
[] new_events
= new EventBuilder
[events
.Length
+1];
1568 System
.Array
.Copy (events
, new_events
, events
.Length
);
1569 new_events
[events
.Length
] = res
;
1570 events
= new_events
;
1572 events
= new EventBuilder
[1];
1578 public FieldBuilder
DefineInitializedData (string name
, byte[] data
, FieldAttributes attributes
) {
1580 throw new ArgumentNullException ("data");
1582 FieldBuilder res
= DefineUninitializedData (name
, data
.Length
, attributes
);
1583 res
.SetRVAData (data
);
1587 public FieldBuilder
DefineUninitializedData (string name
, int size
, FieldAttributes attributes
)
1590 throw new ArgumentNullException ("name");
1591 if (name
.Length
== 0)
1592 throw new ArgumentException ("Empty name is not legal", "name");
1593 if ((size
<= 0) || (size
> 0x3f0000))
1594 throw new ArgumentException ("Data size must be > 0 and < 0x3f0000");
1595 check_not_created ();
1597 string typeName
= "$ArrayType$" + size
;
1598 Type datablobtype
= pmodule
.GetRegisteredType (fullname
+ "+" + typeName
);
1599 if (datablobtype
== null) {
1600 TypeBuilder tb
= DefineNestedType (typeName
,
1601 TypeAttributes
.NestedPrivate
|TypeAttributes
.ExplicitLayout
|TypeAttributes
.Sealed
,
1602 pmodule
.assemblyb
.corlib_value_type
, null, PackingSize
.Size1
, size
);
1606 return DefineField (name
, datablobtype
, attributes
|FieldAttributes
.Static
|FieldAttributes
.HasFieldRVA
);
1609 public TypeToken TypeToken
{
1611 return new TypeToken (0x02000000 | table_idx
);
1615 public void SetParent (Type parent
)
1617 check_not_created ();
1619 if (parent
== null) {
1620 if ((attrs
& TypeAttributes
.Interface
) != 0) {
1621 if ((attrs
& TypeAttributes
.Abstract
) == 0)
1622 throw new InvalidOperationException ("Interface must be declared abstract.");
1625 this.parent
= typeof (object);
1628 this.parent
= parent
;
1631 // will just set the parent-related bits if called a second time
1632 setup_internal_class (this);
1635 internal int get_next_table_index (object obj
, int table
, bool inc
) {
1636 return pmodule
.get_next_table_index (obj
, table
, inc
);
1640 public override InterfaceMapping
GetInterfaceMap (Type interfaceType
)
1642 if (created
== null)
1643 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1645 return created
.GetInterfaceMap (interfaceType
);
1648 internal override bool IsCompilerContext
{
1650 return pmodule
.assemblyb
.IsCompilerContext
;
1654 internal override Type
InternalResolve ()
1660 internal bool is_created
{
1662 return createTypeCalled
;
1666 private Exception
not_supported ()
1668 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1671 private void check_not_created ()
1674 throw new InvalidOperationException ("Unable to change after type has been created.");
1677 private void check_created ()
1680 throw not_supported ();
1683 private void check_name (string argName
, string name
)
1686 throw new ArgumentNullException (argName
);
1687 if (name
.Length
== 0)
1688 throw new ArgumentException ("Empty name is not legal", argName
);
1689 if (name
[0] == ((char)0))
1690 throw new ArgumentException ("Illegal name", argName
);
1693 public override String
ToString ()
1699 public override bool IsAssignableFrom (Type c
)
1701 return base.IsAssignableFrom (c
);
1704 [MonoTODO ("arrays")]
1705 internal bool IsAssignableTo (Type c
)
1710 if (c
.IsInterface
) {
1711 if (parent
!= null && is_created
) {
1712 if (c
.IsAssignableFrom (parent
))
1716 if (interfaces
== null)
1718 foreach (Type t
in interfaces
)
1719 if (c
.IsAssignableFrom (t
))
1726 return c
== typeof (object);
1728 return c
.IsAssignableFrom (parent
);
1731 public bool IsCreated ()
1736 public override Type
[] GetGenericArguments ()
1738 if (generic_params
== null)
1740 Type
[] args
= new Type
[generic_params
.Length
];
1741 generic_params
.CopyTo (args
, 0);
1745 public override Type
GetGenericTypeDefinition ()
1747 if (generic_params
== null)
1748 throw new InvalidOperationException ("Type is not generic");
1752 public override bool ContainsGenericParameters
{
1754 return generic_params
!= null;
1758 public extern override bool IsGenericParameter
{
1759 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
1763 public override GenericParameterAttributes GenericParameterAttributes
{
1764 get { return GenericParameterAttributes.None; }
1767 public override bool IsGenericTypeDefinition
{
1769 return generic_params
!= null;
1773 public override bool IsGenericType
{
1774 get { return IsGenericTypeDefinition; }
1778 public override int GenericParameterPosition
{
1784 public override MethodBase DeclaringMethod
{
1790 public GenericTypeParameterBuilder
[] DefineGenericParameters (params string[] names
)
1793 throw new ArgumentNullException ("names");
1794 if (names
.Length
== 0)
1795 throw new ArgumentException ("names");
1797 setup_generic_class ();
1799 generic_params
= new GenericTypeParameterBuilder
[names
.Length
];
1800 for (int i
= 0; i
< names
.Length
; i
++) {
1801 string item
= names
[i
];
1803 throw new ArgumentNullException ("names");
1804 generic_params
[i
] = new GenericTypeParameterBuilder (this, null, item
, i
);
1807 return generic_params
;
1810 public static ConstructorInfo
GetConstructor (Type type
, ConstructorInfo constructor
)
1812 /*FIXME I would expect the same checks of GetMethod here*/
1814 throw new ArgumentException ("Type is not generic", "type");
1816 if (!type
.IsGenericType
)
1817 throw new ArgumentException ("Type is not a generic type", "type");
1819 if (type
.IsGenericTypeDefinition
)
1820 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1822 if (constructor
== null)
1823 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1825 ConstructorInfo res
= type
.GetConstructor (constructor
);
1827 throw new ArgumentException ("constructor not found");
1832 static bool IsValidGetMethodType (Type type
)
1834 if (type
is TypeBuilder
|| type
is MonoGenericClass
)
1836 /*GetMethod() must work with TypeBuilders after CreateType() was called.*/
1837 if (type
.Module
is ModuleBuilder
)
1839 if (type
.IsGenericParameter
)
1842 Type
[] inst
= type
.GetGenericArguments ();
1845 for (int i
= 0; i
< inst
.Length
; ++i
) {
1846 if (IsValidGetMethodType (inst
[i
]))
1852 public static MethodInfo
GetMethod (Type type
, MethodInfo method
)
1854 if (!IsValidGetMethodType (type
))
1855 throw new ArgumentException ("type is not TypeBuilder but " + type
.GetType (), "type");
1857 if (type
is TypeBuilder
&& type
.ContainsGenericParameters
)
1858 type
= type
.MakeGenericType (type
.GetGenericArguments ());
1860 if (!type
.IsGenericType
)
1861 throw new ArgumentException ("type is not a generic type", "type");
1863 if (!method
.DeclaringType
.IsGenericTypeDefinition
)
1864 throw new ArgumentException ("method declaring type is not a generic type definition", "method");
1865 if (method
.DeclaringType
!= type
.GetGenericTypeDefinition ())
1866 throw new ArgumentException ("method declaring type is not the generic type definition of type", "method");
1868 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1870 MethodInfo res
= type
.GetMethod (method
);
1872 throw new ArgumentException (String
.Format ("method {0} not found in type {1}", method
.Name
, type
));
1877 public static FieldInfo
GetField (Type type
, FieldInfo field
)
1879 if (!type
.IsGenericType
)
1880 throw new ArgumentException ("Type is not a generic type", "type");
1882 if (type
.IsGenericTypeDefinition
)
1883 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1885 if (field
is FieldOnTypeBuilderInst
)
1886 throw new ArgumentException ("The specified field must be declared on a generic type definition.", "field");
1888 FieldInfo res
= type
.GetField (field
);
1890 throw new System
.Exception ("field not found");
1895 void _TypeBuilder
.GetIDsOfNames([In
] ref Guid riid
, IntPtr rgszNames
, uint cNames
, uint lcid
, IntPtr rgDispId
)
1897 throw new NotImplementedException ();
1900 void _TypeBuilder
.GetTypeInfo (uint iTInfo
, uint lcid
, IntPtr ppTInfo
)
1902 throw new NotImplementedException ();
1905 void _TypeBuilder
.GetTypeInfoCount (out uint pcTInfo
)
1907 throw new NotImplementedException ();
1910 void _TypeBuilder
.Invoke (uint dispIdMember
, [In
] ref Guid riid
, uint lcid
, short wFlags
, IntPtr pDispParams
, IntPtr pVarResult
, IntPtr pExcepInfo
, IntPtr puArgErr
)
1912 throw new NotImplementedException ();