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);
324 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr) {
326 return new ConstructorInfo [0];
327 ArrayList l = new ArrayList ();
329 MethodAttributes mattrs;
331 foreach (ConstructorBuilder c in ctors) {
333 mattrs = c.Attributes;
334 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
335 if ((bindingAttr & BindingFlags.Public) != 0)
338 if ((bindingAttr & BindingFlags.NonPublic) != 0)
344 if ((mattrs & MethodAttributes.Static) != 0) {
345 if ((bindingAttr & BindingFlags.Static) != 0)
348 if ((bindingAttr & BindingFlags.Instance) != 0)
355 ConstructorInfo[] result = new ConstructorInfo [l.Count];
360 public override Type GetElementType () { return null; }
363 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
364 throw new NotImplementedException ();
367 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
368 return new EventInfo [0];
372 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
374 throw new NotImplementedException ();
377 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
379 return new FieldInfo [0];
380 ArrayList l = new ArrayList ();
382 FieldAttributes mattrs;
384 foreach (FieldInfo c in fields) {
386 mattrs = c.Attributes;
387 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
388 if ((bindingAttr & BindingFlags.Public) != 0)
391 if ((bindingAttr & BindingFlags.NonPublic) != 0)
397 if ((mattrs & FieldAttributes.Static) != 0) {
398 if ((bindingAttr & BindingFlags.Static) != 0)
401 if ((bindingAttr & BindingFlags.Instance) != 0)
408 FieldInfo[] result = new FieldInfo [l.Count];
414 public override Type GetInterface (string name, bool ignoreCase) {
415 throw new NotImplementedException ();
418 public override Type[] GetInterfaces () {
419 if (interfaces != null) {
420 Type[] ret = new Type [interfaces.Length];
421 interfaces.CopyTo (ret, 0);
424 return Type.EmptyTypes;
429 public override MemberInfo[] GetMembers( BindingFlags bindingAttr) {
431 throw new NotImplementedException ();
434 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
436 return new MethodInfo [0];
437 ArrayList l = new ArrayList ();
439 MethodAttributes mattrs;
441 foreach (MethodInfo c in methods) {
443 mattrs = c.Attributes;
444 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
445 if ((bindingAttr & BindingFlags.Public) != 0)
448 if ((bindingAttr & BindingFlags.NonPublic) != 0)
454 if ((mattrs & MethodAttributes.Static) != 0) {
455 if ((bindingAttr & BindingFlags.Static) != 0)
458 if ((bindingAttr & BindingFlags.Instance) != 0)
465 MethodInfo[] result = new MethodInfo [l.Count];
471 protected override MethodInfo GetMethodImpl( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
473 throw new NotImplementedException ();
477 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
479 throw new NotImplementedException ();
482 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
484 ArrayList result = new ArrayList ();
486 if (subtypes == null)
487 return Type.EmptyTypes;
488 foreach (TypeBuilder t in subtypes) {
490 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
491 if ((bindingAttr & BindingFlags.Public) != 0)
494 if ((bindingAttr & BindingFlags.NonPublic) != 0)
501 Type[] r = new Type [result.Count];
506 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
507 if (properties == null)
508 return new PropertyInfo [0];
509 ArrayList l = new ArrayList ();
511 MethodAttributes mattrs;
514 foreach (PropertyInfo c in properties) {
516 accessor = c.GetGetMethod (true);
517 if (accessor == null)
518 accessor = c.GetSetMethod (true);
519 if (accessor == null)
521 mattrs = accessor.Attributes;
522 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
523 if ((bindingAttr & BindingFlags.Public) != 0)
526 if ((bindingAttr & BindingFlags.NonPublic) != 0)
532 if ((mattrs & MethodAttributes.Static) != 0) {
533 if ((bindingAttr & BindingFlags.Static) != 0)
536 if ((bindingAttr & BindingFlags.Instance) != 0)
543 PropertyInfo[] result = new PropertyInfo [l.Count];
549 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
551 throw new NotImplementedException ();
554 protected override bool HasElementTypeImpl () {
555 return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
559 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
561 throw new NotImplementedException ();
564 protected override bool IsArrayImpl () {
565 return type_is_subtype_of (this, typeof (System.Array), false);
567 protected override bool IsByRefImpl () {
571 protected override bool IsCOMObjectImpl () {
574 protected override bool IsPointerImpl () {
578 protected override bool IsPrimitiveImpl () {
582 protected override bool IsValueTypeImpl () {
583 return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
584 this != pmodule.assemblyb.corlib_value_type &&
585 this != pmodule.assemblyb.corlib_enum_type);
588 public override RuntimeTypeHandle TypeHandle { get { return _impl; } }
590 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
591 string attrname = customBuilder.Ctor.ReflectedType.FullName;
592 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
593 byte[] data = customBuilder.Data;
594 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
595 layout_kind = (int)data [2];
596 layout_kind |= ((int)data [3]) << 8;
597 attrs &= ~TypeAttributes.LayoutMask;
598 switch ((LayoutKind)layout_kind) {
599 case LayoutKind.Auto:
600 attrs |= TypeAttributes.AutoLayout;
602 case LayoutKind.Explicit:
603 attrs |= TypeAttributes.ExplicitLayout;
605 case LayoutKind.Sequential:
606 attrs |= TypeAttributes.SequentialLayout;
609 // we should ignore it since it can be any value anyway...
610 throw new Exception ("Error in customattr");
612 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
614 if (first_type_name == "System.Int16")
616 int nnamed = (int)data [pos++];
617 nnamed |= ((int)data [pos++]) << 8;
618 for (int i = 0; i < nnamed; ++i) {
619 byte named_type = data [pos++];
620 byte type = data [pos++];
621 int len = CustomAttributeBuilder.decode_len (data, pos, out pos);
622 string named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
624 /* all the fields are integers in StructLayout */
625 int value = (int)data [pos++];
626 value |= ((int)data [pos++]) << 8;
627 value |= ((int)data [pos++]) << 16;
628 value |= ((int)data [pos++]) << 24;
629 switch (named_name) {
631 switch ((CharSet)value) {
635 case CharSet.Unicode:
636 attrs |= TypeAttributes.UnicodeClass;
639 attrs |= TypeAttributes.AutoClass;
642 break; // error out...
646 packing_size = (PackingSize)value;
652 break; // error out...
656 } else if (attrname == "System.SerializableAttribute") {
657 attrs |= TypeAttributes.Serializable;
660 if (cattrs != null) {
661 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
662 cattrs.CopyTo (new_array, 0);
663 new_array [cattrs.Length] = customBuilder;
666 cattrs = new CustomAttributeBuilder [1];
667 cattrs [0] = customBuilder;
670 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
671 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
674 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
675 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
676 if (events != null) {
677 EventBuilder[] new_events = new EventBuilder [events.Length+1];
678 System.Array.Copy (events, new_events, events.Length);
679 new_events [events.Length] = res;
682 events = new EventBuilder [1];
688 static int InitializedDataCount = 0;
690 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
691 TypeBuilder datablobtype = pmodule.DefineType ("$ArrayType$"+InitializedDataCount.ToString(),
692 TypeAttributes.Public|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
693 pmodule.assemblyb.corlib_value_type, PackingSize.Size1, data.Length);
694 datablobtype.packing_size = PackingSize.Size1;
695 datablobtype.class_size = data.Length;
696 datablobtype.CreateType ();
697 FieldBuilder res = DefineField (name, datablobtype, attributes|FieldAttributes.Assembly|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
698 res.SetRVAData (data);
699 InitializedDataCount++;
704 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
705 throw new NotImplementedException ();
708 public TypeToken TypeToken {
710 return new TypeToken (0x02000000 | table_idx);
713 public void SetParent (Type parentType) {
716 internal int get_next_table_index (object obj, int table, bool inc) {
717 return pmodule.get_next_table_index (obj, table, inc);