2 // System.Reflection.Emit/TypeBuilder.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
11 using System.Reflection;
12 using System.Reflection.Emit;
13 using System.Runtime.CompilerServices;
14 using System.Runtime.InteropServices;
15 using System.Globalization;
16 using System.Collections;
17 using System.Security;
18 using System.Security.Permissions;
20 namespace System.Reflection.Emit {
21 public sealed class TypeBuilder : Type {
23 private string nspace;
25 private Type nesting_type;
26 private Type[] interfaces;
27 private MethodBuilder[] methods;
28 private ConstructorBuilder[] ctors;
29 private PropertyBuilder[] properties;
30 private FieldBuilder[] fields;
31 private EventBuilder[] events;
32 private CustomAttributeBuilder[] cattrs;
33 internal TypeBuilder[] subtypes;
34 private TypeAttributes attrs;
35 private int table_idx;
36 private ModuleBuilder pmodule;
37 private int class_size;
38 private PackingSize packing_size;
42 public const int UnspecifiedTypeSize = 0;
44 protected override TypeAttributes GetAttributeFlagsImpl () {
48 [MethodImplAttribute(MethodImplOptions.InternalCall)]
49 private extern void setup_internal_class (TypeBuilder tb);
51 [MethodImplAttribute(MethodImplOptions.InternalCall)]
52 private extern void create_internal_class (TypeBuilder tb);
54 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr) {
58 fullname = this.tname = "<Module>";
61 setup_internal_class (this);
64 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size) {
68 this.class_size = type_size;
69 this.packing_size = packing_size;
70 sep_index = name.LastIndexOf('.');
71 if (sep_index != -1) {
72 this.tname = name.Substring (sep_index + 1);
73 this.nspace = name.Substring (0, sep_index);
78 if (interfaces != null) {
79 this.interfaces = new Type[interfaces.Length];
80 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
84 table_idx = mb.get_next_table_index (this, 0x02, true);
85 setup_internal_class (this);
86 fullname = GetFullName ();
89 public override Assembly Assembly {
90 get {return pmodule.Assembly;}
92 public override string AssemblyQualifiedName {
94 return fullname + ", " + Assembly.ToString();
97 public override Type BaseType {
102 public override Type DeclaringType {get {return nesting_type;}}
103 public override Type UnderlyingSystemType {
105 if (fields != null) {
106 foreach (FieldBuilder f in fields) {
107 if ((f.Attributes & FieldAttributes.Static) == 0)
111 throw new InvalidOperationException (String.Format ("typebuilder: {0}", this));
115 string GetFullName () {
116 if (nesting_type != null)
117 return String.Concat (nesting_type.FullName, "+", tname);
118 if ((nspace != null) && (nspace.Length > 0))
119 return String.Concat (nspace, ".", tname);
123 public override string FullName {
129 public override Guid GUID {
130 get {return Guid.Empty;}
133 public override Module Module {
134 get {return pmodule;}
136 public override string Name {
139 public override string Namespace {
142 public PackingSize PackingSize {
143 get {return packing_size;}
145 public override Type ReflectedType {get {return nesting_type;}}
146 public override MemberTypes MemberType {
147 get {return MemberTypes.TypeInfo;}
151 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
152 throw new NotImplementedException ();
155 public void AddInterfaceImplementation( Type interfaceType) {
156 if (interfaces != null) {
157 Type[] ifnew = new Type [interfaces.Length + 1];
158 interfaces.CopyTo (ifnew, 0);
159 ifnew [interfaces.Length] = interfaceType;
162 interfaces = new Type [1];
163 interfaces [0] = interfaceType;
168 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
169 throw new NotImplementedException ();
172 public override bool IsDefined( Type attributeType, bool inherit) {
175 public override object[] GetCustomAttributes(bool inherit) {
178 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
183 public TypeBuilder DefineNestedType (string name) {
184 // FIXME: LAMESPEC: what other attributes should we use here as default?
185 return DefineNestedType (name, TypeAttributes.Public, pmodule.assemblyb.corlib_object_type, null);
188 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
189 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
192 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
193 return DefineNestedType (name, attr, parent, null);
196 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packsize, int typesize) {
197 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize);
198 res.nesting_type = this;
199 res.fullname = res.GetFullName ();
200 pmodule.RegisterTypeName (res, res.fullname);
201 if (subtypes != null) {
202 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
203 System.Array.Copy (subtypes, new_types, subtypes.Length);
204 new_types [subtypes.Length] = res;
205 subtypes = new_types;
207 subtypes = new TypeBuilder [1];
213 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
214 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
217 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
218 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
221 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
222 return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
225 public ConstructorBuilder DefineConstructor( MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
226 ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes);
228 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
229 System.Array.Copy (ctors, new_ctors, ctors.Length);
230 new_ctors [ctors.Length] = cb;
233 ctors = new ConstructorBuilder [1];
239 public ConstructorBuilder DefineDefaultConstructor( MethodAttributes attributes) {
240 return DefineConstructor (attributes, CallingConventions.Standard, null);
243 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
244 return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
247 private void append_method (MethodBuilder mb) {
248 if (methods != null) {
249 MethodBuilder[] new_methods = new MethodBuilder [methods.Length+1];
250 System.Array.Copy (methods, new_methods, methods.Length);
251 new_methods [methods.Length] = mb;
252 methods = new_methods;
254 methods = new MethodBuilder [1];
259 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
260 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes);
265 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
266 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes,
267 dllName, entryName, nativeCallConv, nativeCharSet);
272 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
273 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
274 nativeCallConv, nativeCharSet);
277 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
278 if (methodInfoBody is MethodBuilder) {
279 MethodBuilder mb = (MethodBuilder)methodInfoBody;
280 mb.set_override (methodInfoDeclaration);
284 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
285 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes);
286 if (fields != null) {
287 FieldBuilder[] new_fields = new FieldBuilder [fields.Length+1];
288 System.Array.Copy (fields, new_fields, fields.Length);
289 new_fields [fields.Length] = res;
292 fields = new FieldBuilder [1];
294 create_internal_class (this);
299 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
300 PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
302 if (properties != null) {
303 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
304 System.Array.Copy (properties, new_properties, properties.Length);
305 new_properties [properties.Length] = res;
306 properties = new_properties;
308 properties = new PropertyBuilder [1];
309 properties [0] = res;
315 public ConstructorBuilder DefineTypeInitializer() {
316 throw new NotImplementedException ();
319 [MethodImplAttribute(MethodImplOptions.InternalCall)]
320 private extern Type create_runtime_class (TypeBuilder tb);
322 public Type CreateType() {
323 /* handle nesting_type */
324 if (created != null) {
325 string err="type already created: " + created.ToString();
326 throw new InvalidOperationException (err);
328 if (methods != null) {
329 foreach (MethodBuilder method in methods) {
334 foreach (ConstructorBuilder ctor in ctors) {
338 created = create_runtime_class (this);
344 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr) {
346 return new ConstructorInfo [0];
347 ArrayList l = new ArrayList ();
349 MethodAttributes mattrs;
351 foreach (ConstructorBuilder c in ctors) {
353 mattrs = c.Attributes;
354 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
355 if ((bindingAttr & BindingFlags.Public) != 0)
358 if ((bindingAttr & BindingFlags.NonPublic) != 0)
364 if ((mattrs & MethodAttributes.Static) != 0) {
365 if ((bindingAttr & BindingFlags.Static) != 0)
368 if ((bindingAttr & BindingFlags.Instance) != 0)
375 ConstructorInfo[] result = new ConstructorInfo [l.Count];
380 public override Type GetElementType () { return null; }
383 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
384 throw new NotImplementedException ();
387 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
388 return new EventInfo [0];
392 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
394 throw new NotImplementedException ();
397 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
399 return new FieldInfo [0];
400 ArrayList l = new ArrayList ();
402 FieldAttributes mattrs;
404 foreach (FieldInfo c in fields) {
406 mattrs = c.Attributes;
407 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
408 if ((bindingAttr & BindingFlags.Public) != 0)
411 if ((bindingAttr & BindingFlags.NonPublic) != 0)
417 if ((mattrs & FieldAttributes.Static) != 0) {
418 if ((bindingAttr & BindingFlags.Static) != 0)
421 if ((bindingAttr & BindingFlags.Instance) != 0)
428 FieldInfo[] result = new FieldInfo [l.Count];
434 public override Type GetInterface (string name, bool ignoreCase) {
435 throw new NotImplementedException ();
438 public override Type[] GetInterfaces () {
439 if (interfaces != null) {
440 Type[] ret = new Type [interfaces.Length];
441 interfaces.CopyTo (ret, 0);
444 return Type.EmptyTypes;
449 public override MemberInfo[] GetMembers( BindingFlags bindingAttr) {
451 throw new NotImplementedException ();
454 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
456 return new MethodInfo [0];
457 ArrayList l = new ArrayList ();
459 MethodAttributes mattrs;
461 foreach (MethodInfo c in methods) {
463 mattrs = c.Attributes;
464 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
465 if ((bindingAttr & BindingFlags.Public) != 0)
468 if ((bindingAttr & BindingFlags.NonPublic) != 0)
474 if ((mattrs & MethodAttributes.Static) != 0) {
475 if ((bindingAttr & BindingFlags.Static) != 0)
478 if ((bindingAttr & BindingFlags.Instance) != 0)
485 MethodInfo[] result = new MethodInfo [l.Count];
491 protected override MethodInfo GetMethodImpl( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
493 throw new NotImplementedException ();
497 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
499 throw new NotImplementedException ();
502 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
504 ArrayList result = new ArrayList ();
506 if (subtypes == null)
507 return Type.EmptyTypes;
508 foreach (TypeBuilder t in subtypes) {
510 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
511 if ((bindingAttr & BindingFlags.Public) != 0)
514 if ((bindingAttr & BindingFlags.NonPublic) != 0)
521 Type[] r = new Type [result.Count];
526 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
527 if (properties == null)
528 return new PropertyInfo [0];
529 ArrayList l = new ArrayList ();
531 MethodAttributes mattrs;
534 foreach (PropertyInfo c in properties) {
536 accessor = c.GetGetMethod (true);
537 if (accessor == null)
538 accessor = c.GetSetMethod (true);
539 if (accessor == null)
541 mattrs = accessor.Attributes;
542 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
543 if ((bindingAttr & BindingFlags.Public) != 0)
546 if ((bindingAttr & BindingFlags.NonPublic) != 0)
552 if ((mattrs & MethodAttributes.Static) != 0) {
553 if ((bindingAttr & BindingFlags.Static) != 0)
556 if ((bindingAttr & BindingFlags.Instance) != 0)
563 PropertyInfo[] result = new PropertyInfo [l.Count];
569 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
571 throw new NotImplementedException ();
574 protected override bool HasElementTypeImpl () {
575 return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
579 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
581 throw new NotImplementedException ();
584 protected override bool IsArrayImpl () {
585 return type_is_subtype_of (this, typeof (System.Array), false);
587 protected override bool IsByRefImpl () {
591 protected override bool IsCOMObjectImpl () {
594 protected override bool IsPointerImpl () {
598 protected override bool IsPrimitiveImpl () {
602 protected override bool IsValueTypeImpl () {
603 return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
604 this != pmodule.assemblyb.corlib_value_type &&
605 this != pmodule.assemblyb.corlib_enum_type);
608 public override RuntimeTypeHandle TypeHandle { get { return _impl; } }
610 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
611 string attrname = customBuilder.Ctor.ReflectedType.FullName;
612 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
613 byte[] data = customBuilder.Data;
614 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
615 layout_kind = (int)data [2];
616 layout_kind |= ((int)data [3]) << 8;
617 attrs &= ~TypeAttributes.LayoutMask;
618 switch ((LayoutKind)layout_kind) {
619 case LayoutKind.Auto:
620 attrs |= TypeAttributes.AutoLayout;
622 case LayoutKind.Explicit:
623 attrs |= TypeAttributes.ExplicitLayout;
625 case LayoutKind.Sequential:
626 attrs |= TypeAttributes.SequentialLayout;
629 // we should ignore it since it can be any value anyway...
630 throw new Exception ("Error in customattr");
632 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
634 if (first_type_name == "System.Int16")
636 int nnamed = (int)data [pos++];
637 nnamed |= ((int)data [pos++]) << 8;
638 for (int i = 0; i < nnamed; ++i) {
639 byte named_type = data [pos++];
640 byte type = data [pos++];
641 int len = CustomAttributeBuilder.decode_len (data, pos, out pos);
642 string named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
644 /* all the fields are integers in StructLayout */
645 int value = (int)data [pos++];
646 value |= ((int)data [pos++]) << 8;
647 value |= ((int)data [pos++]) << 16;
648 value |= ((int)data [pos++]) << 24;
649 switch (named_name) {
651 switch ((CharSet)value) {
655 case CharSet.Unicode:
656 attrs |= TypeAttributes.UnicodeClass;
659 attrs |= TypeAttributes.AutoClass;
662 break; // error out...
666 packing_size = (PackingSize)value;
672 break; // error out...
676 } else if (attrname == "System.SerializableAttribute") {
677 attrs |= TypeAttributes.Serializable;
680 if (cattrs != null) {
681 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
682 cattrs.CopyTo (new_array, 0);
683 new_array [cattrs.Length] = customBuilder;
686 cattrs = new CustomAttributeBuilder [1];
687 cattrs [0] = customBuilder;
690 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
691 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
694 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
695 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
696 if (events != null) {
697 EventBuilder[] new_events = new EventBuilder [events.Length+1];
698 System.Array.Copy (events, new_events, events.Length);
699 new_events [events.Length] = res;
702 events = new EventBuilder [1];
708 static int InitializedDataCount = 0;
710 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
711 TypeBuilder datablobtype = DefineNestedType ("$ArrayType$"+InitializedDataCount.ToString(),
712 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
713 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, data.Length);
714 datablobtype.packing_size = PackingSize.Size1;
715 datablobtype.class_size = data.Length;
716 datablobtype.CreateType ();
717 FieldBuilder res = DefineField (name, datablobtype, attributes|FieldAttributes.Assembly|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
718 res.SetRVAData (data);
719 InitializedDataCount++;
724 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
725 throw new NotImplementedException ();
728 public TypeToken TypeToken {
730 return new TypeToken (0x02000000 | table_idx);
733 public void SetParent (Type parentType) {
736 internal int get_next_table_index (object obj, int table, bool inc) {
737 return pmodule.get_next_table_index (obj, table, inc);
740 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
743 throw new NotSupportedException ("This method is not implemented for incomplete types.");
745 return created.GetInterfaceMap (interfaceType);