2 Copyright (C) 2008-2011 Jeroen Frijters
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
25 using System.Collections.Generic;
26 using System.Diagnostics;
27 using System.Runtime.InteropServices;
28 using IKVM.Reflection.Impl;
29 using IKVM.Reflection.Metadata;
30 using IKVM.Reflection.Writer;
32 namespace IKVM.Reflection.Emit
34 public sealed class GenericTypeParameterBuilder : Type
36 private readonly string name;
37 private readonly TypeBuilder type;
38 private readonly MethodBuilder method;
39 private readonly int paramPseudoIndex;
40 private readonly int position;
41 private int typeToken;
42 private Type baseType;
43 private GenericParameterAttributes attr;
45 internal GenericTypeParameterBuilder(string name, TypeBuilder type, MethodBuilder method, int position)
50 this.position = position;
51 GenericParamTable.Record rec = new GenericParamTable.Record();
52 rec.Number = (short)position;
54 rec.Owner = type != null ? type.MetadataToken : method.MetadataToken;
55 rec.Name = this.ModuleBuilder.Strings.Add(name);
56 this.paramPseudoIndex = this.ModuleBuilder.GenericParam.AddRecord(rec);
59 public override string AssemblyQualifiedName
64 public override bool IsValueType
66 get { return (this.GenericParameterAttributes & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0; }
69 public override Type BaseType
71 get { return baseType; }
74 public override Type[] __GetDeclaredInterfaces()
76 throw new NotImplementedException();
79 public override TypeAttributes Attributes
81 get { return TypeAttributes.Public; }
84 public override string Namespace
86 get { return DeclaringType.Namespace; }
89 public override string Name
94 public override string FullName
99 public override string ToString()
104 private ModuleBuilder ModuleBuilder
106 get { return type != null ? type.ModuleBuilder : method.ModuleBuilder; }
109 public override Module Module
111 get { return ModuleBuilder; }
114 public override bool IsGenericParameter
119 public override int GenericParameterPosition
121 get { return position; }
124 public override Type DeclaringType
129 public override MethodBase DeclaringMethod
131 get { return method; }
134 public override Type[] GetGenericParameterConstraints()
136 throw new NotImplementedException();
139 public override GenericParameterAttributes GenericParameterAttributes
155 private void AddConstraint(Type type)
157 GenericParamConstraintTable.Record rec = new GenericParamConstraintTable.Record();
158 rec.Owner = paramPseudoIndex;
159 rec.Constraint = this.ModuleBuilder.GetTypeTokenForMemberRef(type);
160 this.ModuleBuilder.GenericParamConstraint.AddRecord(rec);
163 public void SetBaseTypeConstraint(Type baseTypeConstraint)
165 this.baseType = baseTypeConstraint;
166 AddConstraint(baseTypeConstraint);
169 public void SetInterfaceConstraints(params Type[] interfaceConstraints)
171 foreach (Type type in interfaceConstraints)
177 public void SetGenericParameterAttributes(GenericParameterAttributes genericParameterAttributes)
179 this.attr = genericParameterAttributes;
180 // for now we'll back patch the table
181 this.ModuleBuilder.GenericParam.PatchAttribute(paramPseudoIndex, genericParameterAttributes);
184 public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
186 this.ModuleBuilder.SetCustomAttribute((GenericParamTable.Index << 24) | paramPseudoIndex, customBuilder);
189 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
191 SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
194 internal override int GetModuleBuilderToken()
198 ByteBuffer spec = new ByteBuffer(5);
199 Signature.WriteTypeSpec(this.ModuleBuilder, spec, this);
200 typeToken = 0x1B000000 | this.ModuleBuilder.TypeSpec.AddRecord(this.ModuleBuilder.Blobs.Add(spec));
205 internal override Type BindTypeParameters(IGenericBinder binder)
209 return binder.BindTypeParameter(this);
213 return binder.BindMethodParameter(this);
218 public sealed class TypeBuilder : Type, ITypeOwner
220 public const int UnspecifiedTypeSize = 0;
221 private readonly ITypeOwner owner;
222 private readonly int token;
224 private Type lazyBaseType; // (lazyBaseType == null && attribs & TypeAttributes.Interface) == 0) => BaseType == System.Object
225 private readonly int typeName;
226 private readonly int typeNameSpace;
227 private readonly string ns;
228 private readonly string name;
229 private readonly List<MethodBuilder> methods = new List<MethodBuilder>();
230 private readonly List<FieldBuilder> fields = new List<FieldBuilder>();
231 private List<PropertyBuilder> properties;
232 private List<EventBuilder> events;
233 private TypeAttributes attribs;
234 private GenericTypeParameterBuilder[] gtpb;
235 private List<CustomAttributeBuilder> declarativeSecurity;
236 private List<Type> interfaces;
239 private bool hasLayout;
241 internal TypeBuilder(ITypeOwner owner, string ns, string name)
244 this.token = this.ModuleBuilder.TypeDef.AllocToken();
247 this.typeNameSpace = ns == null ? 0 : this.ModuleBuilder.Strings.Add(ns);
248 this.typeName = this.ModuleBuilder.Strings.Add(name);
249 MarkEnumOrValueType(ns, name);
252 public ConstructorBuilder DefineDefaultConstructor(MethodAttributes attributes)
254 ConstructorBuilder cb = DefineConstructor(attributes, CallingConventions.Standard, Type.EmptyTypes);
255 ILGenerator ilgen = cb.GetILGenerator();
256 ilgen.Emit(OpCodes.Ldarg_0);
257 ilgen.Emit(OpCodes.Call, BaseType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null));
258 ilgen.Emit(OpCodes.Ret);
262 public ConstructorBuilder DefineConstructor(MethodAttributes attribs, CallingConventions callConv, Type[] parameterTypes)
264 return DefineConstructor(attribs, callConv, parameterTypes, null, null);
267 public ConstructorBuilder DefineConstructor(MethodAttributes attribs, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
269 attribs |= MethodAttributes.RTSpecialName | MethodAttributes.SpecialName;
270 string name = (attribs & MethodAttributes.Static) == 0 ? ConstructorInfo.ConstructorName : ConstructorInfo.TypeConstructorName;
271 MethodBuilder mb = DefineMethod(name, attribs, callingConvention, null, null, null, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
272 return new ConstructorBuilder(mb);
275 public ConstructorBuilder DefineTypeInitializer()
277 MethodBuilder mb = DefineMethod(ConstructorInfo.TypeConstructorName, MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName, null, Type.EmptyTypes);
278 return new ConstructorBuilder(mb);
281 private MethodBuilder CreateMethodBuilder(string name, MethodAttributes attributes, CallingConventions callingConvention)
283 this.ModuleBuilder.MethodDef.AddVirtualRecord();
284 MethodBuilder mb = new MethodBuilder(this, name, attributes, callingConvention);
289 public MethodBuilder DefineMethod(string name, MethodAttributes attribs)
291 return DefineMethod(name, attribs, CallingConventions.Standard);
294 public MethodBuilder DefineMethod(string name, MethodAttributes attribs, CallingConventions callingConvention)
296 return CreateMethodBuilder(name, attribs, callingConvention);
299 public MethodBuilder DefineMethod(string name, MethodAttributes attribs, Type returnType, Type[] parameterTypes)
301 return DefineMethod(name, attribs, CallingConventions.Standard, returnType, null, null, parameterTypes, null, null);
304 public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
306 return DefineMethod(name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
309 public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
311 MethodBuilder mb = CreateMethodBuilder(name, attributes, callingConvention);
312 mb.SetSignature(returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
316 public MethodBuilder DefinePInvokeMethod(string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
318 return DefinePInvokeMethod(name, dllName, null, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
321 public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
323 return DefinePInvokeMethod(name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
326 public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention,
327 Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers,
328 Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers,
329 CallingConvention nativeCallConv, CharSet nativeCharSet)
331 MethodBuilder mb = DefineMethod(name, attributes | MethodAttributes.PinvokeImpl, callingConvention,
332 returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers,
333 parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
334 mb.SetDllImportPseudoCustomAttribute(dllName, entryName, nativeCallConv, nativeCharSet, null, null, null, null, null);
338 public void DefineMethodOverride(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)
340 MethodImplTable.Record rec = new MethodImplTable.Record();
342 rec.MethodBody = this.ModuleBuilder.GetMethodToken(methodInfoBody).Token;
343 rec.MethodDeclaration = this.ModuleBuilder.GetMethodToken(methodInfoDeclaration).Token;
344 this.ModuleBuilder.MethodImpl.AddRecord(rec);
347 public FieldBuilder DefineField(string name, Type fieldType, FieldAttributes attribs)
349 return DefineField(name, fieldType, null, null, attribs);
352 public FieldBuilder DefineField(string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes)
354 return __DefineField(fieldName, type, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers), attributes);
357 public FieldBuilder __DefineField(string fieldName, Type type, CustomModifiers customModifiers, FieldAttributes attributes)
359 FieldBuilder fb = new FieldBuilder(this, fieldName, type, customModifiers, attributes);
364 public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes)
366 return DefineProperty(name, attributes, returnType, null, null, parameterTypes, null, null);
369 public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers,
370 Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
372 return DefinePropertyImpl(name, attributes, CallingConventions.Standard, true, returnType, parameterTypes,
373 PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, Util.NullSafeLength(parameterTypes)));
376 public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, CallingConventions callingConvention,
377 Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers,
378 Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
380 return DefinePropertyImpl(name, attributes, callingConvention, false, returnType, parameterTypes,
381 PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, Util.NullSafeLength(parameterTypes)));
384 public PropertyBuilder __DefineProperty(string name, PropertyAttributes attributes, CallingConventions callingConvention,
385 Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
387 return DefinePropertyImpl(name, attributes, callingConvention, false, returnType, parameterTypes,
388 PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, Util.NullSafeLength(parameterTypes)));
391 private PropertyBuilder DefinePropertyImpl(string name, PropertyAttributes attributes, CallingConventions callingConvention, bool patchCallingConvention,
392 Type returnType, Type[] parameterTypes, PackedCustomModifiers customModifiers)
394 if (properties == null)
396 properties = new List<PropertyBuilder>();
398 PropertySignature sig = PropertySignature.Create(callingConvention, returnType, parameterTypes, customModifiers);
399 PropertyBuilder pb = new PropertyBuilder(this, name, attributes, sig, patchCallingConvention);
404 public EventBuilder DefineEvent(string name, EventAttributes attributes, Type eventtype)
408 events = new List<EventBuilder>();
410 EventBuilder eb = new EventBuilder(this, name, attributes, eventtype);
415 public TypeBuilder DefineNestedType(string name)
417 return DefineNestedType(name, TypeAttributes.Class | TypeAttributes.NestedPrivate);
420 public TypeBuilder DefineNestedType(string name, TypeAttributes attribs)
422 return DefineNestedType(name, attribs, null);
425 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, Type[] interfaces)
427 TypeBuilder tb = DefineNestedType(name, attr, parent);
428 if (interfaces != null)
430 foreach (Type iface in interfaces)
432 tb.AddInterfaceImplementation(iface);
438 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent)
440 return DefineNestedType(name, attr, parent, 0);
443 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, int typeSize)
445 return DefineNestedType(name, attr, parent, PackingSize.Unspecified, typeSize);
448 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, PackingSize packSize)
450 return DefineNestedType(name, attr, parent, packSize, 0);
453 private TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, PackingSize packSize, int typeSize)
456 int lastdot = name.LastIndexOf('.');
459 ns = name.Substring(0, lastdot);
460 name = name.Substring(lastdot + 1);
462 TypeBuilder typeBuilder = __DefineNestedType(ns, name);
463 typeBuilder.__SetAttributes(attr);
464 typeBuilder.SetParent(parent);
465 if (packSize != PackingSize.Unspecified || typeSize != 0)
467 typeBuilder.__SetLayout((int)packSize, typeSize);
472 public TypeBuilder __DefineNestedType(string ns, string name)
474 this.typeFlags |= TypeFlags.HasNestedTypes;
475 TypeBuilder typeBuilder = this.ModuleBuilder.DefineType(this, ns, name);
476 NestedClassTable.Record rec = new NestedClassTable.Record();
477 rec.NestedClass = typeBuilder.MetadataToken;
478 rec.EnclosingClass = this.MetadataToken;
479 this.ModuleBuilder.NestedClass.AddRecord(rec);
483 public void SetParent(Type parent)
485 lazyBaseType = parent;
488 public void AddInterfaceImplementation(Type interfaceType)
490 if (interfaces == null)
492 interfaces = new List<Type>();
494 interfaces.Add(interfaceType);
497 public void __SetInterfaceImplementationCustomAttribute(Type interfaceType, CustomAttributeBuilder cab)
499 this.ModuleBuilder.SetInterfaceImplementationCustomAttribute(this, interfaceType, cab);
507 public PackingSize PackingSize
509 get { return (PackingSize)pack; }
512 public override bool __GetLayout(out int packingSize, out int typeSize)
514 packingSize = this.pack;
515 typeSize = this.size;
519 public void __SetLayout(int packingSize, int typesize)
521 this.pack = (short)packingSize;
522 this.size = typesize;
523 this.hasLayout = true;
526 private void SetStructLayoutPseudoCustomAttribute(CustomAttributeBuilder customBuilder)
528 object val = customBuilder.GetConstructorArgument(0);
532 layout = (LayoutKind)(short)val;
536 layout = (LayoutKind)val;
538 StructLayoutAttribute attr = new StructLayoutAttribute(layout);
539 attr.Pack = (int?)customBuilder.GetFieldValue("Pack") ?? 0;
540 attr.Size = (int?)customBuilder.GetFieldValue("Size") ?? 0;
541 attr.CharSet = customBuilder.GetFieldValue<CharSet>("CharSet") ?? CharSet.None;
542 attribs &= ~TypeAttributes.LayoutMask;
545 case LayoutKind.Auto:
546 attribs |= TypeAttributes.AutoLayout;
548 case LayoutKind.Explicit:
549 attribs |= TypeAttributes.ExplicitLayout;
551 case LayoutKind.Sequential:
552 attribs |= TypeAttributes.SequentialLayout;
555 attribs &= ~TypeAttributes.StringFormatMask;
556 switch (attr.CharSet)
560 attribs |= TypeAttributes.AnsiClass;
563 attribs |= TypeAttributes.AutoClass;
565 case CharSet.Unicode:
566 attribs |= TypeAttributes.UnicodeClass;
569 pack = (short)attr.Pack;
571 hasLayout = pack != 0 || size != 0;
574 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
576 SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
579 public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
581 Universe u = this.ModuleBuilder.universe;
582 Type type = customBuilder.Constructor.DeclaringType;
583 if (type == u.System_Runtime_InteropServices_StructLayoutAttribute)
585 SetStructLayoutPseudoCustomAttribute(customBuilder.DecodeBlob(this.Assembly));
587 else if (type == u.System_SerializableAttribute)
589 attribs |= TypeAttributes.Serializable;
591 else if (type == u.System_Runtime_InteropServices_ComImportAttribute)
593 attribs |= TypeAttributes.Import;
595 else if (type == u.System_Runtime_CompilerServices_SpecialNameAttribute)
597 attribs |= TypeAttributes.SpecialName;
601 if (type == u.System_Security_SuppressUnmanagedCodeSecurityAttribute)
603 attribs |= TypeAttributes.HasSecurity;
605 this.ModuleBuilder.SetCustomAttribute(token, customBuilder);
609 public void __AddDeclarativeSecurity(CustomAttributeBuilder customBuilder)
611 attribs |= TypeAttributes.HasSecurity;
612 if (declarativeSecurity == null)
614 declarativeSecurity = new List<CustomAttributeBuilder>();
616 declarativeSecurity.Add(customBuilder);
619 public void AddDeclarativeSecurity(System.Security.Permissions.SecurityAction securityAction, System.Security.PermissionSet permissionSet)
621 this.ModuleBuilder.AddDeclarativeSecurity(token, securityAction, permissionSet);
622 this.attribs |= TypeAttributes.HasSecurity;
625 public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names)
627 typeFlags |= TypeFlags.IsGenericTypeDefinition;
628 gtpb = new GenericTypeParameterBuilder[names.Length];
629 for (int i = 0; i < names.Length; i++)
631 gtpb[i] = new GenericTypeParameterBuilder(names[i], this, null, i);
633 return (GenericTypeParameterBuilder[])gtpb.Clone();
636 public override Type[] GetGenericArguments()
638 return Util.Copy(gtpb);
641 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
643 return gtpb == null ? Empty<CustomModifiers>.Array : new CustomModifiers[gtpb.Length];
646 internal override Type GetGenericTypeArgument(int index)
651 public override bool ContainsGenericParameters
653 get { return gtpb != null; }
656 public override Type GetGenericTypeDefinition()
661 public Type CreateType()
663 if ((typeFlags & TypeFlags.Baked) != 0)
665 // .NET allows multiple invocations (subsequent invocations return the same baked type)
666 throw new NotImplementedException();
668 typeFlags |= TypeFlags.Baked;
671 ClassLayoutTable.Record rec = new ClassLayoutTable.Record();
672 rec.PackingSize = pack;
673 rec.ClassSize = size;
675 this.ModuleBuilder.ClassLayout.AddRecord(rec);
677 foreach (MethodBuilder mb in methods)
681 if (declarativeSecurity != null)
683 this.ModuleBuilder.AddDeclarativeSecurity(token, declarativeSecurity);
685 if (!IsModulePseudoType)
687 Type baseType = this.BaseType;
688 if (baseType != null)
690 extends = this.ModuleBuilder.GetTypeToken(baseType).Token;
693 if (interfaces != null)
695 foreach (Type interfaceType in interfaces)
697 InterfaceImplTable.Record rec = new InterfaceImplTable.Record();
699 rec.Interface = this.ModuleBuilder.GetTypeToken(interfaceType).Token;
700 this.ModuleBuilder.InterfaceImpl.AddRecord(rec);
703 return new BakedType(this);
706 internal void PopulatePropertyAndEventTables()
708 if (properties != null)
710 PropertyMapTable.Record rec = new PropertyMapTable.Record();
712 rec.PropertyList = this.ModuleBuilder.Property.RowCount + 1;
713 this.ModuleBuilder.PropertyMap.AddRecord(rec);
714 foreach (PropertyBuilder pb in properties)
721 EventMapTable.Record rec = new EventMapTable.Record();
723 rec.EventList = this.ModuleBuilder.Event.RowCount + 1;
724 this.ModuleBuilder.EventMap.AddRecord(rec);
725 foreach (EventBuilder eb in events)
732 public override Type BaseType
736 if (lazyBaseType == null && !IsInterface)
738 Type obj = Module.universe.System_Object;
748 public override string FullName
754 return this.DeclaringType.FullName + "+" + TypeNameParser.Escape(name);
758 return TypeNameParser.Escape(name);
762 return TypeNameParser.Escape(ns) + "." + TypeNameParser.Escape(name);
767 public override string __Name
772 public override string __Namespace
777 public override string Name
779 // FXBUG for a TypeBuilder the name is not escaped
783 public override string Namespace
787 // for some reason, TypeBuilder doesn't return null (and mcs depends on this)
788 // note also that we don't return the declaring type namespace for nested types
793 public override TypeAttributes Attributes
795 get { return attribs; }
798 public void __SetAttributes(TypeAttributes attributes)
800 this.attribs = attributes;
803 public override Type[] __GetDeclaredInterfaces()
805 return Util.ToArray(interfaces, Type.EmptyTypes);
808 public override MethodBase[] __GetDeclaredMethods()
810 MethodBase[] methods = new MethodBase[this.methods.Count];
811 for (int i = 0; i < methods.Length; i++)
813 MethodBuilder mb = this.methods[i];
814 if (mb.IsConstructor)
816 methods[i] = new ConstructorInfoImpl(mb);
826 public override StructLayoutAttribute StructLayoutAttribute
831 switch (attribs & TypeAttributes.LayoutMask)
833 case TypeAttributes.ExplicitLayout:
834 layout = LayoutKind.Explicit;
836 case TypeAttributes.SequentialLayout:
837 layout = LayoutKind.Sequential;
840 layout = LayoutKind.Auto;
843 StructLayoutAttribute attr = new StructLayoutAttribute(layout);
844 attr.Pack = (ushort)pack;
846 switch (attribs & TypeAttributes.StringFormatMask)
848 case TypeAttributes.AutoClass:
849 attr.CharSet = CharSet.Auto;
851 case TypeAttributes.UnicodeClass:
852 attr.CharSet = CharSet.Unicode;
854 case TypeAttributes.AnsiClass:
855 attr.CharSet = CharSet.Ansi;
858 attr.CharSet = CharSet.None;
865 public override Type DeclaringType
867 get { return owner as TypeBuilder; }
870 public override bool IsGenericType
872 get { return IsGenericTypeDefinition; }
875 public override bool IsGenericTypeDefinition
877 get { return (typeFlags & TypeFlags.IsGenericTypeDefinition) != 0; }
880 public override int MetadataToken
882 get { return token; }
885 public FieldBuilder DefineUninitializedData(string name, int size, FieldAttributes attributes)
887 return DefineInitializedData(name, new byte[size], attributes);
890 public FieldBuilder DefineInitializedData(string name, byte[] data, FieldAttributes attributes)
892 Type fieldType = this.ModuleBuilder.GetType("$ArrayType$" + data.Length);
893 if (fieldType == null)
895 TypeBuilder tb = this.ModuleBuilder.DefineType("$ArrayType$" + data.Length, TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.ExplicitLayout, this.Module.universe.System_ValueType, PackingSize.Size1, data.Length);
899 FieldBuilder fb = DefineField(name, fieldType, attributes | FieldAttributes.Static);
900 fb.__SetDataAndRVA(data);
904 public static MethodInfo GetMethod(Type type, MethodInfo method)
906 return new GenericMethodInstance(type, method, null);
909 public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor)
911 return new ConstructorInfoImpl(GetMethod(type, constructor.GetMethodInfo()));
914 public static FieldInfo GetField(Type type, FieldInfo field)
916 return new GenericFieldInstance(type, field);
919 public override Module Module
921 get { return owner.ModuleBuilder; }
924 public TypeToken TypeToken
926 get { return new TypeToken(token); }
929 internal void WriteTypeDefRecord(MetadataWriter mw, ref int fieldList, ref int methodList)
931 mw.Write((int)attribs);
932 mw.WriteStringIndex(typeName);
933 mw.WriteStringIndex(typeNameSpace);
934 mw.WriteTypeDefOrRef(extends);
935 mw.WriteField(fieldList);
936 mw.WriteMethodDef(methodList);
937 methodList += methods.Count;
938 fieldList += fields.Count;
941 internal void WriteMethodDefRecords(int baseRVA, MetadataWriter mw, ref int paramList)
943 foreach (MethodBuilder mb in methods)
945 mb.WriteMethodDefRecord(baseRVA, mw, ref paramList);
949 internal void ResolveMethodAndFieldTokens(ref int methodToken, ref int fieldToken, ref int parameterToken)
951 foreach (MethodBuilder method in methods)
953 method.FixupToken(methodToken++, ref parameterToken);
955 foreach (FieldBuilder field in fields)
957 field.FixupToken(fieldToken++);
961 internal void WriteParamRecords(MetadataWriter mw)
963 foreach (MethodBuilder mb in methods)
965 mb.WriteParamRecords(mw);
969 internal void WriteFieldRecords(MetadataWriter mw)
971 foreach (FieldBuilder fb in fields)
973 fb.WriteFieldRecords(mw);
977 internal ModuleBuilder ModuleBuilder
979 get { return owner.ModuleBuilder; }
982 ModuleBuilder ITypeOwner.ModuleBuilder
984 get { return owner.ModuleBuilder; }
987 internal override int GetModuleBuilderToken()
992 internal bool HasNestedTypes
994 get { return (typeFlags & TypeFlags.HasNestedTypes) != 0; }
997 // helper for ModuleBuilder.ResolveMethod()
998 internal MethodBase LookupMethod(int token)
1000 foreach (MethodBuilder method in methods)
1002 if (method.MetadataToken == token)
1010 public bool IsCreated()
1012 return (typeFlags & TypeFlags.Baked) != 0;
1015 internal override void CheckBaked()
1017 if ((typeFlags & TypeFlags.Baked) == 0)
1019 throw new NotSupportedException();
1023 public override Type[] __GetDeclaredTypes()
1025 if (this.HasNestedTypes)
1027 List<Type> types = new List<Type>();
1028 List<int> classes = this.ModuleBuilder.NestedClass.GetNestedClasses(token);
1029 foreach (int nestedClass in classes)
1031 types.Add(this.ModuleBuilder.ResolveType(nestedClass));
1033 return types.ToArray();
1037 return Type.EmptyTypes;
1041 public override FieldInfo[] __GetDeclaredFields()
1043 return Util.ToArray(fields, Empty<FieldInfo>.Array);
1046 public override EventInfo[] __GetDeclaredEvents()
1048 return Util.ToArray(events, Empty<EventInfo>.Array);
1051 public override PropertyInfo[] __GetDeclaredProperties()
1053 return Util.ToArray(properties, Empty<PropertyInfo>.Array);
1056 internal override bool IsModulePseudoType
1058 get { return token == 0x02000001; }
1062 sealed class BakedType : Type
1064 internal BakedType(TypeBuilder typeBuilder)
1069 public override string AssemblyQualifiedName
1071 get { return underlyingType.AssemblyQualifiedName; }
1074 public override Type BaseType
1076 get { return underlyingType.BaseType; }
1079 public override string __Name
1081 get { return underlyingType.__Name; }
1084 public override string __Namespace
1086 get { return underlyingType.__Namespace; }
1089 public override string Name
1091 // we need to escape here, because TypeBuilder.Name does not escape
1092 get { return TypeNameParser.Escape(underlyingType.__Name); }
1095 public override string FullName
1097 get { return GetFullName(); }
1100 public override TypeAttributes Attributes
1102 get { return underlyingType.Attributes; }
1105 public override Type[] __GetDeclaredInterfaces()
1107 return underlyingType.__GetDeclaredInterfaces();
1110 public override MethodBase[] __GetDeclaredMethods()
1112 return underlyingType.__GetDeclaredMethods();
1115 public override __MethodImplMap __GetMethodImplMap()
1117 return underlyingType.__GetMethodImplMap();
1120 public override FieldInfo[] __GetDeclaredFields()
1122 return underlyingType.__GetDeclaredFields();
1125 public override EventInfo[] __GetDeclaredEvents()
1127 return underlyingType.__GetDeclaredEvents();
1130 public override PropertyInfo[] __GetDeclaredProperties()
1132 return underlyingType.__GetDeclaredProperties();
1135 public override Type[] __GetDeclaredTypes()
1137 return underlyingType.__GetDeclaredTypes();
1140 public override Type DeclaringType
1142 get { return underlyingType.DeclaringType; }
1145 public override StructLayoutAttribute StructLayoutAttribute
1147 get { return underlyingType.StructLayoutAttribute; }
1150 public override Type[] GetGenericArguments()
1152 return underlyingType.GetGenericArguments();
1155 internal override Type GetGenericTypeArgument(int index)
1157 return underlyingType.GetGenericTypeArgument(index);
1160 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
1162 return underlyingType.__GetGenericArgumentsCustomModifiers();
1165 public override bool IsGenericType
1167 get { return underlyingType.IsGenericType; }
1170 public override bool IsGenericTypeDefinition
1172 get { return underlyingType.IsGenericTypeDefinition; }
1175 public override bool ContainsGenericParameters
1177 get { return underlyingType.ContainsGenericParameters; }
1180 public override int MetadataToken
1182 get { return underlyingType.MetadataToken; }
1185 public override Module Module
1187 get { return underlyingType.Module; }
1190 internal override int GetModuleBuilderToken()
1192 return underlyingType.GetModuleBuilderToken();