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 : TypeInfo
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
148 internal override void CheckBaked()
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 public override int MetadataToken
204 return (GenericParamTable.Index << 24) | paramPseudoIndex;
208 internal override int GetModuleBuilderToken()
212 ByteBuffer spec = new ByteBuffer(5);
213 Signature.WriteTypeSpec(this.ModuleBuilder, spec, this);
214 typeToken = 0x1B000000 | this.ModuleBuilder.TypeSpec.AddRecord(this.ModuleBuilder.Blobs.Add(spec));
219 internal override Type BindTypeParameters(IGenericBinder binder)
223 return binder.BindTypeParameter(this);
227 return binder.BindMethodParameter(this);
231 internal override int GetCurrentToken()
233 if (this.ModuleBuilder.IsSaved)
235 return (GenericParamTable.Index << 24) | this.Module.GenericParam.GetIndexFixup()[paramPseudoIndex - 1] + 1;
239 return (GenericParamTable.Index << 24) | paramPseudoIndex;
243 internal override bool IsBaked
245 get { return ((MemberInfo)type ?? method).IsBaked; }
249 public sealed class TypeBuilder : TypeInfo, ITypeOwner
251 public const int UnspecifiedTypeSize = 0;
252 private readonly ITypeOwner owner;
253 private readonly int token;
255 private Type lazyBaseType; // (lazyBaseType == null && attribs & TypeAttributes.Interface) == 0) => BaseType == System.Object
256 private readonly int typeName;
257 private readonly int typeNameSpace;
258 private readonly string ns;
259 private readonly string name;
260 private readonly List<MethodBuilder> methods = new List<MethodBuilder>();
261 private readonly List<FieldBuilder> fields = new List<FieldBuilder>();
262 private List<PropertyBuilder> properties;
263 private List<EventBuilder> events;
264 private TypeAttributes attribs;
265 private GenericTypeParameterBuilder[] gtpb;
266 private List<CustomAttributeBuilder> declarativeSecurity;
267 private List<Type> interfaces;
270 private bool hasLayout;
272 internal TypeBuilder(ITypeOwner owner, string ns, string name)
275 this.token = this.ModuleBuilder.TypeDef.AllocToken();
278 this.typeNameSpace = ns == null ? 0 : this.ModuleBuilder.Strings.Add(ns);
279 this.typeName = this.ModuleBuilder.Strings.Add(name);
280 MarkEnumOrValueType(ns, name);
283 public ConstructorBuilder DefineDefaultConstructor(MethodAttributes attributes)
285 ConstructorBuilder cb = DefineConstructor(attributes, CallingConventions.Standard, Type.EmptyTypes);
286 ILGenerator ilgen = cb.GetILGenerator();
287 ilgen.Emit(OpCodes.Ldarg_0);
288 ilgen.Emit(OpCodes.Call, BaseType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null));
289 ilgen.Emit(OpCodes.Ret);
293 public ConstructorBuilder DefineConstructor(MethodAttributes attribs, CallingConventions callConv, Type[] parameterTypes)
295 return DefineConstructor(attribs, callConv, parameterTypes, null, null);
298 public ConstructorBuilder DefineConstructor(MethodAttributes attribs, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
300 attribs |= MethodAttributes.RTSpecialName | MethodAttributes.SpecialName;
301 string name = (attribs & MethodAttributes.Static) == 0 ? ConstructorInfo.ConstructorName : ConstructorInfo.TypeConstructorName;
302 MethodBuilder mb = DefineMethod(name, attribs, callingConvention, null, null, null, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
303 return new ConstructorBuilder(mb);
306 public ConstructorBuilder DefineTypeInitializer()
308 MethodBuilder mb = DefineMethod(ConstructorInfo.TypeConstructorName, MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName, null, Type.EmptyTypes);
309 return new ConstructorBuilder(mb);
312 private MethodBuilder CreateMethodBuilder(string name, MethodAttributes attributes, CallingConventions callingConvention)
314 this.ModuleBuilder.MethodDef.AddVirtualRecord();
315 MethodBuilder mb = new MethodBuilder(this, name, attributes, callingConvention);
320 public MethodBuilder DefineMethod(string name, MethodAttributes attribs)
322 return DefineMethod(name, attribs, CallingConventions.Standard);
325 public MethodBuilder DefineMethod(string name, MethodAttributes attribs, CallingConventions callingConvention)
327 return CreateMethodBuilder(name, attribs, callingConvention);
330 public MethodBuilder DefineMethod(string name, MethodAttributes attribs, Type returnType, Type[] parameterTypes)
332 return DefineMethod(name, attribs, CallingConventions.Standard, returnType, null, null, parameterTypes, null, null);
335 public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
337 return DefineMethod(name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
340 public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
342 MethodBuilder mb = CreateMethodBuilder(name, attributes, callingConvention);
343 mb.SetSignature(returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
347 public MethodBuilder DefinePInvokeMethod(string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
349 return DefinePInvokeMethod(name, dllName, null, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
352 public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
354 return DefinePInvokeMethod(name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
357 public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention,
358 Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers,
359 Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers,
360 CallingConvention nativeCallConv, CharSet nativeCharSet)
362 MethodBuilder mb = DefineMethod(name, attributes | MethodAttributes.PinvokeImpl, callingConvention,
363 returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers,
364 parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
365 mb.SetDllImportPseudoCustomAttribute(dllName, entryName, nativeCallConv, nativeCharSet, null, null, null, null, null);
369 public void DefineMethodOverride(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)
371 MethodImplTable.Record rec = new MethodImplTable.Record();
373 rec.MethodBody = this.ModuleBuilder.GetMethodToken(methodInfoBody).Token;
374 rec.MethodDeclaration = this.ModuleBuilder.GetMethodTokenWinRT(methodInfoDeclaration);
375 this.ModuleBuilder.MethodImpl.AddRecord(rec);
378 public FieldBuilder DefineField(string name, Type fieldType, FieldAttributes attribs)
380 return DefineField(name, fieldType, null, null, attribs);
383 public FieldBuilder DefineField(string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes)
385 return __DefineField(fieldName, type, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers), attributes);
388 public FieldBuilder __DefineField(string fieldName, Type type, CustomModifiers customModifiers, FieldAttributes attributes)
390 FieldBuilder fb = new FieldBuilder(this, fieldName, type, customModifiers, attributes);
395 public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes)
397 return DefineProperty(name, attributes, returnType, null, null, parameterTypes, null, null);
400 public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers,
401 Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
403 return DefinePropertyImpl(name, attributes, CallingConventions.Standard, true, returnType, parameterTypes,
404 PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, Util.NullSafeLength(parameterTypes)));
407 public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, CallingConventions callingConvention,
408 Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers,
409 Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
411 return DefinePropertyImpl(name, attributes, callingConvention, false, returnType, parameterTypes,
412 PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, Util.NullSafeLength(parameterTypes)));
415 public PropertyBuilder __DefineProperty(string name, PropertyAttributes attributes, CallingConventions callingConvention,
416 Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
418 return DefinePropertyImpl(name, attributes, callingConvention, false, returnType, parameterTypes,
419 PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, Util.NullSafeLength(parameterTypes)));
422 private PropertyBuilder DefinePropertyImpl(string name, PropertyAttributes attributes, CallingConventions callingConvention, bool patchCallingConvention,
423 Type returnType, Type[] parameterTypes, PackedCustomModifiers customModifiers)
425 if (properties == null)
427 properties = new List<PropertyBuilder>();
429 PropertySignature sig = PropertySignature.Create(callingConvention, returnType, parameterTypes, customModifiers);
430 PropertyBuilder pb = new PropertyBuilder(this, name, attributes, sig, patchCallingConvention);
435 public EventBuilder DefineEvent(string name, EventAttributes attributes, Type eventtype)
439 events = new List<EventBuilder>();
441 EventBuilder eb = new EventBuilder(this, name, attributes, eventtype);
446 public TypeBuilder DefineNestedType(string name)
448 return DefineNestedType(name, TypeAttributes.Class | TypeAttributes.NestedPrivate);
451 public TypeBuilder DefineNestedType(string name, TypeAttributes attribs)
453 return DefineNestedType(name, attribs, null);
456 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, Type[] interfaces)
458 TypeBuilder tb = DefineNestedType(name, attr, parent);
459 if (interfaces != null)
461 foreach (Type iface in interfaces)
463 tb.AddInterfaceImplementation(iface);
469 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent)
471 return DefineNestedType(name, attr, parent, 0);
474 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, int typeSize)
476 return DefineNestedType(name, attr, parent, PackingSize.Unspecified, typeSize);
479 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, PackingSize packSize)
481 return DefineNestedType(name, attr, parent, packSize, 0);
484 private TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, PackingSize packSize, int typeSize)
487 int lastdot = name.LastIndexOf('.');
490 ns = name.Substring(0, lastdot);
491 name = name.Substring(lastdot + 1);
493 TypeBuilder typeBuilder = __DefineNestedType(ns, name);
494 typeBuilder.__SetAttributes(attr);
495 typeBuilder.SetParent(parent);
496 if (packSize != PackingSize.Unspecified || typeSize != 0)
498 typeBuilder.__SetLayout((int)packSize, typeSize);
503 public TypeBuilder __DefineNestedType(string ns, string name)
505 this.typeFlags |= TypeFlags.HasNestedTypes;
506 TypeBuilder typeBuilder = this.ModuleBuilder.DefineType(this, ns, name);
507 NestedClassTable.Record rec = new NestedClassTable.Record();
508 rec.NestedClass = typeBuilder.MetadataToken;
509 rec.EnclosingClass = this.MetadataToken;
510 this.ModuleBuilder.NestedClass.AddRecord(rec);
514 public void SetParent(Type parent)
516 lazyBaseType = parent;
519 public void AddInterfaceImplementation(Type interfaceType)
521 if (interfaces == null)
523 interfaces = new List<Type>();
525 interfaces.Add(interfaceType);
528 public void __SetInterfaceImplementationCustomAttribute(Type interfaceType, CustomAttributeBuilder cab)
530 this.ModuleBuilder.SetInterfaceImplementationCustomAttribute(this, interfaceType, cab);
538 public PackingSize PackingSize
540 get { return (PackingSize)pack; }
543 public override bool __GetLayout(out int packingSize, out int typeSize)
545 packingSize = this.pack;
546 typeSize = this.size;
550 public void __SetLayout(int packingSize, int typesize)
552 this.pack = (short)packingSize;
553 this.size = typesize;
554 this.hasLayout = true;
557 private void SetStructLayoutPseudoCustomAttribute(CustomAttributeBuilder customBuilder)
559 object val = customBuilder.GetConstructorArgument(0);
563 layout = (LayoutKind)(short)val;
567 layout = (LayoutKind)val;
569 StructLayoutAttribute attr = new StructLayoutAttribute(layout);
570 attr.Pack = (int?)customBuilder.GetFieldValue("Pack") ?? 0;
571 attr.Size = (int?)customBuilder.GetFieldValue("Size") ?? 0;
572 attr.CharSet = customBuilder.GetFieldValue<CharSet>("CharSet") ?? CharSet.None;
573 attribs &= ~TypeAttributes.LayoutMask;
576 case LayoutKind.Auto:
577 attribs |= TypeAttributes.AutoLayout;
579 case LayoutKind.Explicit:
580 attribs |= TypeAttributes.ExplicitLayout;
582 case LayoutKind.Sequential:
583 attribs |= TypeAttributes.SequentialLayout;
586 attribs &= ~TypeAttributes.StringFormatMask;
587 switch (attr.CharSet)
591 attribs |= TypeAttributes.AnsiClass;
594 attribs |= TypeAttributes.AutoClass;
596 case CharSet.Unicode:
597 attribs |= TypeAttributes.UnicodeClass;
600 pack = (short)attr.Pack;
602 hasLayout = pack != 0 || size != 0;
605 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
607 SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
610 public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
612 Universe u = this.ModuleBuilder.universe;
613 Type type = customBuilder.Constructor.DeclaringType;
614 if (type == u.System_Runtime_InteropServices_StructLayoutAttribute)
616 SetStructLayoutPseudoCustomAttribute(customBuilder.DecodeBlob(this.Assembly));
618 else if (type == u.System_SerializableAttribute)
620 attribs |= TypeAttributes.Serializable;
622 else if (type == u.System_Runtime_InteropServices_ComImportAttribute)
624 attribs |= TypeAttributes.Import;
626 else if (type == u.System_Runtime_CompilerServices_SpecialNameAttribute)
628 attribs |= TypeAttributes.SpecialName;
632 if (type == u.System_Security_SuppressUnmanagedCodeSecurityAttribute)
634 attribs |= TypeAttributes.HasSecurity;
636 this.ModuleBuilder.SetCustomAttribute(token, customBuilder);
640 public void __AddDeclarativeSecurity(CustomAttributeBuilder customBuilder)
642 attribs |= TypeAttributes.HasSecurity;
643 if (declarativeSecurity == null)
645 declarativeSecurity = new List<CustomAttributeBuilder>();
647 declarativeSecurity.Add(customBuilder);
650 public void AddDeclarativeSecurity(System.Security.Permissions.SecurityAction securityAction, System.Security.PermissionSet permissionSet)
652 this.ModuleBuilder.AddDeclarativeSecurity(token, securityAction, permissionSet);
653 this.attribs |= TypeAttributes.HasSecurity;
656 public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names)
658 typeFlags |= TypeFlags.IsGenericTypeDefinition;
659 gtpb = new GenericTypeParameterBuilder[names.Length];
660 for (int i = 0; i < names.Length; i++)
662 gtpb[i] = new GenericTypeParameterBuilder(names[i], this, null, i);
664 return (GenericTypeParameterBuilder[])gtpb.Clone();
667 public override Type[] GetGenericArguments()
669 return Util.Copy(gtpb);
672 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
674 return gtpb == null ? Empty<CustomModifiers>.Array : new CustomModifiers[gtpb.Length];
677 internal override Type GetGenericTypeArgument(int index)
682 public override bool ContainsGenericParameters
684 get { return gtpb != null; }
687 public override Type GetGenericTypeDefinition()
692 public Type CreateType()
694 if ((typeFlags & TypeFlags.Baked) != 0)
696 // .NET allows multiple invocations (subsequent invocations return the same baked type)
697 throw new NotImplementedException();
699 typeFlags |= TypeFlags.Baked;
702 ClassLayoutTable.Record rec = new ClassLayoutTable.Record();
703 rec.PackingSize = pack;
704 rec.ClassSize = size;
706 this.ModuleBuilder.ClassLayout.AddRecord(rec);
708 bool hasConstructor = false;
709 foreach (MethodBuilder mb in methods)
711 hasConstructor |= mb.IsSpecialName && mb.Name == ConstructorInfo.ConstructorName;
714 if (!hasConstructor && !IsModulePseudoType && !IsInterface && !IsValueType && !(IsAbstract && IsSealed))
716 ((MethodBuilder)DefineDefaultConstructor(MethodAttributes.Public).GetMethodInfo()).Bake();
718 if (declarativeSecurity != null)
720 this.ModuleBuilder.AddDeclarativeSecurity(token, declarativeSecurity);
722 if (!IsModulePseudoType)
724 Type baseType = this.BaseType;
725 if (baseType != null)
727 extends = this.ModuleBuilder.GetTypeToken(baseType).Token;
730 if (interfaces != null)
732 foreach (Type interfaceType in interfaces)
734 InterfaceImplTable.Record rec = new InterfaceImplTable.Record();
736 rec.Interface = this.ModuleBuilder.GetTypeToken(interfaceType).Token;
737 this.ModuleBuilder.InterfaceImpl.AddRecord(rec);
740 return new BakedType(this);
743 internal void PopulatePropertyAndEventTables()
745 if (properties != null)
747 PropertyMapTable.Record rec = new PropertyMapTable.Record();
749 rec.PropertyList = this.ModuleBuilder.Property.RowCount + 1;
750 this.ModuleBuilder.PropertyMap.AddRecord(rec);
751 foreach (PropertyBuilder pb in properties)
758 EventMapTable.Record rec = new EventMapTable.Record();
760 rec.EventList = this.ModuleBuilder.Event.RowCount + 1;
761 this.ModuleBuilder.EventMap.AddRecord(rec);
762 foreach (EventBuilder eb in events)
769 public override Type BaseType
773 if (lazyBaseType == null && !IsInterface)
775 Type obj = Module.universe.System_Object;
785 public override string FullName
791 return this.DeclaringType.FullName + "+" + TypeNameParser.Escape(name);
795 return TypeNameParser.Escape(name);
799 return TypeNameParser.Escape(ns) + "." + TypeNameParser.Escape(name);
804 public override string __Name
809 public override string __Namespace
814 public override string Name
816 // FXBUG for a TypeBuilder the name is not escaped
820 public override string Namespace
824 // for some reason, TypeBuilder doesn't return null (and mcs depends on this)
825 // note also that we don't return the declaring type namespace for nested types
830 public override TypeAttributes Attributes
832 get { return attribs; }
835 public void __SetAttributes(TypeAttributes attributes)
837 this.attribs = attributes;
840 public override Type[] __GetDeclaredInterfaces()
842 return Util.ToArray(interfaces, Type.EmptyTypes);
845 public override MethodBase[] __GetDeclaredMethods()
847 MethodBase[] methods = new MethodBase[this.methods.Count];
848 for (int i = 0; i < methods.Length; i++)
850 MethodBuilder mb = this.methods[i];
851 if (mb.IsConstructor)
853 methods[i] = new ConstructorInfoImpl(mb);
863 public override StructLayoutAttribute StructLayoutAttribute
868 switch (attribs & TypeAttributes.LayoutMask)
870 case TypeAttributes.ExplicitLayout:
871 layout = LayoutKind.Explicit;
873 case TypeAttributes.SequentialLayout:
874 layout = LayoutKind.Sequential;
877 layout = LayoutKind.Auto;
880 StructLayoutAttribute attr = new StructLayoutAttribute(layout);
881 attr.Pack = (ushort)pack;
883 switch (attribs & TypeAttributes.StringFormatMask)
885 case TypeAttributes.AutoClass:
886 attr.CharSet = CharSet.Auto;
888 case TypeAttributes.UnicodeClass:
889 attr.CharSet = CharSet.Unicode;
891 case TypeAttributes.AnsiClass:
892 attr.CharSet = CharSet.Ansi;
895 attr.CharSet = CharSet.None;
902 public override Type DeclaringType
904 get { return owner as TypeBuilder; }
907 public override bool IsGenericType
909 get { return IsGenericTypeDefinition; }
912 public override bool IsGenericTypeDefinition
914 get { return (typeFlags & TypeFlags.IsGenericTypeDefinition) != 0; }
917 public override int MetadataToken
919 get { return token; }
922 public FieldBuilder DefineUninitializedData(string name, int size, FieldAttributes attributes)
924 return DefineInitializedData(name, new byte[size], attributes);
927 public FieldBuilder DefineInitializedData(string name, byte[] data, FieldAttributes attributes)
929 Type fieldType = this.ModuleBuilder.GetType("$ArrayType$" + data.Length);
930 if (fieldType == null)
932 TypeBuilder tb = this.ModuleBuilder.DefineType("$ArrayType$" + data.Length, TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.ExplicitLayout, this.Module.universe.System_ValueType, PackingSize.Size1, data.Length);
936 FieldBuilder fb = DefineField(name, fieldType, attributes | FieldAttributes.Static);
937 fb.__SetDataAndRVA(data);
941 public static MethodInfo GetMethod(Type type, MethodInfo method)
943 return new GenericMethodInstance(type, method, null);
946 public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor)
948 return new ConstructorInfoImpl(GetMethod(type, constructor.GetMethodInfo()));
951 public static FieldInfo GetField(Type type, FieldInfo field)
953 return new GenericFieldInstance(type, field);
956 public override Module Module
958 get { return owner.ModuleBuilder; }
961 public TypeToken TypeToken
963 get { return new TypeToken(token); }
966 internal void WriteTypeDefRecord(MetadataWriter mw, ref int fieldList, ref int methodList)
968 mw.Write((int)attribs);
969 mw.WriteStringIndex(typeName);
970 mw.WriteStringIndex(typeNameSpace);
971 mw.WriteTypeDefOrRef(extends);
972 mw.WriteField(fieldList);
973 mw.WriteMethodDef(methodList);
974 methodList += methods.Count;
975 fieldList += fields.Count;
978 internal void WriteMethodDefRecords(int baseRVA, MetadataWriter mw, ref int paramList)
980 foreach (MethodBuilder mb in methods)
982 mb.WriteMethodDefRecord(baseRVA, mw, ref paramList);
986 internal void ResolveMethodAndFieldTokens(ref int methodToken, ref int fieldToken, ref int parameterToken)
988 foreach (MethodBuilder method in methods)
990 method.FixupToken(methodToken++, ref parameterToken);
992 foreach (FieldBuilder field in fields)
994 field.FixupToken(fieldToken++);
998 internal void WriteParamRecords(MetadataWriter mw)
1000 foreach (MethodBuilder mb in methods)
1002 mb.WriteParamRecords(mw);
1006 internal void WriteFieldRecords(MetadataWriter mw)
1008 foreach (FieldBuilder fb in fields)
1010 fb.WriteFieldRecords(mw);
1014 internal ModuleBuilder ModuleBuilder
1016 get { return owner.ModuleBuilder; }
1019 ModuleBuilder ITypeOwner.ModuleBuilder
1021 get { return owner.ModuleBuilder; }
1024 internal override int GetModuleBuilderToken()
1029 internal bool HasNestedTypes
1031 get { return (typeFlags & TypeFlags.HasNestedTypes) != 0; }
1034 // helper for ModuleBuilder.ResolveMethod()
1035 internal MethodBase LookupMethod(int token)
1037 foreach (MethodBuilder method in methods)
1039 if (method.MetadataToken == token)
1047 public bool IsCreated()
1049 return (typeFlags & TypeFlags.Baked) != 0;
1052 internal override void CheckBaked()
1054 if ((typeFlags & TypeFlags.Baked) == 0)
1056 throw new NotSupportedException();
1060 public override Type[] __GetDeclaredTypes()
1062 if (this.HasNestedTypes)
1064 List<Type> types = new List<Type>();
1065 List<int> classes = this.ModuleBuilder.NestedClass.GetNestedClasses(token);
1066 foreach (int nestedClass in classes)
1068 types.Add(this.ModuleBuilder.ResolveType(nestedClass));
1070 return types.ToArray();
1074 return Type.EmptyTypes;
1078 public override FieldInfo[] __GetDeclaredFields()
1080 return Util.ToArray(fields, Empty<FieldInfo>.Array);
1083 public override EventInfo[] __GetDeclaredEvents()
1085 return Util.ToArray(events, Empty<EventInfo>.Array);
1088 public override PropertyInfo[] __GetDeclaredProperties()
1090 return Util.ToArray(properties, Empty<PropertyInfo>.Array);
1093 internal override bool IsModulePseudoType
1095 get { return token == 0x02000001; }
1098 internal override bool IsBaked
1100 get { return IsCreated(); }
1104 sealed class BakedType : TypeInfo
1106 internal BakedType(TypeBuilder typeBuilder)
1111 public override string AssemblyQualifiedName
1113 get { return underlyingType.AssemblyQualifiedName; }
1116 public override Type BaseType
1118 get { return underlyingType.BaseType; }
1121 public override string __Name
1123 get { return underlyingType.__Name; }
1126 public override string __Namespace
1128 get { return underlyingType.__Namespace; }
1131 public override string Name
1133 // we need to escape here, because TypeBuilder.Name does not escape
1134 get { return TypeNameParser.Escape(underlyingType.__Name); }
1137 public override string FullName
1139 get { return GetFullName(); }
1142 public override TypeAttributes Attributes
1144 get { return underlyingType.Attributes; }
1147 public override Type[] __GetDeclaredInterfaces()
1149 return underlyingType.__GetDeclaredInterfaces();
1152 public override MethodBase[] __GetDeclaredMethods()
1154 return underlyingType.__GetDeclaredMethods();
1157 public override __MethodImplMap __GetMethodImplMap()
1159 return underlyingType.__GetMethodImplMap();
1162 public override FieldInfo[] __GetDeclaredFields()
1164 return underlyingType.__GetDeclaredFields();
1167 public override EventInfo[] __GetDeclaredEvents()
1169 return underlyingType.__GetDeclaredEvents();
1172 public override PropertyInfo[] __GetDeclaredProperties()
1174 return underlyingType.__GetDeclaredProperties();
1177 public override Type[] __GetDeclaredTypes()
1179 return underlyingType.__GetDeclaredTypes();
1182 public override Type DeclaringType
1184 get { return underlyingType.DeclaringType; }
1187 public override StructLayoutAttribute StructLayoutAttribute
1189 get { return underlyingType.StructLayoutAttribute; }
1192 public override Type[] GetGenericArguments()
1194 return underlyingType.GetGenericArguments();
1197 internal override Type GetGenericTypeArgument(int index)
1199 return underlyingType.GetGenericTypeArgument(index);
1202 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
1204 return underlyingType.__GetGenericArgumentsCustomModifiers();
1207 public override bool IsGenericType
1209 get { return underlyingType.IsGenericType; }
1212 public override bool IsGenericTypeDefinition
1214 get { return underlyingType.IsGenericTypeDefinition; }
1217 public override bool ContainsGenericParameters
1219 get { return underlyingType.ContainsGenericParameters; }
1222 public override int MetadataToken
1224 get { return underlyingType.MetadataToken; }
1227 public override Module Module
1229 get { return underlyingType.Module; }
1232 internal override int GetModuleBuilderToken()
1234 return underlyingType.GetModuleBuilderToken();
1237 internal override bool IsBaked
1239 get { return true; }