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);
251 public ConstructorBuilder DefineDefaultConstructor(MethodAttributes attributes)
253 ConstructorBuilder cb = DefineConstructor(attributes, CallingConventions.Standard, Type.EmptyTypes);
254 ILGenerator ilgen = cb.GetILGenerator();
255 ilgen.Emit(OpCodes.Ldarg_0);
256 ilgen.Emit(OpCodes.Call, BaseType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null));
257 ilgen.Emit(OpCodes.Ret);
261 public ConstructorBuilder DefineConstructor(MethodAttributes attribs, CallingConventions callConv, Type[] parameterTypes)
263 return DefineConstructor(attribs, callConv, parameterTypes, null, null);
266 public ConstructorBuilder DefineConstructor(MethodAttributes attribs, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
268 attribs |= MethodAttributes.RTSpecialName | MethodAttributes.SpecialName;
269 string name = (attribs & MethodAttributes.Static) == 0 ? ConstructorInfo.ConstructorName : ConstructorInfo.TypeConstructorName;
270 MethodBuilder mb = DefineMethod(name, attribs, callingConvention, null, null, null, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
271 return new ConstructorBuilder(mb);
274 public ConstructorBuilder DefineTypeInitializer()
276 MethodBuilder mb = DefineMethod(ConstructorInfo.TypeConstructorName, MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName, null, Type.EmptyTypes);
277 return new ConstructorBuilder(mb);
280 private MethodBuilder CreateMethodBuilder(string name, MethodAttributes attributes, CallingConventions callingConvention)
282 this.ModuleBuilder.MethodDef.AddVirtualRecord();
283 MethodBuilder mb = new MethodBuilder(this, name, attributes, callingConvention);
288 public MethodBuilder DefineMethod(string name, MethodAttributes attribs)
290 return DefineMethod(name, attribs, CallingConventions.Standard);
293 public MethodBuilder DefineMethod(string name, MethodAttributes attribs, CallingConventions callingConvention)
295 return CreateMethodBuilder(name, attribs, callingConvention);
298 public MethodBuilder DefineMethod(string name, MethodAttributes attribs, Type returnType, Type[] parameterTypes)
300 return DefineMethod(name, attribs, CallingConventions.Standard, returnType, null, null, parameterTypes, null, null);
303 public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
305 return DefineMethod(name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
308 public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
310 MethodBuilder mb = CreateMethodBuilder(name, attributes, callingConvention);
311 mb.SetSignature(returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
315 public MethodBuilder DefinePInvokeMethod(string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
317 return DefinePInvokeMethod(name, dllName, null, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
320 public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
322 return DefinePInvokeMethod(name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
325 public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention,
326 Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers,
327 Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers,
328 CallingConvention nativeCallConv, CharSet nativeCharSet)
330 MethodBuilder mb = DefineMethod(name, attributes | MethodAttributes.PinvokeImpl, callingConvention,
331 returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers,
332 parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
333 mb.SetDllImportPseudoCustomAttribute(dllName, entryName, nativeCallConv, nativeCharSet, null, null, null, null, null);
337 public void DefineMethodOverride(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)
339 MethodImplTable.Record rec = new MethodImplTable.Record();
341 rec.MethodBody = this.ModuleBuilder.GetMethodToken(methodInfoBody).Token;
342 rec.MethodDeclaration = this.ModuleBuilder.GetMethodToken(methodInfoDeclaration).Token;
343 this.ModuleBuilder.MethodImpl.AddRecord(rec);
346 public FieldBuilder DefineField(string name, Type fieldType, FieldAttributes attribs)
348 return DefineField(name, fieldType, null, null, attribs);
351 public FieldBuilder DefineField(string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes)
353 FieldBuilder fb = new FieldBuilder(this, fieldName, type, requiredCustomModifiers, optionalCustomModifiers, attributes);
358 public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes)
360 return DefineProperty(name, attributes, returnType, null, null, parameterTypes, null, null);
363 public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers,
364 Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
366 return DefinePropertyImpl(name, attributes, CallingConventions.Standard, true, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers,
367 parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
370 public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, CallingConventions callingConvention,
371 Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers,
372 Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
374 return DefinePropertyImpl(name, attributes, callingConvention, false, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers,
375 parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
378 private PropertyBuilder DefinePropertyImpl(string name, PropertyAttributes attributes, CallingConventions callingConvention, bool patchCallingConvention,
379 Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers,
380 Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
382 if (properties == null)
384 properties = new List<PropertyBuilder>();
386 PropertySignature sig = PropertySignature.Create(callingConvention, returnType, returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers,
387 parameterTypes, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers);
388 PropertyBuilder pb = new PropertyBuilder(this, name, attributes, sig, patchCallingConvention);
393 public EventBuilder DefineEvent(string name, EventAttributes attributes, Type eventtype)
397 events = new List<EventBuilder>();
399 EventBuilder eb = new EventBuilder(this, name, attributes, eventtype);
404 public TypeBuilder DefineNestedType(string name)
406 return DefineNestedType(name, TypeAttributes.Class | TypeAttributes.NestedPrivate);
409 public TypeBuilder DefineNestedType(string name, TypeAttributes attribs)
411 return DefineNestedType(name, attribs, null);
414 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, Type[] interfaces)
416 TypeBuilder tb = DefineNestedType(name, attr, parent);
417 foreach (Type iface in interfaces)
419 tb.AddInterfaceImplementation(iface);
424 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent)
426 return DefineNestedType(name, attr, parent, 0);
429 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, int typeSize)
431 return DefineNestedType(name, attr, parent, PackingSize.Unspecified, typeSize);
434 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, PackingSize packSize)
436 return DefineNestedType(name, attr, parent, packSize, 0);
439 private TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, PackingSize packSize, int typeSize)
442 int lastdot = name.LastIndexOf('.');
445 ns = name.Substring(0, lastdot);
446 name = name.Substring(lastdot + 1);
448 TypeBuilder typeBuilder = __DefineNestedType(ns, name);
449 typeBuilder.__SetAttributes(attr);
450 typeBuilder.SetParent(parent);
451 if (packSize != PackingSize.Unspecified || typeSize != 0)
453 typeBuilder.__SetLayout((int)packSize, typeSize);
458 public TypeBuilder __DefineNestedType(string ns, string name)
460 this.typeFlags |= TypeFlags.HasNestedTypes;
461 TypeBuilder typeBuilder = this.ModuleBuilder.DefineType(this, ns, name);
462 NestedClassTable.Record rec = new NestedClassTable.Record();
463 rec.NestedClass = typeBuilder.MetadataToken;
464 rec.EnclosingClass = this.MetadataToken;
465 this.ModuleBuilder.NestedClass.AddRecord(rec);
469 public void SetParent(Type parent)
471 lazyBaseType = parent;
474 public void AddInterfaceImplementation(Type interfaceType)
476 if (interfaces == null)
478 interfaces = new List<Type>();
480 interfaces.Add(interfaceType);
488 public PackingSize PackingSize
490 get { return (PackingSize)pack; }
493 public override bool __GetLayout(out int packingSize, out int typeSize)
495 packingSize = this.pack;
496 typeSize = this.size;
500 public void __SetLayout(int packingSize, int typesize)
502 this.pack = (short)packingSize;
503 this.size = typesize;
504 this.hasLayout = true;
507 private void SetStructLayoutPseudoCustomAttribute(CustomAttributeBuilder customBuilder)
509 object val = customBuilder.GetConstructorArgument(0);
513 layout = (LayoutKind)(short)val;
517 layout = (LayoutKind)val;
519 StructLayoutAttribute attr = new StructLayoutAttribute(layout);
520 attr.Pack = (int?)customBuilder.GetFieldValue("Pack") ?? 0;
521 attr.Size = (int?)customBuilder.GetFieldValue("Size") ?? 0;
522 attr.CharSet = customBuilder.GetFieldValue<CharSet>("CharSet") ?? CharSet.None;
523 attribs &= ~TypeAttributes.LayoutMask;
526 case LayoutKind.Auto:
527 attribs |= TypeAttributes.AutoLayout;
529 case LayoutKind.Explicit:
530 attribs |= TypeAttributes.ExplicitLayout;
532 case LayoutKind.Sequential:
533 attribs |= TypeAttributes.SequentialLayout;
536 attribs &= ~TypeAttributes.StringFormatMask;
537 switch (attr.CharSet)
541 attribs |= TypeAttributes.AnsiClass;
544 attribs |= TypeAttributes.AutoClass;
546 case CharSet.Unicode:
547 attribs |= TypeAttributes.UnicodeClass;
550 pack = (short)attr.Pack;
552 hasLayout = pack != 0 || size != 0;
555 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
557 SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
560 public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
562 Universe u = this.ModuleBuilder.universe;
563 Type type = customBuilder.Constructor.DeclaringType;
564 if (type == u.System_Runtime_InteropServices_StructLayoutAttribute)
566 SetStructLayoutPseudoCustomAttribute(customBuilder.DecodeBlob(this.Assembly));
568 else if (type == u.System_SerializableAttribute)
570 attribs |= TypeAttributes.Serializable;
572 else if (type == u.System_Runtime_InteropServices_ComImportAttribute)
574 attribs |= TypeAttributes.Import;
576 else if (type == u.System_Runtime_CompilerServices_SpecialNameAttribute)
578 attribs |= TypeAttributes.SpecialName;
582 if (type == u.System_Security_SuppressUnmanagedCodeSecurityAttribute)
584 attribs |= TypeAttributes.HasSecurity;
586 this.ModuleBuilder.SetCustomAttribute(token, customBuilder);
590 public void __AddDeclarativeSecurity(CustomAttributeBuilder customBuilder)
592 attribs |= TypeAttributes.HasSecurity;
593 if (declarativeSecurity == null)
595 declarativeSecurity = new List<CustomAttributeBuilder>();
597 declarativeSecurity.Add(customBuilder);
600 public void AddDeclarativeSecurity(System.Security.Permissions.SecurityAction securityAction, System.Security.PermissionSet permissionSet)
602 this.ModuleBuilder.AddDeclarativeSecurity(token, securityAction, permissionSet);
603 this.attribs |= TypeAttributes.HasSecurity;
606 public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names)
608 typeFlags |= TypeFlags.IsGenericTypeDefinition;
609 gtpb = new GenericTypeParameterBuilder[names.Length];
610 for (int i = 0; i < names.Length; i++)
612 gtpb[i] = new GenericTypeParameterBuilder(names[i], this, null, i);
614 return (GenericTypeParameterBuilder[])gtpb.Clone();
617 public override Type[] GetGenericArguments()
619 return Util.Copy(gtpb);
622 public override Type[][] __GetGenericArgumentsOptionalCustomModifiers()
624 return gtpb == null ? Empty<Type[]>.Array : Util.Copy(new Type[gtpb.Length][]);
627 public override Type[][] __GetGenericArgumentsRequiredCustomModifiers()
629 return gtpb == null ? Empty<Type[]>.Array : Util.Copy(new Type[gtpb.Length][]);
632 internal override Type GetGenericTypeArgument(int index)
637 public override bool ContainsGenericParameters
639 get { return gtpb != null; }
642 public override Type GetGenericTypeDefinition()
647 public Type CreateType()
649 if ((typeFlags & TypeFlags.Baked) != 0)
651 // .NET allows multiple invocations (subsequent invocations return the same baked type)
652 throw new NotImplementedException();
654 typeFlags |= TypeFlags.Baked;
657 ClassLayoutTable.Record rec = new ClassLayoutTable.Record();
658 rec.PackingSize = pack;
659 rec.ClassSize = size;
661 this.ModuleBuilder.ClassLayout.AddRecord(rec);
663 foreach (MethodBuilder mb in methods)
667 if (declarativeSecurity != null)
669 this.ModuleBuilder.AddDeclarativeSecurity(token, declarativeSecurity);
671 if (!IsModulePseudoType)
673 Type baseType = this.BaseType;
674 if (baseType != null)
676 extends = this.ModuleBuilder.GetTypeToken(baseType).Token;
679 if (interfaces != null)
681 foreach (Type interfaceType in interfaces)
683 InterfaceImplTable.Record rec = new InterfaceImplTable.Record();
685 rec.Interface = this.ModuleBuilder.GetTypeToken(interfaceType).Token;
686 this.ModuleBuilder.InterfaceImpl.AddRecord(rec);
689 return new BakedType(this);
692 internal void PopulatePropertyAndEventTables()
694 if (properties != null)
696 PropertyMapTable.Record rec = new PropertyMapTable.Record();
698 rec.PropertyList = this.ModuleBuilder.Property.RowCount + 1;
699 this.ModuleBuilder.PropertyMap.AddRecord(rec);
700 foreach (PropertyBuilder pb in properties)
707 EventMapTable.Record rec = new EventMapTable.Record();
709 rec.EventList = this.ModuleBuilder.Event.RowCount + 1;
710 this.ModuleBuilder.EventMap.AddRecord(rec);
711 foreach (EventBuilder eb in events)
718 public override Type BaseType
722 if (lazyBaseType == null && !IsInterface)
724 Type obj = Module.universe.System_Object;
734 public override string FullName
740 return this.DeclaringType.FullName + "+" + TypeNameParser.Escape(name);
744 return TypeNameParser.Escape(name);
748 return TypeNameParser.Escape(ns) + "." + TypeNameParser.Escape(name);
753 public override string __Name
758 public override string __Namespace
763 public override string Name
765 // FXBUG for a TypeBuilder the name is not escaped
769 public override string Namespace
773 // for some reason, TypeBuilder doesn't return null (and mcs depends on this)
774 // note also that we don't return the declaring type namespace for nested types
779 public override TypeAttributes Attributes
781 get { return attribs; }
784 public void __SetAttributes(TypeAttributes attributes)
786 this.attribs = attributes;
789 public override Type[] __GetDeclaredInterfaces()
791 return Util.ToArray(interfaces, Type.EmptyTypes);
794 public override MethodBase[] __GetDeclaredMethods()
796 MethodBase[] methods = new MethodBase[this.methods.Count];
797 for (int i = 0; i < methods.Length; i++)
799 MethodBuilder mb = this.methods[i];
800 if (mb.IsConstructor)
802 methods[i] = new ConstructorInfoImpl(mb);
812 public override StructLayoutAttribute StructLayoutAttribute
817 switch (attribs & TypeAttributes.LayoutMask)
819 case TypeAttributes.ExplicitLayout:
820 layout = LayoutKind.Explicit;
822 case TypeAttributes.SequentialLayout:
823 layout = LayoutKind.Sequential;
826 layout = LayoutKind.Auto;
829 StructLayoutAttribute attr = new StructLayoutAttribute(layout);
830 attr.Pack = (ushort)pack;
832 switch (attribs & TypeAttributes.StringFormatMask)
834 case TypeAttributes.AutoClass:
835 attr.CharSet = CharSet.Auto;
837 case TypeAttributes.UnicodeClass:
838 attr.CharSet = CharSet.Unicode;
840 case TypeAttributes.AnsiClass:
841 attr.CharSet = CharSet.Ansi;
844 attr.CharSet = CharSet.None;
851 public override Type DeclaringType
853 get { return owner as TypeBuilder; }
856 public override bool IsGenericType
858 get { return IsGenericTypeDefinition; }
861 public override bool IsGenericTypeDefinition
863 get { return (typeFlags & TypeFlags.IsGenericTypeDefinition) != 0; }
866 public override int MetadataToken
868 get { return token; }
871 public FieldBuilder DefineUninitializedData(string name, int size, FieldAttributes attributes)
873 return DefineInitializedData(name, new byte[size], attributes);
876 public FieldBuilder DefineInitializedData(string name, byte[] data, FieldAttributes attributes)
878 Type fieldType = this.ModuleBuilder.GetType("$ArrayType$" + data.Length);
879 if (fieldType == null)
881 TypeBuilder tb = this.ModuleBuilder.DefineType("$ArrayType$" + data.Length, TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.ExplicitLayout, this.Module.universe.System_ValueType, PackingSize.Size1, data.Length);
885 FieldBuilder fb = DefineField(name, fieldType, attributes | FieldAttributes.Static);
886 fb.__SetDataAndRVA(data);
890 public static MethodInfo GetMethod(Type type, MethodInfo method)
892 return new GenericMethodInstance(type, method, null);
895 public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor)
897 return new ConstructorInfoImpl(GetMethod(type, constructor.GetMethodInfo()));
900 public static FieldInfo GetField(Type type, FieldInfo field)
902 return new GenericFieldInstance(type, field);
905 public override Module Module
907 get { return owner.ModuleBuilder; }
910 public TypeToken TypeToken
912 get { return new TypeToken(token); }
915 internal void WriteTypeDefRecord(MetadataWriter mw, ref int fieldList, ref int methodList)
917 mw.Write((int)attribs);
918 mw.WriteStringIndex(typeName);
919 mw.WriteStringIndex(typeNameSpace);
920 mw.WriteTypeDefOrRef(extends);
921 mw.WriteField(fieldList);
922 mw.WriteMethodDef(methodList);
923 methodList += methods.Count;
924 fieldList += fields.Count;
927 internal void WriteMethodDefRecords(int baseRVA, MetadataWriter mw, ref int paramList)
929 foreach (MethodBuilder mb in methods)
931 mb.WriteMethodDefRecord(baseRVA, mw, ref paramList);
935 internal void ResolveMethodAndFieldTokens(ref int methodToken, ref int fieldToken, ref int parameterToken)
937 foreach (MethodBuilder method in methods)
939 method.FixupToken(methodToken++, ref parameterToken);
941 foreach (FieldBuilder field in fields)
943 field.FixupToken(fieldToken++);
947 internal void WriteParamRecords(MetadataWriter mw)
949 foreach (MethodBuilder mb in methods)
951 mb.WriteParamRecords(mw);
955 internal void WriteFieldRecords(MetadataWriter mw)
957 foreach (FieldBuilder fb in fields)
959 fb.WriteFieldRecords(mw);
963 internal ModuleBuilder ModuleBuilder
965 get { return owner.ModuleBuilder; }
968 ModuleBuilder ITypeOwner.ModuleBuilder
970 get { return owner.ModuleBuilder; }
973 internal override int GetModuleBuilderToken()
978 internal bool HasNestedTypes
980 get { return (typeFlags & TypeFlags.HasNestedTypes) != 0; }
983 // helper for ModuleBuilder.ResolveMethod()
984 internal MethodBase LookupMethod(int token)
986 foreach (MethodBuilder method in methods)
988 if (method.MetadataToken == token)
996 public bool IsCreated()
998 return (typeFlags & TypeFlags.Baked) != 0;
1001 internal override void CheckBaked()
1003 if ((typeFlags & TypeFlags.Baked) == 0)
1005 throw new NotSupportedException();
1009 public override Type[] __GetDeclaredTypes()
1011 if (this.HasNestedTypes)
1013 List<Type> types = new List<Type>();
1014 List<int> classes = this.ModuleBuilder.NestedClass.GetNestedClasses(token);
1015 foreach (int nestedClass in classes)
1017 types.Add(this.ModuleBuilder.ResolveType(nestedClass));
1019 return types.ToArray();
1023 return Type.EmptyTypes;
1027 public override FieldInfo[] __GetDeclaredFields()
1029 return Util.ToArray(fields, Empty<FieldInfo>.Array);
1032 public override EventInfo[] __GetDeclaredEvents()
1034 return Util.ToArray(events, Empty<EventInfo>.Array);
1037 public override PropertyInfo[] __GetDeclaredProperties()
1039 return Util.ToArray(properties, Empty<PropertyInfo>.Array);
1042 internal override bool IsModulePseudoType
1044 get { return token == 0x02000001; }
1048 sealed class BakedType : Type
1050 internal BakedType(TypeBuilder typeBuilder)
1055 public override string AssemblyQualifiedName
1057 get { return underlyingType.AssemblyQualifiedName; }
1060 public override Type BaseType
1062 get { return underlyingType.BaseType; }
1065 public override string __Name
1067 get { return underlyingType.__Name; }
1070 public override string __Namespace
1072 get { return underlyingType.__Namespace; }
1075 public override string Name
1077 // we need to escape here, because TypeBuilder.Name does not escape
1078 get { return TypeNameParser.Escape(underlyingType.__Name); }
1081 public override string FullName
1083 get { return GetFullName(); }
1086 public override TypeAttributes Attributes
1088 get { return underlyingType.Attributes; }
1091 public override Type[] __GetDeclaredInterfaces()
1093 return underlyingType.__GetDeclaredInterfaces();
1096 public override MethodBase[] __GetDeclaredMethods()
1098 return underlyingType.__GetDeclaredMethods();
1101 public override __MethodImplMap __GetMethodImplMap()
1103 return underlyingType.__GetMethodImplMap();
1106 public override FieldInfo[] __GetDeclaredFields()
1108 return underlyingType.__GetDeclaredFields();
1111 public override EventInfo[] __GetDeclaredEvents()
1113 return underlyingType.__GetDeclaredEvents();
1116 public override PropertyInfo[] __GetDeclaredProperties()
1118 return underlyingType.__GetDeclaredProperties();
1121 public override Type[] __GetDeclaredTypes()
1123 return underlyingType.__GetDeclaredTypes();
1126 public override Type DeclaringType
1128 get { return underlyingType.DeclaringType; }
1131 public override StructLayoutAttribute StructLayoutAttribute
1133 get { return underlyingType.StructLayoutAttribute; }
1136 public override Type[] GetGenericArguments()
1138 return underlyingType.GetGenericArguments();
1141 internal override Type GetGenericTypeArgument(int index)
1143 return underlyingType.GetGenericTypeArgument(index);
1146 public override Type[][] __GetGenericArgumentsOptionalCustomModifiers()
1148 return underlyingType.__GetGenericArgumentsOptionalCustomModifiers();
1151 public override Type[][] __GetGenericArgumentsRequiredCustomModifiers()
1153 return underlyingType.__GetGenericArgumentsRequiredCustomModifiers();
1156 public override bool IsGenericType
1158 get { return underlyingType.IsGenericType; }
1161 public override bool IsGenericTypeDefinition
1163 get { return underlyingType.IsGenericTypeDefinition; }
1166 public override bool ContainsGenericParameters
1168 get { return underlyingType.ContainsGenericParameters; }
1171 public override int MetadataToken
1173 get { return underlyingType.MetadataToken; }
1176 public override Module Module
1178 get { return underlyingType.Module; }
1181 internal override int GetModuleBuilderToken()
1183 return underlyingType.GetModuleBuilderToken();