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 Type UnderlyingSystemType
94 public override string Name
99 public override string FullName
104 public override string ToString()
109 private ModuleBuilder ModuleBuilder
111 get { return type != null ? type.ModuleBuilder : method.ModuleBuilder; }
114 public override Module Module
116 get { return ModuleBuilder; }
119 public override bool IsGenericParameter
124 public override int GenericParameterPosition
126 get { return position; }
129 public override Type DeclaringType
134 public override MethodBase DeclaringMethod
136 get { return method; }
139 public override Type[] GetGenericParameterConstraints()
141 throw new NotImplementedException();
144 public override GenericParameterAttributes GenericParameterAttributes
160 private void AddConstraint(Type type)
162 GenericParamConstraintTable.Record rec = new GenericParamConstraintTable.Record();
163 rec.Owner = paramPseudoIndex;
164 rec.Constraint = this.ModuleBuilder.GetTypeTokenForMemberRef(type);
165 this.ModuleBuilder.GenericParamConstraint.AddRecord(rec);
168 public void SetBaseTypeConstraint(Type baseTypeConstraint)
170 this.baseType = baseTypeConstraint;
171 AddConstraint(baseTypeConstraint);
174 public void SetInterfaceConstraints(params Type[] interfaceConstraints)
176 foreach (Type type in interfaceConstraints)
182 public void SetGenericParameterAttributes(GenericParameterAttributes genericParameterAttributes)
184 this.attr = genericParameterAttributes;
185 // for now we'll back patch the table
186 this.ModuleBuilder.GenericParam.PatchAttribute(paramPseudoIndex, genericParameterAttributes);
189 public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
191 this.ModuleBuilder.SetCustomAttribute((GenericParamTable.Index << 24) | paramPseudoIndex, customBuilder);
194 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
196 SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
199 internal override int GetModuleBuilderToken()
203 ByteBuffer spec = new ByteBuffer(5);
204 Signature.WriteTypeSpec(this.ModuleBuilder, spec, this);
205 typeToken = 0x1B000000 | this.ModuleBuilder.TypeSpec.AddRecord(this.ModuleBuilder.Blobs.Add(spec));
210 internal override Type BindTypeParameters(IGenericBinder binder)
214 return binder.BindTypeParameter(this);
218 return binder.BindMethodParameter(this);
223 public sealed class TypeBuilder : Type, ITypeOwner
225 public const int UnspecifiedTypeSize = 0;
226 private readonly ITypeOwner owner;
227 private readonly int token;
229 private Type baseType;
230 private readonly int typeName;
231 private readonly int typeNameSpace;
232 private readonly string ns;
233 private readonly string name;
234 private readonly List<MethodBuilder> methods = new List<MethodBuilder>();
235 private readonly List<FieldBuilder> fields = new List<FieldBuilder>();
236 private List<PropertyBuilder> properties;
237 private List<EventBuilder> events;
238 private TypeAttributes attribs;
239 private TypeFlags typeFlags;
240 private GenericTypeParameterBuilder[] gtpb;
241 private List<CustomAttributeBuilder> declarativeSecurity;
242 private List<Type> interfaces;
245 private enum TypeFlags
247 IsGenericTypeDefinition = 1,
252 internal TypeBuilder(ITypeOwner owner, string ns, string name, TypeAttributes attribs)
255 this.token = this.ModuleBuilder.TypeDef.AllocToken();
258 this.attribs = attribs;
259 this.typeNameSpace = ns == null ? 0 : this.ModuleBuilder.Strings.Add(ns);
260 this.typeName = this.ModuleBuilder.Strings.Add(name);
263 public ConstructorBuilder DefineDefaultConstructor(MethodAttributes attributes)
265 ConstructorBuilder cb = DefineConstructor(attributes, CallingConventions.Standard, Type.EmptyTypes);
266 ILGenerator ilgen = cb.GetILGenerator();
267 ilgen.Emit(OpCodes.Ldarg_0);
268 ilgen.Emit(OpCodes.Call, baseType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null));
269 ilgen.Emit(OpCodes.Ret);
273 public ConstructorBuilder DefineConstructor(MethodAttributes attribs, CallingConventions callConv, Type[] parameterTypes)
275 return DefineConstructor(attribs, callConv, parameterTypes, null, null);
278 public ConstructorBuilder DefineConstructor(MethodAttributes attribs, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
280 attribs |= MethodAttributes.RTSpecialName | MethodAttributes.SpecialName;
281 string name = (attribs & MethodAttributes.Static) == 0 ? ConstructorInfo.ConstructorName : ConstructorInfo.TypeConstructorName;
282 MethodBuilder mb = DefineMethod(name, attribs, callingConvention, null, null, null, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
283 return new ConstructorBuilder(mb);
286 public ConstructorBuilder DefineTypeInitializer()
288 MethodBuilder mb = DefineMethod(ConstructorInfo.TypeConstructorName, MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName, null, Type.EmptyTypes);
289 return new ConstructorBuilder(mb);
292 private MethodBuilder CreateMethodBuilder(string name, MethodAttributes attributes, CallingConventions callingConvention)
294 this.ModuleBuilder.MethodDef.AddVirtualRecord();
295 MethodBuilder mb = new MethodBuilder(this, name, attributes, callingConvention);
300 public MethodBuilder DefineMethod(string name, MethodAttributes attribs)
302 return DefineMethod(name, attribs, CallingConventions.Standard);
305 public MethodBuilder DefineMethod(string name, MethodAttributes attribs, CallingConventions callingConvention)
307 return CreateMethodBuilder(name, attribs, callingConvention);
310 public MethodBuilder DefineMethod(string name, MethodAttributes attribs, Type returnType, Type[] parameterTypes)
312 return DefineMethod(name, attribs, CallingConventions.Standard, returnType, null, null, parameterTypes, null, null);
315 public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
317 return DefineMethod(name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
320 public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
322 MethodBuilder mb = CreateMethodBuilder(name, attributes, callingConvention);
323 mb.SetSignature(returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
327 public MethodBuilder DefinePInvokeMethod(string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
329 return DefinePInvokeMethod(name, dllName, null, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
332 public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
334 return DefinePInvokeMethod(name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
337 public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention,
338 Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers,
339 Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers,
340 CallingConvention nativeCallConv, CharSet nativeCharSet)
342 MethodBuilder mb = DefineMethod(name, attributes | MethodAttributes.PinvokeImpl, callingConvention,
343 returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers,
344 parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
345 mb.SetDllImportPseudoCustomAttribute(dllName, entryName, nativeCallConv, nativeCharSet, null, null, null, null, null);
349 public void DefineMethodOverride(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)
351 MethodImplTable.Record rec = new MethodImplTable.Record();
353 rec.MethodBody = this.ModuleBuilder.GetMethodToken(methodInfoBody).Token;
354 rec.MethodDeclaration = this.ModuleBuilder.GetMethodToken(methodInfoDeclaration).Token;
355 this.ModuleBuilder.MethodImpl.AddRecord(rec);
358 public FieldBuilder DefineField(string name, Type fieldType, FieldAttributes attribs)
360 return DefineField(name, fieldType, null, null, attribs);
363 public FieldBuilder DefineField(string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes)
365 FieldBuilder fb = new FieldBuilder(this, fieldName, type, requiredCustomModifiers, optionalCustomModifiers, attributes);
370 public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes)
372 return DefineProperty(name, attributes, returnType, null, null, parameterTypes, null, null);
375 public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers,
376 Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
378 return DefinePropertyImpl(name, attributes, CallingConventions.Standard, true, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers,
379 parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
382 public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, CallingConventions callingConvention,
383 Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers,
384 Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
386 return DefinePropertyImpl(name, attributes, callingConvention, false, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers,
387 parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
390 private PropertyBuilder DefinePropertyImpl(string name, PropertyAttributes attributes, CallingConventions callingConvention, bool patchCallingConvention,
391 Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers,
392 Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
394 if (properties == null)
396 properties = new List<PropertyBuilder>();
398 PropertySignature sig = PropertySignature.Create(callingConvention, returnType, returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers,
399 parameterTypes, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers);
400 PropertyBuilder pb = new PropertyBuilder(this, name, attributes, sig, patchCallingConvention);
405 public EventBuilder DefineEvent(string name, EventAttributes attributes, Type eventtype)
409 events = new List<EventBuilder>();
411 EventBuilder eb = new EventBuilder(this, name, attributes, eventtype);
416 public TypeBuilder DefineNestedType(string name)
418 return DefineNestedType(name, TypeAttributes.Class | TypeAttributes.NestedPrivate);
421 public TypeBuilder DefineNestedType(string name, TypeAttributes attribs)
423 return DefineNestedType(name, attribs, null);
426 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, Type[] interfaces)
428 TypeBuilder tb = DefineNestedType(name, attr, parent);
429 foreach (Type iface in interfaces)
431 tb.AddInterfaceImplementation(iface);
436 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent)
438 return DefineNestedType(name, attr, parent, 0);
441 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, int typeSize)
443 return DefineNestedType(name, attr, parent, PackingSize.Unspecified, typeSize);
446 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, PackingSize packSize)
448 return DefineNestedType(name, attr, parent, packSize, 0);
451 private TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, PackingSize packSize, int typeSize)
454 int lastdot = name.LastIndexOf('.');
457 ns = name.Substring(0, lastdot);
458 name = name.Substring(lastdot + 1);
460 TypeBuilder typeBuilder = __DefineNestedType(ns, name, attr);
461 if (parent == null && (attr & TypeAttributes.Interface) == 0)
463 parent = this.ModuleBuilder.universe.System_Object;
465 typeBuilder.SetParent(parent);
466 this.ModuleBuilder.SetPackingSizeAndTypeSize(typeBuilder, PackingSize.Unspecified, typeSize);
470 public TypeBuilder __DefineNestedType(string ns, string name, TypeAttributes attr)
472 this.typeFlags |= TypeFlags.HasNestedTypes;
473 TypeBuilder typeBuilder = this.ModuleBuilder.DefineType(this, ns, name, attr);
474 NestedClassTable.Record rec = new NestedClassTable.Record();
475 rec.NestedClass = typeBuilder.MetadataToken;
476 rec.EnclosingClass = this.MetadataToken;
477 this.ModuleBuilder.NestedClass.AddRecord(rec);
481 public void SetParent(Type parent)
486 public void AddInterfaceImplementation(Type interfaceType)
488 if (interfaces == null)
490 interfaces = new List<Type>();
492 interfaces.Add(interfaceType);
499 for (int i = 0; i < this.ModuleBuilder.ClassLayout.records.Length; i++)
501 if (this.ModuleBuilder.ClassLayout.records[i].Parent == token)
503 return this.ModuleBuilder.ClassLayout.records[i].ClassSize;
510 public PackingSize PackingSize
514 for (int i = 0; i < this.ModuleBuilder.ClassLayout.records.Length; i++)
516 if (this.ModuleBuilder.ClassLayout.records[i].Parent == token)
518 return (PackingSize)this.ModuleBuilder.ClassLayout.records[i].PackingSize;
521 return PackingSize.Unspecified;
525 private void SetStructLayoutPseudoCustomAttribute(CustomAttributeBuilder customBuilder)
527 object val = customBuilder.GetConstructorArgument(0);
531 layout = (LayoutKind)(short)val;
535 layout = (LayoutKind)val;
537 int? pack = (int?)customBuilder.GetFieldValue("Pack");
538 int? size = (int?)customBuilder.GetFieldValue("Size");
539 if (pack.HasValue || size.HasValue)
541 ClassLayoutTable.Record rec = new ClassLayoutTable.Record();
542 rec.PackingSize = (short)(pack ?? 0);
543 rec.ClassSize = size ?? 0;
545 this.ModuleBuilder.ClassLayout.AddOrReplaceRecord(rec);
547 attribs &= ~TypeAttributes.LayoutMask;
550 case LayoutKind.Auto:
551 attribs |= TypeAttributes.AutoLayout;
553 case LayoutKind.Explicit:
554 attribs |= TypeAttributes.ExplicitLayout;
556 case LayoutKind.Sequential:
557 attribs |= TypeAttributes.SequentialLayout;
560 CharSet? charSet = customBuilder.GetFieldValue<CharSet>("CharSet");
561 attribs &= ~TypeAttributes.StringFormatMask;
562 switch (charSet ?? CharSet.None)
566 attribs |= TypeAttributes.AnsiClass;
569 attribs |= TypeAttributes.AutoClass;
571 case CharSet.Unicode:
572 attribs |= TypeAttributes.UnicodeClass;
577 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
579 SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
582 public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
584 Universe u = this.ModuleBuilder.universe;
585 Type type = customBuilder.Constructor.DeclaringType;
586 if (type == u.System_Runtime_InteropServices_StructLayoutAttribute)
588 SetStructLayoutPseudoCustomAttribute(customBuilder.DecodeBlob(this.Assembly));
590 else if (type == u.System_SerializableAttribute)
592 attribs |= TypeAttributes.Serializable;
594 else if (type == u.System_Runtime_InteropServices_ComImportAttribute)
596 attribs |= TypeAttributes.Import;
598 else if (type == u.System_Runtime_CompilerServices_SpecialNameAttribute)
600 attribs |= TypeAttributes.SpecialName;
604 if (type == u.System_Security_SuppressUnmanagedCodeSecurityAttribute)
606 attribs |= TypeAttributes.HasSecurity;
608 this.ModuleBuilder.SetCustomAttribute(token, customBuilder);
612 public void __AddDeclarativeSecurity(CustomAttributeBuilder customBuilder)
614 attribs |= TypeAttributes.HasSecurity;
615 if (declarativeSecurity == null)
617 declarativeSecurity = new List<CustomAttributeBuilder>();
619 declarativeSecurity.Add(customBuilder);
622 public void AddDeclarativeSecurity(System.Security.Permissions.SecurityAction securityAction, System.Security.PermissionSet permissionSet)
624 this.ModuleBuilder.AddDeclarativeSecurity(token, securityAction, permissionSet);
625 this.attribs |= TypeAttributes.HasSecurity;
628 public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names)
630 typeFlags |= TypeFlags.IsGenericTypeDefinition;
631 gtpb = new GenericTypeParameterBuilder[names.Length];
632 for (int i = 0; i < names.Length; i++)
634 gtpb[i] = new GenericTypeParameterBuilder(names[i], this, null, i);
636 return (GenericTypeParameterBuilder[])gtpb.Clone();
639 public override Type[] GetGenericArguments()
641 return Util.Copy(gtpb);
644 public override Type[][] __GetGenericArgumentsOptionalCustomModifiers()
646 return gtpb == null ? Empty<Type[]>.Array : Util.Copy(new Type[gtpb.Length][]);
649 public override Type[][] __GetGenericArgumentsRequiredCustomModifiers()
651 return gtpb == null ? Empty<Type[]>.Array : Util.Copy(new Type[gtpb.Length][]);
654 internal override Type GetGenericTypeArgument(int index)
659 public override bool ContainsGenericParameters
661 get { return gtpb != null; }
664 public override Type GetGenericTypeDefinition()
669 public Type CreateType()
671 if ((typeFlags & TypeFlags.Baked) != 0)
673 // .NET allows multiple invocations (subsequent invocations return the same baked type)
674 throw new NotImplementedException();
676 typeFlags |= TypeFlags.Baked;
677 foreach (MethodBuilder mb in methods)
681 if (declarativeSecurity != null)
683 this.ModuleBuilder.AddDeclarativeSecurity(token, declarativeSecurity);
685 if (baseType != null)
687 extends = this.ModuleBuilder.GetTypeToken(baseType).Token;
689 if (interfaces != null)
691 foreach (Type interfaceType in interfaces)
693 InterfaceImplTable.Record rec = new InterfaceImplTable.Record();
695 rec.Interface = this.ModuleBuilder.GetTypeToken(interfaceType).Token;
696 this.ModuleBuilder.InterfaceImpl.AddRecord(rec);
699 return new BakedType(this);
702 internal void PopulatePropertyAndEventTables()
704 if (properties != null)
706 PropertyMapTable.Record rec = new PropertyMapTable.Record();
708 rec.PropertyList = this.ModuleBuilder.Property.RowCount + 1;
709 this.ModuleBuilder.PropertyMap.AddRecord(rec);
710 foreach (PropertyBuilder pb in properties)
717 EventMapTable.Record rec = new EventMapTable.Record();
719 rec.EventList = this.ModuleBuilder.Event.RowCount + 1;
720 this.ModuleBuilder.EventMap.AddRecord(rec);
721 foreach (EventBuilder eb in events)
728 public override Type BaseType
730 get { return baseType; }
733 public override string FullName
739 return this.DeclaringType.FullName + "+" + TypeNameParser.Escape(name);
743 return TypeNameParser.Escape(name);
747 return TypeNameParser.Escape(ns) + "." + TypeNameParser.Escape(name);
752 public override string __Name
757 public override string __Namespace
762 public override string Name
764 // FXBUG for a TypeBuilder the name is not escaped
768 public override string Namespace
772 // for some reason, TypeBuilder doesn't return null (and mcs depends on this)
773 // note also that we don't return the declaring type namespace for nested types
778 public override TypeAttributes Attributes
780 get { return attribs; }
783 public void __SetAttributes(TypeAttributes attributes)
785 this.attribs = attributes;
788 public override Type[] __GetDeclaredInterfaces()
790 return Util.ToArray(interfaces, Type.EmptyTypes);
793 public override MethodBase[] __GetDeclaredMethods()
795 MethodBase[] methods = new MethodBase[this.methods.Count];
796 for (int i = 0; i < methods.Length; i++)
798 MethodBuilder mb = this.methods[i];
799 if (mb.IsConstructor)
801 methods[i] = new ConstructorInfoImpl(mb);
811 public override StructLayoutAttribute StructLayoutAttribute
815 StructLayoutAttribute attr;
816 if ((attribs & TypeAttributes.ExplicitLayout) != 0)
818 attr = new StructLayoutAttribute(LayoutKind.Explicit);
821 this.ModuleBuilder.ClassLayout.GetLayout(token, ref attr.Pack, ref attr.Size);
825 attr = new StructLayoutAttribute((attribs & TypeAttributes.SequentialLayout) != 0 ? LayoutKind.Sequential : LayoutKind.Auto);
829 switch (attribs & TypeAttributes.StringFormatMask)
831 case TypeAttributes.AutoClass:
832 attr.CharSet = CharSet.Auto;
834 case TypeAttributes.UnicodeClass:
835 attr.CharSet = CharSet.Unicode;
837 case TypeAttributes.AnsiClass:
838 attr.CharSet = CharSet.Ansi;
845 public override Type DeclaringType
847 get { return owner as TypeBuilder; }
850 public override bool IsGenericType
852 get { return IsGenericTypeDefinition; }
855 public override bool IsGenericTypeDefinition
857 get { return (typeFlags & TypeFlags.IsGenericTypeDefinition) != 0; }
860 public override int MetadataToken
862 get { return token; }
865 public FieldBuilder DefineUninitializedData(string name, int size, FieldAttributes attributes)
867 return DefineInitializedData(name, new byte[size], attributes);
870 public FieldBuilder DefineInitializedData(string name, byte[] data, FieldAttributes attributes)
872 Type fieldType = this.ModuleBuilder.GetType("$ArrayType$" + data.Length);
873 if (fieldType == null)
875 TypeBuilder tb = this.ModuleBuilder.DefineType("$ArrayType$" + data.Length, TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.ExplicitLayout, this.Module.universe.System_ValueType, PackingSize.Size1, data.Length);
879 FieldBuilder fb = DefineField(name, fieldType, attributes | FieldAttributes.Static);
880 fb.__SetDataAndRVA(data);
884 public static MethodInfo GetMethod(Type type, MethodInfo method)
886 return new GenericMethodInstance(type, method, null);
889 public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor)
891 return new ConstructorInfoImpl(GetMethod(type, constructor.GetMethodInfo()));
894 public static FieldInfo GetField(Type type, FieldInfo field)
896 return new GenericFieldInstance(type, field);
899 public override Module Module
901 get { return owner.ModuleBuilder; }
904 public TypeToken TypeToken
906 get { return new TypeToken(token); }
909 internal void WriteTypeDefRecord(MetadataWriter mw, ref int fieldList, ref int methodList)
911 mw.Write((int)attribs);
912 mw.WriteStringIndex(typeName);
913 mw.WriteStringIndex(typeNameSpace);
914 mw.WriteTypeDefOrRef(extends);
915 mw.WriteField(fieldList);
916 mw.WriteMethodDef(methodList);
917 methodList += methods.Count;
918 fieldList += fields.Count;
921 internal void WriteMethodDefRecords(int baseRVA, MetadataWriter mw, ref int paramList)
923 foreach (MethodBuilder mb in methods)
925 mb.WriteMethodDefRecord(baseRVA, mw, ref paramList);
929 internal void ResolveMethodAndFieldTokens(ref int methodToken, ref int fieldToken, ref int parameterToken)
931 foreach (MethodBuilder method in methods)
933 method.FixupToken(methodToken++, ref parameterToken);
935 foreach (FieldBuilder field in fields)
937 field.FixupToken(fieldToken++);
941 internal void WriteParamRecords(MetadataWriter mw)
943 foreach (MethodBuilder mb in methods)
945 mb.WriteParamRecords(mw);
949 internal void WriteFieldRecords(MetadataWriter mw)
951 foreach (FieldBuilder fb in fields)
953 fb.WriteFieldRecords(mw);
957 internal ModuleBuilder ModuleBuilder
959 get { return owner.ModuleBuilder; }
962 ModuleBuilder ITypeOwner.ModuleBuilder
964 get { return owner.ModuleBuilder; }
967 internal override int GetModuleBuilderToken()
972 internal bool HasNestedTypes
974 get { return (typeFlags & TypeFlags.HasNestedTypes) != 0; }
977 // helper for ModuleBuilder.ResolveMethod()
978 internal MethodBase LookupMethod(int token)
980 foreach (MethodBuilder method in methods)
982 if (method.MetadataToken == token)
990 public bool IsCreated()
992 return (typeFlags & TypeFlags.Baked) != 0;
995 internal override void CheckBaked()
997 if ((typeFlags & TypeFlags.Baked) == 0)
999 throw new NotSupportedException();
1003 public override Type[] __GetDeclaredTypes()
1005 if (this.HasNestedTypes)
1007 List<Type> types = new List<Type>();
1008 List<int> classes = this.ModuleBuilder.NestedClass.GetNestedClasses(token);
1009 foreach (int nestedClass in classes)
1011 types.Add(this.ModuleBuilder.ResolveType(nestedClass));
1013 return types.ToArray();
1017 return Type.EmptyTypes;
1021 public override FieldInfo[] __GetDeclaredFields()
1023 return Util.ToArray(fields, Empty<FieldInfo>.Array);
1026 public override EventInfo[] __GetDeclaredEvents()
1028 return Util.ToArray(events, Empty<EventInfo>.Array);
1031 public override PropertyInfo[] __GetDeclaredProperties()
1033 return Util.ToArray(properties, Empty<PropertyInfo>.Array);
1036 internal override bool IsModulePseudoType
1038 get { return token == 0x02000001; }
1042 sealed class BakedType : Type
1044 private readonly TypeBuilder typeBuilder;
1046 internal BakedType(TypeBuilder typeBuilder)
1048 this.typeBuilder = typeBuilder;
1051 public override string AssemblyQualifiedName
1053 get { return typeBuilder.AssemblyQualifiedName; }
1056 public override Type BaseType
1058 get { return typeBuilder.BaseType; }
1061 public override string __Name
1063 get { return typeBuilder.__Name; }
1066 public override string __Namespace
1068 get { return typeBuilder.__Namespace; }
1071 public override string Name
1073 // we need to escape here, because TypeBuilder.Name does not escape
1074 get { return TypeNameParser.Escape(typeBuilder.__Name); }
1077 public override string FullName
1079 get { return GetFullName(); }
1082 public override TypeAttributes Attributes
1084 get { return typeBuilder.Attributes; }
1087 public override Type[] __GetDeclaredInterfaces()
1089 return typeBuilder.__GetDeclaredInterfaces();
1092 public override MethodBase[] __GetDeclaredMethods()
1094 return typeBuilder.__GetDeclaredMethods();
1097 public override __MethodImplMap __GetMethodImplMap()
1099 return typeBuilder.__GetMethodImplMap();
1102 public override FieldInfo[] __GetDeclaredFields()
1104 return typeBuilder.__GetDeclaredFields();
1107 public override EventInfo[] __GetDeclaredEvents()
1109 return typeBuilder.__GetDeclaredEvents();
1112 public override PropertyInfo[] __GetDeclaredProperties()
1114 return typeBuilder.__GetDeclaredProperties();
1117 public override Type[] __GetDeclaredTypes()
1119 return typeBuilder.__GetDeclaredTypes();
1122 public override Type DeclaringType
1124 get { return typeBuilder.DeclaringType; }
1127 public override StructLayoutAttribute StructLayoutAttribute
1129 get { return typeBuilder.StructLayoutAttribute; }
1132 public override Type UnderlyingSystemType
1134 // Type.Equals/GetHashCode relies on this
1135 get { return typeBuilder; }
1138 public override Type[] GetGenericArguments()
1140 return typeBuilder.GetGenericArguments();
1143 internal override Type GetGenericTypeArgument(int index)
1145 return typeBuilder.GetGenericTypeArgument(index);
1148 public override Type[][] __GetGenericArgumentsOptionalCustomModifiers()
1150 return typeBuilder.__GetGenericArgumentsOptionalCustomModifiers();
1153 public override Type[][] __GetGenericArgumentsRequiredCustomModifiers()
1155 return typeBuilder.__GetGenericArgumentsRequiredCustomModifiers();
1158 public override bool IsGenericType
1160 get { return typeBuilder.IsGenericType; }
1163 public override bool IsGenericTypeDefinition
1165 get { return typeBuilder.IsGenericTypeDefinition; }
1168 public override bool ContainsGenericParameters
1170 get { return typeBuilder.ContainsGenericParameters; }
1173 public override int MetadataToken
1175 get { return typeBuilder.MetadataToken; }
1178 public override Module Module
1180 get { return typeBuilder.Module; }
1183 internal override int GetModuleBuilderToken()
1185 return typeBuilder.GetModuleBuilderToken();