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;
40 public const int UnspecifiedTypeSize = -1;
42 protected override TypeAttributes GetAttributeFlagsImpl () {
46 [MethodImplAttribute(MethodImplOptions.InternalCall)]
47 private extern void setup_internal_class (TypeBuilder tb);
49 [MethodImplAttribute(MethodImplOptions.InternalCall)]
50 private extern void create_internal_class (TypeBuilder tb);
52 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size) {
56 this.class_size = type_size;
57 this.packing_size = packing_size;
58 sep_index = name.LastIndexOf('.');
59 if (sep_index != -1) {
60 this.tname = name.Substring (sep_index + 1);
61 this.nspace = name.Substring (0, sep_index);
66 if (interfaces != null) {
67 this.interfaces = new Type[interfaces.Length];
68 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
72 table_idx = mb.get_next_table_index (this, 0x02, true);
73 setup_internal_class (this);
76 public override Assembly Assembly {
77 get {return pmodule.Assembly;}
79 public override string AssemblyQualifiedName {
81 return FullName + ", " + Assembly.ToString();
84 public override Type BaseType {
89 public override Type DeclaringType {get {return null;}}
90 public override Type UnderlyingSystemType {
93 foreach (FieldBuilder f in fields) {
94 if ((f.Attributes & FieldAttributes.Static) == 0)
98 throw new InvalidOperationException (String.Format ("typebuilder: {0}", this));
102 public override string FullName {
104 if (nesting_type != null)
105 return String.Concat (nesting_type.FullName, "+", tname);
106 if ((nspace != null) && (nspace.Length > 0))
107 return String.Concat (nspace, ".", tname);
112 public override Guid GUID {
113 get {return Guid.Empty;}
116 public override Module Module {
117 get {return pmodule;}
119 public override string Name {
122 public override string Namespace {
125 public PackingSize PackingSize {
126 get {return packing_size;}
128 public override Type ReflectedType {get {return parent;}}
129 public override MemberTypes MemberType {
130 get {return MemberTypes.TypeInfo;}
134 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
135 throw new NotImplementedException ();
138 public void AddInterfaceImplementation( Type interfaceType) {
139 if (interfaces != null) {
140 Type[] ifnew = new Type [interfaces.Length + 1];
141 interfaces.CopyTo (ifnew, 0);
142 ifnew [interfaces.Length] = interfaceType;
145 interfaces = new Type [1];
146 interfaces [0] = interfaceType;
151 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
152 throw new NotImplementedException ();
155 public override bool IsDefined( Type attributeType, bool inherit) {
158 public override object[] GetCustomAttributes(bool inherit) {
161 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
166 public TypeBuilder DefineNestedType (string name) {
167 // FIXME: LAMESPEC: what other attributes should we use here as default?
168 return DefineNestedType (name, TypeAttributes.Public, pmodule.assemblyb.corlib_object_type, null);
171 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
172 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
175 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
176 return DefineNestedType (name, attr, parent, null);
179 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packsize, int typesize) {
180 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize);
181 res.nesting_type = this;
182 if (subtypes != null) {
183 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
184 System.Array.Copy (subtypes, new_types, subtypes.Length);
185 new_types [subtypes.Length] = res;
186 subtypes = new_types;
188 subtypes = new TypeBuilder [1];
194 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
195 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
198 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
199 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
202 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
203 return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
206 public ConstructorBuilder DefineConstructor( MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
207 ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes);
209 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
210 System.Array.Copy (ctors, new_ctors, ctors.Length);
211 new_ctors [ctors.Length] = cb;
214 ctors = new ConstructorBuilder [1];
220 public ConstructorBuilder DefineDefaultConstructor( MethodAttributes attributes) {
221 return DefineConstructor (attributes, CallingConventions.Standard, null);
224 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
225 return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
228 private void append_method (MethodBuilder mb) {
229 if (methods != null) {
230 MethodBuilder[] new_methods = new MethodBuilder [methods.Length+1];
231 System.Array.Copy (methods, new_methods, methods.Length);
232 new_methods [methods.Length] = mb;
233 methods = new_methods;
235 methods = new MethodBuilder [1];
240 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
241 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes);
246 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
247 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes,
248 dllName, entryName, nativeCallConv, nativeCharSet);
253 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
254 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
255 nativeCallConv, nativeCharSet);
258 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
259 if (methodInfoBody is MethodBuilder) {
260 MethodBuilder mb = (MethodBuilder)methodInfoBody;
261 mb.set_override (methodInfoDeclaration);
265 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
266 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes);
267 if (fields != null) {
268 FieldBuilder[] new_fields = new FieldBuilder [fields.Length+1];
269 System.Array.Copy (fields, new_fields, fields.Length);
270 new_fields [fields.Length] = res;
273 fields = new FieldBuilder [1];
275 create_internal_class (this);
280 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
281 PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
283 if (properties != null) {
284 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
285 System.Array.Copy (properties, new_properties, properties.Length);
286 new_properties [properties.Length] = res;
287 properties = new_properties;
289 properties = new PropertyBuilder [1];
290 properties [0] = res;
296 public ConstructorBuilder DefineTypeInitializer() {
297 throw new NotImplementedException ();
300 public Type CreateType() {
301 if (methods != null) {
302 foreach (MethodBuilder method in methods) {
307 foreach (ConstructorBuilder ctor in ctors) {
315 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr) {
317 return new ConstructorInfo [0];
318 ArrayList l = new ArrayList ();
320 MethodAttributes mattrs;
322 foreach (ConstructorBuilder c in ctors) {
324 mattrs = c.Attributes;
325 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
326 if ((bindingAttr & BindingFlags.Public) != 0)
329 if ((bindingAttr & BindingFlags.NonPublic) != 0)
335 if ((mattrs & MethodAttributes.Static) != 0) {
336 if ((bindingAttr & BindingFlags.Static) != 0)
339 if ((bindingAttr & BindingFlags.Instance) != 0)
346 ConstructorInfo[] result = new ConstructorInfo [l.Count];
351 public override Type GetElementType () { return null; }
354 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
355 throw new NotImplementedException ();
358 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
359 return new EventInfo [0];
363 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
365 throw new NotImplementedException ();
368 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
370 return new FieldInfo [0];
371 ArrayList l = new ArrayList ();
373 FieldAttributes mattrs;
375 foreach (FieldInfo c in fields) {
377 mattrs = c.Attributes;
378 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
379 if ((bindingAttr & BindingFlags.Public) != 0)
382 if ((bindingAttr & BindingFlags.NonPublic) != 0)
388 if ((mattrs & FieldAttributes.Static) != 0) {
389 if ((bindingAttr & BindingFlags.Static) != 0)
392 if ((bindingAttr & BindingFlags.Instance) != 0)
399 FieldInfo[] result = new FieldInfo [l.Count];
405 public override Type GetInterface (string name, bool ignoreCase) {
406 throw new NotImplementedException ();
409 public override Type[] GetInterfaces () {
410 if (interfaces != null) {
411 Type[] ret = new Type [interfaces.Length];
412 interfaces.CopyTo (ret, 0);
415 return Type.EmptyTypes;
420 public override MemberInfo[] GetMembers( BindingFlags bindingAttr) {
422 throw new NotImplementedException ();
425 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
427 return new MethodInfo [0];
428 ArrayList l = new ArrayList ();
430 MethodAttributes mattrs;
432 foreach (MethodInfo c in methods) {
434 mattrs = c.Attributes;
435 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
436 if ((bindingAttr & BindingFlags.Public) != 0)
439 if ((bindingAttr & BindingFlags.NonPublic) != 0)
445 if ((mattrs & MethodAttributes.Static) != 0) {
446 if ((bindingAttr & BindingFlags.Static) != 0)
449 if ((bindingAttr & BindingFlags.Instance) != 0)
456 MethodInfo[] result = new MethodInfo [l.Count];
462 protected override MethodInfo GetMethodImpl( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
464 throw new NotImplementedException ();
468 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
470 throw new NotImplementedException ();
473 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
475 ArrayList result = new ArrayList ();
477 if (subtypes == null)
478 return Type.EmptyTypes;
479 foreach (TypeBuilder t in subtypes) {
481 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
482 if ((bindingAttr & BindingFlags.Public) != 0)
485 if ((bindingAttr & BindingFlags.NonPublic) != 0)
492 Type[] r = new Type [result.Count];
497 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
498 if (properties == null)
499 return new PropertyInfo [0];
500 ArrayList l = new ArrayList ();
502 MethodAttributes mattrs;
505 foreach (PropertyInfo c in properties) {
507 accessor = c.GetGetMethod (true);
508 if (accessor == null)
509 accessor = c.GetSetMethod (true);
510 if (accessor == null)
512 mattrs = accessor.Attributes;
513 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
514 if ((bindingAttr & BindingFlags.Public) != 0)
517 if ((bindingAttr & BindingFlags.NonPublic) != 0)
523 if ((mattrs & MethodAttributes.Static) != 0) {
524 if ((bindingAttr & BindingFlags.Static) != 0)
527 if ((bindingAttr & BindingFlags.Instance) != 0)
534 PropertyInfo[] result = new PropertyInfo [l.Count];
540 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
542 throw new NotImplementedException ();
545 protected override bool HasElementTypeImpl () {
546 return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
550 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
552 throw new NotImplementedException ();
555 protected override bool IsArrayImpl () {
556 return type_is_subtype_of (this, typeof (System.Array), false);
558 protected override bool IsByRefImpl () {
562 protected override bool IsCOMObjectImpl () {
565 protected override bool IsPointerImpl () {
569 protected override bool IsPrimitiveImpl () {
573 protected override bool IsValueTypeImpl () {
574 //Console.WriteLine ("is value type: {0}: {1} ({2}) base: {3}", AssemblyQualifiedName, type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false), pmodule.assemblyb.corlib_value_type.AssemblyQualifiedName, BaseType != null?BaseType.AssemblyQualifiedName: "");
575 return (type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false));
576 // this != pmodule.assemblyb.corlib_value_type &&
577 // this != typeof (System.Enum); /* FIXME: handle a TypeBuilder System.Enum */
579 /* return type_is_subtype_of (this, typeof(System.ValueType), false) &&
580 this != typeof(System.ValueType) &&
581 this != typeof (System.Enum);*/
584 public override RuntimeTypeHandle TypeHandle { get { return _impl; } }
586 private static int decode_len (byte[] data, int pos, out int rpos) {
588 if ((data [pos] & 0x80) == 0) {
589 len = (int)(data [pos++] & 0x7f);
590 } else if ((data [pos] & 0x40) == 0) {
591 len = ((data [pos] & 0x3f) << 8) + data [pos + 1];
594 len = ((data [pos] & 0x1f) << 24) + (data [pos + 1] << 16) + (data [pos + 2] << 8) + data [pos + 3];
601 private static string string_from_bytes (byte[] data, int pos, int len) {
602 char[] chars = new char [len];
603 // FIXME: use a utf8 decoder here
604 for (int i = 0; i < len; ++i)
605 chars [i] = (char)data [pos + i];
606 return new String (chars);
609 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
610 string attrname = customBuilder.Ctor.ReflectedType.FullName;
611 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
612 byte[] data = customBuilder.Data;
613 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
614 layout_kind = (int)data [2];
615 layout_kind |= ((int)data [3]) << 8;
616 attrs &= ~TypeAttributes.LayoutMask;
617 switch ((LayoutKind)layout_kind) {
618 case LayoutKind.Auto:
619 attrs |= TypeAttributes.AutoLayout;
621 case LayoutKind.Explicit:
622 attrs |= TypeAttributes.ExplicitLayout;
624 case LayoutKind.Sequential:
625 attrs |= TypeAttributes.SequentialLayout;
628 // we should ignore it since it can be any value anyway...
629 throw new Exception ("Error in customattr");
631 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
633 if (first_type_name == "System.Int16")
635 int nnamed = (int)data [pos++];
636 nnamed |= ((int)data [pos++]) << 8;
637 for (int i = 0; i < nnamed; ++i) {
638 byte type = data [pos++];
639 int len = decode_len (data, pos, out pos);
640 string named_name = string_from_bytes (data, pos, len);
642 /* all the fields are integers in StructLayout */
643 int value = (int)data [pos++];
644 value |= ((int)data [pos++]) << 8;
645 value |= ((int)data [pos++]) << 16;
646 value |= ((int)data [pos++]) << 24;
647 switch (named_name) {
649 switch ((CharSet)value) {
653 case CharSet.Unicode:
654 attrs |= TypeAttributes.UnicodeClass;
657 attrs |= TypeAttributes.AutoClass;
660 break; // error out...
664 packing_size = (PackingSize)value;
670 break; // error out...
674 } else if (attrname == "System.SerializableAttribute") {
675 attrs |= TypeAttributes.Serializable;
678 if (cattrs != null) {
679 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
680 cattrs.CopyTo (new_array, 0);
681 new_array [cattrs.Length] = customBuilder;
684 cattrs = new CustomAttributeBuilder [1];
685 cattrs [0] = customBuilder;
688 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
689 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
692 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
693 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
694 if (events != null) {
695 EventBuilder[] new_events = new EventBuilder [events.Length+1];
696 System.Array.Copy (events, new_events, events.Length);
697 new_events [events.Length] = res;
700 events = new EventBuilder [1];
706 static int InitializedDataCount = 0;
708 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
709 TypeBuilder datablobtype = pmodule.DefineType ("$ArrayType$"+InitializedDataCount.ToString(),
710 TypeAttributes.Public|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
711 pmodule.assemblyb.corlib_value_type, PackingSize.Size1, data.Length);
712 datablobtype.packing_size = PackingSize.Size1;
713 datablobtype.class_size = data.Length;
714 datablobtype.CreateType ();
715 FieldBuilder res = DefineField (name, datablobtype, attributes|FieldAttributes.Assembly|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
716 res.SetRVAData (data);
717 InitializedDataCount++;
722 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
723 throw new NotImplementedException ();
726 public TypeToken TypeToken {
728 return new TypeToken (0x02000000 | table_idx);
731 public void SetParent (Type parentType) {
734 internal int get_next_table_index (object obj, int table, bool inc) {
735 return pmodule.get_next_table_index (obj, table, inc);