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;
16 namespace System.Reflection.Emit {
17 public sealed class TypeBuilder : Type {
19 private string nspace;
21 private Type[] interfaces;
22 private MethodBuilder[] methods;
23 private PropertyBuilder[] properties;
24 private FieldBuilder[] fields;
25 private TypeAttributes attrs;
26 private int table_idx;
27 internal ModuleBuilder pmodule;
28 private PackingSize packing_size;
30 public const int UnspecifiedTypeSize = -1;
32 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces) {
36 packing_size = PackingSize.Unspecified;
37 sep_index = name.LastIndexOf('.');
38 if (sep_index != -1) {
39 this.tname = name.Substring (sep_index + 1);
40 this.nspace = name.Substring (0, sep_index);
45 if (interfaces != null) {
46 this.interfaces = new Type[interfaces.Length];
47 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
50 table_idx = mb.assemblyb.get_next_table_index (0x02, true);
53 public override Assembly Assembly {get {return null;}}
54 public override string AssemblyQualifiedName {get {return null;}}
55 public override Type BaseType {get {return parent;}}
56 public override Type DeclaringType {get {return null;}}
57 public override string FullName {
60 return String.Concat (nspace, ".", tname);
64 //public override Guid GUID {get {return null;}}
65 public override Module Module {
68 public override string Name {
71 public override string Namespace {
74 public PackingSize PackingSize {
75 get {return packing_size;}
77 public override Type ReflectedType {get {return null;}}
78 public override MemberTypes MemberType { get {return (MemberTypes)0;}}
80 public override bool IsDefined( Type attributeType, bool inherit) {
83 public override object[] GetCustomAttributes(bool inherit) {
86 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
90 public TypeBuilder DefineNestedType (string name) {
91 // FIXME: LAMESPEC: what other attributes should we use here as default?
92 return DefineNestedType (name, TypeAttributes.Public, typeof(object), null);
95 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
96 return DefineNestedType (name, attr, typeof(object), null);
99 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
100 return DefineNestedType (name, attr, parent, null);
103 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
104 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces);
105 /*if (types != null) {
106 TypeBuilder[] new_types = new TypeBuilder [types.Length];
107 System.Array.Copy (types, new_types, types.Length);
108 new_types [types.Length] = res;
111 types = new TypeBuilder [1];
117 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
118 return DefineNestedType (name, attr, parent, null);
121 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
122 return DefineNestedType (name, attr, parent, null);
125 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize, int typesize) {
126 return DefineNestedType (name, attr, parent, null);
129 public ConstructorBuilder DefineConstructor( MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
133 public ConstructorBuilder DefineDefaultConstructor( MethodAttributes attributes) {
137 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
138 return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
141 private void append_method (MethodBuilder mb) {
142 if (methods != null) {
143 MethodBuilder[] new_methods = new MethodBuilder [methods.Length+1];
144 System.Array.Copy (methods, new_methods, methods.Length);
145 new_methods [methods.Length] = mb;
146 methods = new_methods;
148 methods = new MethodBuilder [1];
153 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
154 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes);
159 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
160 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes,
161 dllName, entryName, nativeCallConv, nativeCharSet);
166 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
167 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
168 nativeCallConv, nativeCharSet);
171 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
172 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes);
173 if (fields != null) {
174 FieldBuilder[] new_fields = new FieldBuilder [fields.Length+1];
175 System.Array.Copy (fields, new_fields, fields.Length);
176 new_fields [fields.Length] = res;
179 fields = new FieldBuilder [1];
185 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
186 PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
188 if (properties != null) {
189 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
190 System.Array.Copy (properties, new_properties, properties.Length);
191 new_properties [properties.Length] = res;
192 properties = new_properties;
194 properties = new PropertyBuilder [1];
195 properties [0] = res;
200 public Type CreateType() {
201 if (methods != null) {
202 foreach (MethodBuilder method in methods) {
209 public override Type GetElementType () { return null; }
211 public override Type[] GetInterfaces () { return null; }
213 public override RuntimeTypeHandle TypeHandle { get { return _impl; } }
215 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
217 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
220 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
224 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
228 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {