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[] interfaces;
26 private MethodBuilder[] methods;
27 private ConstructorBuilder[] ctors;
28 private PropertyBuilder[] properties;
29 private FieldBuilder[] fields;
30 private CustomAttributeBuilder[] cattrs;
31 internal TypeBuilder[] subtypes;
32 private TypeAttributes attrs;
33 private int table_idx;
34 private ModuleBuilder pmodule;
35 private int class_size;
36 private PackingSize packing_size;
38 public const int UnspecifiedTypeSize = -1;
40 protected override TypeAttributes GetAttributeFlagsImpl () {
44 [MethodImplAttribute(MethodImplOptions.InternalCall)]
45 private extern void setup_internal_class (TypeBuilder tb);
47 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces) {
51 packing_size = PackingSize.Unspecified;
52 sep_index = name.LastIndexOf('.');
53 if (sep_index != -1) {
54 this.tname = name.Substring (sep_index + 1);
55 this.nspace = name.Substring (0, sep_index);
60 if (interfaces != null) {
61 this.interfaces = new Type[interfaces.Length];
62 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
66 table_idx = mb.get_next_table_index (0x02, true);
67 setup_internal_class (this);
70 public override Assembly Assembly {
71 get {return pmodule.Assembly;}
73 public override string AssemblyQualifiedName {get {return null;}}
74 public override Type BaseType {
79 public override Type DeclaringType {get {return null;}}
80 public override Type UnderlyingSystemType {
84 public override string FullName {
86 if ((nspace != null) && (nspace.Length > 0))
87 return String.Concat (nspace, ".", tname);
92 public override Guid GUID {
93 get {return Guid.Empty;}
96 public override Module Module {
99 public override string Name {
102 public override string Namespace {
105 public PackingSize PackingSize {
106 get {return packing_size;}
108 public override Type ReflectedType {get {return parent;}}
109 public override MemberTypes MemberType {
110 get {return MemberTypes.TypeInfo;}
113 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
114 throw new NotImplementedException ();
117 public void AddInterfaceImplementation( Type interfaceType) {
118 throw new NotImplementedException ();
121 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
122 throw new NotImplementedException ();
125 public override bool IsDefined( Type attributeType, bool inherit) {
128 public override object[] GetCustomAttributes(bool inherit) {
131 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
136 public TypeBuilder DefineNestedType (string name) {
137 // FIXME: LAMESPEC: what other attributes should we use here as default?
138 return DefineNestedType (name, TypeAttributes.Public, typeof(object), null);
141 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
142 return DefineNestedType (name, attr, typeof(object), null);
145 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
146 return DefineNestedType (name, attr, parent, null);
149 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
150 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces);
151 if (subtypes != null) {
152 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
153 System.Array.Copy (subtypes, new_types, subtypes.Length);
154 new_types [subtypes.Length] = res;
155 subtypes = new_types;
157 subtypes = new TypeBuilder [1];
163 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
164 return DefineNestedType (name, attr, parent, null);
167 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
168 return DefineNestedType (name, attr, parent, null);
171 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize, int typesize) {
172 return DefineNestedType (name, attr, parent, null);
175 public ConstructorBuilder DefineConstructor( MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
176 ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes);
178 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
179 System.Array.Copy (ctors, new_ctors, ctors.Length);
180 new_ctors [ctors.Length] = cb;
183 ctors = new ConstructorBuilder [1];
189 public ConstructorBuilder DefineDefaultConstructor( MethodAttributes attributes) {
190 return DefineConstructor (attributes, CallingConventions.Standard, null);
193 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
194 return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
197 private void append_method (MethodBuilder mb) {
198 if (methods != null) {
199 MethodBuilder[] new_methods = new MethodBuilder [methods.Length+1];
200 System.Array.Copy (methods, new_methods, methods.Length);
201 new_methods [methods.Length] = mb;
202 methods = new_methods;
204 methods = new MethodBuilder [1];
209 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
210 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes);
215 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
216 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes,
217 dllName, entryName, nativeCallConv, nativeCharSet);
222 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
223 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
224 nativeCallConv, nativeCharSet);
227 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
228 if (methodInfoBody is MethodBuilder) {
229 MethodBuilder mb = (MethodBuilder)methodInfoBody;
230 mb.set_override (methodInfoDeclaration);
234 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
235 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes);
236 if (fields != null) {
237 FieldBuilder[] new_fields = new FieldBuilder [fields.Length+1];
238 System.Array.Copy (fields, new_fields, fields.Length);
239 new_fields [fields.Length] = res;
242 fields = new FieldBuilder [1];
248 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
249 PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
251 if (properties != null) {
252 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
253 System.Array.Copy (properties, new_properties, properties.Length);
254 new_properties [properties.Length] = res;
255 properties = new_properties;
257 properties = new PropertyBuilder [1];
258 properties [0] = res;
263 public ConstructorBuilder DefineTypeInitializer() {
264 throw new NotImplementedException ();
267 public Type CreateType() {
268 if (methods != null) {
269 foreach (MethodBuilder method in methods) {
274 foreach (ConstructorBuilder ctor in ctors) {
281 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr) {
283 return new ConstructorInfo [0];
284 ArrayList l = new ArrayList ();
286 MethodAttributes mattrs;
288 foreach (ConstructorBuilder c in ctors) {
290 mattrs = c.Attributes;
291 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
292 if ((bindingAttr & BindingFlags.Public) != 0)
295 if ((bindingAttr & BindingFlags.NonPublic) != 0)
301 if ((mattrs & MethodAttributes.Static) != 0) {
302 if ((bindingAttr & BindingFlags.Static) != 0)
305 if ((bindingAttr & BindingFlags.Instance) != 0)
312 ConstructorInfo[] result = new ConstructorInfo [l.Count];
317 public override Type GetElementType () { return null; }
319 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
320 throw new NotImplementedException ();
323 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
324 return new EventInfo [0];
327 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
329 throw new NotImplementedException ();
332 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
334 return new FieldInfo [0];
335 ArrayList l = new ArrayList ();
337 FieldAttributes mattrs;
339 foreach (FieldInfo c in fields) {
341 mattrs = c.Attributes;
342 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
343 if ((bindingAttr & BindingFlags.Public) != 0)
346 if ((bindingAttr & BindingFlags.NonPublic) != 0)
352 if ((mattrs & FieldAttributes.Static) != 0) {
353 if ((bindingAttr & BindingFlags.Static) != 0)
356 if ((bindingAttr & BindingFlags.Instance) != 0)
363 FieldInfo[] result = new FieldInfo [l.Count];
368 public override Type GetInterface (string name, bool ignoreCase) {
369 throw new NotImplementedException ();
372 public override Type[] GetInterfaces () {
373 if (interfaces != null) {
376 pi = parent.GetInterfaces ();
378 pi = Type.EmptyTypes;
379 Type[] ret = new Type [interfaces.Length + pi.Length];
380 interfaces.CopyTo (ret, 0);
381 pi.CopyTo (ret, interfaces.Length);
385 return parent.GetInterfaces ();
387 return Type.EmptyTypes;
391 public override MemberInfo[] GetMembers( BindingFlags bindingAttr) {
393 throw new NotImplementedException ();
396 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
398 return new MethodInfo [0];
399 ArrayList l = new ArrayList ();
401 MethodAttributes mattrs;
403 foreach (MethodInfo c in methods) {
405 mattrs = c.Attributes;
406 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
407 if ((bindingAttr & BindingFlags.Public) != 0)
410 if ((bindingAttr & BindingFlags.NonPublic) != 0)
416 if ((mattrs & MethodAttributes.Static) != 0) {
417 if ((bindingAttr & BindingFlags.Static) != 0)
420 if ((bindingAttr & BindingFlags.Instance) != 0)
427 MethodInfo[] result = new MethodInfo [l.Count];
432 protected override MethodInfo GetMethodImpl( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
434 throw new NotImplementedException ();
438 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
440 throw new NotImplementedException ();
444 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
446 throw new NotImplementedException ();
450 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
451 if (properties == null)
452 return new PropertyInfo [0];
453 ArrayList l = new ArrayList ();
455 MethodAttributes mattrs;
458 foreach (PropertyInfo c in properties) {
460 accessor = c.GetGetMethod (true);
461 if (accessor == null)
462 accessor = c.GetSetMethod (true);
463 if (accessor == null)
465 mattrs = accessor.Attributes;
466 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
467 if ((bindingAttr & BindingFlags.Public) != 0)
470 if ((bindingAttr & BindingFlags.NonPublic) != 0)
476 if ((mattrs & MethodAttributes.Static) != 0) {
477 if ((bindingAttr & BindingFlags.Static) != 0)
480 if ((bindingAttr & BindingFlags.Instance) != 0)
487 PropertyInfo[] result = new PropertyInfo [l.Count];
492 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
494 throw new NotImplementedException ();
498 protected override bool HasElementTypeImpl () {
499 return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
502 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
504 throw new NotImplementedException ();
508 protected override bool IsArrayImpl () {
512 protected override bool IsByRefImpl () {
516 protected override bool IsCOMObjectImpl () {
519 protected override bool IsPointerImpl () {
523 protected override bool IsPrimitiveImpl () {
527 protected override bool IsValueTypeImpl () {
529 return type_is_subtype_of (this, typeof (System.ValueType), false);
532 public override RuntimeTypeHandle TypeHandle { get { return _impl; } }
534 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
535 if (cattrs != null) {
536 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
537 cattrs.CopyTo (new_array, 0);
538 new_array [cattrs.Length] = customBuilder;
541 cattrs = new CustomAttributeBuilder [1];
542 cattrs [0] = customBuilder;
545 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
546 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
549 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
550 throw new NotImplementedException ();
553 static int InitializedDataCount = 0;
555 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
556 TypeBuilder datablobtype = pmodule.DefineType ("$ArrayType$"+InitializedDataCount.ToString(),
557 TypeAttributes.Public|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
558 typeof (System.ValueType), PackingSize.Size1, data.Length);
559 datablobtype.packing_size = PackingSize.Size1;
560 datablobtype.class_size = data.Length;
561 datablobtype.CreateType ();
562 FieldBuilder res = DefineField (name, datablobtype, attributes|FieldAttributes.Assembly|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
563 res.SetRVAData (data);
564 InitializedDataCount++;
568 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
569 throw new NotImplementedException ();
573 public void SetParent (Type parentType) {
576 internal int get_next_table_index (int table, bool inc) {
577 return pmodule.get_next_table_index (table, inc);