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;
41 public const int UnspecifiedTypeSize = 0;
43 protected override TypeAttributes GetAttributeFlagsImpl () {
47 [MethodImplAttribute(MethodImplOptions.InternalCall)]
48 private extern void setup_internal_class (TypeBuilder tb);
50 [MethodImplAttribute(MethodImplOptions.InternalCall)]
51 private extern void create_internal_class (TypeBuilder tb);
53 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size) {
57 this.class_size = type_size;
58 this.packing_size = packing_size;
59 sep_index = name.LastIndexOf('.');
60 if (sep_index != -1) {
61 this.tname = name.Substring (sep_index + 1);
62 this.nspace = name.Substring (0, sep_index);
67 if (interfaces != null) {
68 this.interfaces = new Type[interfaces.Length];
69 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
73 table_idx = mb.get_next_table_index (this, 0x02, true);
74 setup_internal_class (this);
77 public override Assembly Assembly {
78 get {return pmodule.Assembly;}
80 public override string AssemblyQualifiedName {
82 return FullName + ", " + Assembly.ToString();
85 public override Type BaseType {
90 public override Type DeclaringType {get {return nesting_type;}}
91 public override Type UnderlyingSystemType {
94 foreach (FieldBuilder f in fields) {
95 if ((f.Attributes & FieldAttributes.Static) == 0)
99 throw new InvalidOperationException (String.Format ("typebuilder: {0}", this));
103 public override string FullName {
105 if (nesting_type != null)
106 return String.Concat (nesting_type.FullName, "+", tname);
107 if ((nspace != null) && (nspace.Length > 0))
108 return String.Concat (nspace, ".", tname);
113 public override Guid GUID {
114 get {return Guid.Empty;}
117 public override Module Module {
118 get {return pmodule;}
120 public override string Name {
123 public override string Namespace {
126 public PackingSize PackingSize {
127 get {return packing_size;}
129 public override Type ReflectedType {get {return nesting_type;}}
130 public override MemberTypes MemberType {
131 get {return MemberTypes.TypeInfo;}
135 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
136 throw new NotImplementedException ();
139 public void AddInterfaceImplementation( Type interfaceType) {
140 if (interfaces != null) {
141 Type[] ifnew = new Type [interfaces.Length + 1];
142 interfaces.CopyTo (ifnew, 0);
143 ifnew [interfaces.Length] = interfaceType;
146 interfaces = new Type [1];
147 interfaces [0] = interfaceType;
152 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
153 throw new NotImplementedException ();
156 public override bool IsDefined( Type attributeType, bool inherit) {
159 public override object[] GetCustomAttributes(bool inherit) {
162 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
167 public TypeBuilder DefineNestedType (string name) {
168 // FIXME: LAMESPEC: what other attributes should we use here as default?
169 return DefineNestedType (name, TypeAttributes.Public, pmodule.assemblyb.corlib_object_type, null);
172 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
173 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
176 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
177 return DefineNestedType (name, attr, parent, null);
180 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packsize, int typesize) {
181 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize);
182 res.nesting_type = this;
183 if (subtypes != null) {
184 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
185 System.Array.Copy (subtypes, new_types, subtypes.Length);
186 new_types [subtypes.Length] = res;
187 subtypes = new_types;
189 subtypes = new TypeBuilder [1];
195 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
196 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
199 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
200 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
203 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
204 return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
207 public ConstructorBuilder DefineConstructor( MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
208 ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes);
210 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
211 System.Array.Copy (ctors, new_ctors, ctors.Length);
212 new_ctors [ctors.Length] = cb;
215 ctors = new ConstructorBuilder [1];
221 public ConstructorBuilder DefineDefaultConstructor( MethodAttributes attributes) {
222 return DefineConstructor (attributes, CallingConventions.Standard, null);
225 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
226 return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
229 private void append_method (MethodBuilder mb) {
230 if (methods != null) {
231 MethodBuilder[] new_methods = new MethodBuilder [methods.Length+1];
232 System.Array.Copy (methods, new_methods, methods.Length);
233 new_methods [methods.Length] = mb;
234 methods = new_methods;
236 methods = new MethodBuilder [1];
241 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
242 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes);
247 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
248 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes,
249 dllName, entryName, nativeCallConv, nativeCharSet);
254 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
255 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
256 nativeCallConv, nativeCharSet);
259 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
260 if (methodInfoBody is MethodBuilder) {
261 MethodBuilder mb = (MethodBuilder)methodInfoBody;
262 mb.set_override (methodInfoDeclaration);
266 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
267 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes);
268 if (fields != null) {
269 FieldBuilder[] new_fields = new FieldBuilder [fields.Length+1];
270 System.Array.Copy (fields, new_fields, fields.Length);
271 new_fields [fields.Length] = res;
274 fields = new FieldBuilder [1];
276 create_internal_class (this);
281 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
282 PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
284 if (properties != null) {
285 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
286 System.Array.Copy (properties, new_properties, properties.Length);
287 new_properties [properties.Length] = res;
288 properties = new_properties;
290 properties = new PropertyBuilder [1];
291 properties [0] = res;
297 public ConstructorBuilder DefineTypeInitializer() {
298 throw new NotImplementedException ();
301 [MethodImplAttribute(MethodImplOptions.InternalCall)]
302 private extern Type create_runtime_class (TypeBuilder tb);
304 public Type CreateType() {
305 /* handle nesting_type */
307 throw new InvalidOperationException ("type already created");
308 if (methods != null) {
309 foreach (MethodBuilder method in methods) {
314 foreach (ConstructorBuilder ctor in ctors) {
318 created = create_runtime_class (this);
323 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr) {
325 return new ConstructorInfo [0];
326 ArrayList l = new ArrayList ();
328 MethodAttributes mattrs;
330 foreach (ConstructorBuilder c in ctors) {
332 mattrs = c.Attributes;
333 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
334 if ((bindingAttr & BindingFlags.Public) != 0)
337 if ((bindingAttr & BindingFlags.NonPublic) != 0)
343 if ((mattrs & MethodAttributes.Static) != 0) {
344 if ((bindingAttr & BindingFlags.Static) != 0)
347 if ((bindingAttr & BindingFlags.Instance) != 0)
354 ConstructorInfo[] result = new ConstructorInfo [l.Count];
359 public override Type GetElementType () { return null; }
362 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
363 throw new NotImplementedException ();
366 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
367 return new EventInfo [0];
371 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
373 throw new NotImplementedException ();
376 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
378 return new FieldInfo [0];
379 ArrayList l = new ArrayList ();
381 FieldAttributes mattrs;
383 foreach (FieldInfo c in fields) {
385 mattrs = c.Attributes;
386 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
387 if ((bindingAttr & BindingFlags.Public) != 0)
390 if ((bindingAttr & BindingFlags.NonPublic) != 0)
396 if ((mattrs & FieldAttributes.Static) != 0) {
397 if ((bindingAttr & BindingFlags.Static) != 0)
400 if ((bindingAttr & BindingFlags.Instance) != 0)
407 FieldInfo[] result = new FieldInfo [l.Count];
413 public override Type GetInterface (string name, bool ignoreCase) {
414 throw new NotImplementedException ();
417 public override Type[] GetInterfaces () {
418 if (interfaces != null) {
419 Type[] ret = new Type [interfaces.Length];
420 interfaces.CopyTo (ret, 0);
423 return Type.EmptyTypes;
428 public override MemberInfo[] GetMembers( BindingFlags bindingAttr) {
430 throw new NotImplementedException ();
433 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
435 return new MethodInfo [0];
436 ArrayList l = new ArrayList ();
438 MethodAttributes mattrs;
440 foreach (MethodInfo c in methods) {
442 mattrs = c.Attributes;
443 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
444 if ((bindingAttr & BindingFlags.Public) != 0)
447 if ((bindingAttr & BindingFlags.NonPublic) != 0)
453 if ((mattrs & MethodAttributes.Static) != 0) {
454 if ((bindingAttr & BindingFlags.Static) != 0)
457 if ((bindingAttr & BindingFlags.Instance) != 0)
464 MethodInfo[] result = new MethodInfo [l.Count];
470 protected override MethodInfo GetMethodImpl( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
472 throw new NotImplementedException ();
476 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
478 throw new NotImplementedException ();
481 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
483 ArrayList result = new ArrayList ();
485 if (subtypes == null)
486 return Type.EmptyTypes;
487 foreach (TypeBuilder t in subtypes) {
489 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
490 if ((bindingAttr & BindingFlags.Public) != 0)
493 if ((bindingAttr & BindingFlags.NonPublic) != 0)
500 Type[] r = new Type [result.Count];
505 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
506 if (properties == null)
507 return new PropertyInfo [0];
508 ArrayList l = new ArrayList ();
510 MethodAttributes mattrs;
513 foreach (PropertyInfo c in properties) {
515 accessor = c.GetGetMethod (true);
516 if (accessor == null)
517 accessor = c.GetSetMethod (true);
518 if (accessor == null)
520 mattrs = accessor.Attributes;
521 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
522 if ((bindingAttr & BindingFlags.Public) != 0)
525 if ((bindingAttr & BindingFlags.NonPublic) != 0)
531 if ((mattrs & MethodAttributes.Static) != 0) {
532 if ((bindingAttr & BindingFlags.Static) != 0)
535 if ((bindingAttr & BindingFlags.Instance) != 0)
542 PropertyInfo[] result = new PropertyInfo [l.Count];
548 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
550 throw new NotImplementedException ();
553 protected override bool HasElementTypeImpl () {
554 return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
558 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
560 throw new NotImplementedException ();
563 protected override bool IsArrayImpl () {
564 return type_is_subtype_of (this, typeof (System.Array), false);
566 protected override bool IsByRefImpl () {
570 protected override bool IsCOMObjectImpl () {
573 protected override bool IsPointerImpl () {
577 protected override bool IsPrimitiveImpl () {
581 protected override bool IsValueTypeImpl () {
582 return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
583 this != pmodule.assemblyb.corlib_value_type &&
584 this != pmodule.assemblyb.corlib_enum_type);
587 public override RuntimeTypeHandle TypeHandle { get { return _impl; } }
589 private static int decode_len (byte[] data, int pos, out int rpos) {
591 if ((data [pos] & 0x80) == 0) {
592 len = (int)(data [pos++] & 0x7f);
593 } else if ((data [pos] & 0x40) == 0) {
594 len = ((data [pos] & 0x3f) << 8) + data [pos + 1];
597 len = ((data [pos] & 0x1f) << 24) + (data [pos + 1] << 16) + (data [pos + 2] << 8) + data [pos + 3];
604 private static string string_from_bytes (byte[] data, int pos, int len) {
605 char[] chars = new char [len];
606 // FIXME: use a utf8 decoder here
607 for (int i = 0; i < len; ++i)
608 chars [i] = (char)data [pos + i];
609 return new String (chars);
612 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
613 string attrname = customBuilder.Ctor.ReflectedType.FullName;
614 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
615 byte[] data = customBuilder.Data;
616 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
617 layout_kind = (int)data [2];
618 layout_kind |= ((int)data [3]) << 8;
619 attrs &= ~TypeAttributes.LayoutMask;
620 switch ((LayoutKind)layout_kind) {
621 case LayoutKind.Auto:
622 attrs |= TypeAttributes.AutoLayout;
624 case LayoutKind.Explicit:
625 attrs |= TypeAttributes.ExplicitLayout;
627 case LayoutKind.Sequential:
628 attrs |= TypeAttributes.SequentialLayout;
631 // we should ignore it since it can be any value anyway...
632 throw new Exception ("Error in customattr");
634 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
636 if (first_type_name == "System.Int16")
638 int nnamed = (int)data [pos++];
639 nnamed |= ((int)data [pos++]) << 8;
640 for (int i = 0; i < nnamed; ++i) {
641 byte type = data [pos++];
642 int len = decode_len (data, pos, out pos);
643 string named_name = string_from_bytes (data, pos, len);
645 /* all the fields are integers in StructLayout */
646 int value = (int)data [pos++];
647 value |= ((int)data [pos++]) << 8;
648 value |= ((int)data [pos++]) << 16;
649 value |= ((int)data [pos++]) << 24;
650 switch (named_name) {
652 switch ((CharSet)value) {
656 case CharSet.Unicode:
657 attrs |= TypeAttributes.UnicodeClass;
660 attrs |= TypeAttributes.AutoClass;
663 break; // error out...
667 packing_size = (PackingSize)value;
673 break; // error out...
677 } else if (attrname == "System.SerializableAttribute") {
678 attrs |= TypeAttributes.Serializable;
681 if (cattrs != null) {
682 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
683 cattrs.CopyTo (new_array, 0);
684 new_array [cattrs.Length] = customBuilder;
687 cattrs = new CustomAttributeBuilder [1];
688 cattrs [0] = customBuilder;
691 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
692 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
695 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
696 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
697 if (events != null) {
698 EventBuilder[] new_events = new EventBuilder [events.Length+1];
699 System.Array.Copy (events, new_events, events.Length);
700 new_events [events.Length] = res;
703 events = new EventBuilder [1];
709 static int InitializedDataCount = 0;
711 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
712 TypeBuilder datablobtype = pmodule.DefineType ("$ArrayType$"+InitializedDataCount.ToString(),
713 TypeAttributes.Public|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
714 pmodule.assemblyb.corlib_value_type, PackingSize.Size1, data.Length);
715 datablobtype.packing_size = PackingSize.Size1;
716 datablobtype.class_size = data.Length;
717 datablobtype.CreateType ();
718 FieldBuilder res = DefineField (name, datablobtype, attributes|FieldAttributes.Assembly|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
719 res.SetRVAData (data);
720 InitializedDataCount++;
725 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
726 throw new NotImplementedException ();
729 public TypeToken TypeToken {
731 return new TypeToken (0x02000000 | table_idx);
734 public void SetParent (Type parentType) {
737 internal int get_next_table_index (object obj, int table, bool inc) {
738 return pmodule.get_next_table_index (obj, table, inc);