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
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 : Type, 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 foreach (MethodBuilder mb in methods)
712 if (declarativeSecurity != null)
714 this.ModuleBuilder.AddDeclarativeSecurity(token, declarativeSecurity);
716 if (!IsModulePseudoType)
718 Type baseType = this.BaseType;
719 if (baseType != null)
721 extends = this.ModuleBuilder.GetTypeToken(baseType).Token;
724 if (interfaces != null)
726 foreach (Type interfaceType in interfaces)
728 InterfaceImplTable.Record rec = new InterfaceImplTable.Record();
730 rec.Interface = this.ModuleBuilder.GetTypeToken(interfaceType).Token;
731 this.ModuleBuilder.InterfaceImpl.AddRecord(rec);
734 return new BakedType(this);
737 internal void PopulatePropertyAndEventTables()
739 if (properties != null)
741 PropertyMapTable.Record rec = new PropertyMapTable.Record();
743 rec.PropertyList = this.ModuleBuilder.Property.RowCount + 1;
744 this.ModuleBuilder.PropertyMap.AddRecord(rec);
745 foreach (PropertyBuilder pb in properties)
752 EventMapTable.Record rec = new EventMapTable.Record();
754 rec.EventList = this.ModuleBuilder.Event.RowCount + 1;
755 this.ModuleBuilder.EventMap.AddRecord(rec);
756 foreach (EventBuilder eb in events)
763 public override Type BaseType
767 if (lazyBaseType == null && !IsInterface)
769 Type obj = Module.universe.System_Object;
779 public override string FullName
785 return this.DeclaringType.FullName + "+" + TypeNameParser.Escape(name);
789 return TypeNameParser.Escape(name);
793 return TypeNameParser.Escape(ns) + "." + TypeNameParser.Escape(name);
798 public override string __Name
803 public override string __Namespace
808 public override string Name
810 // FXBUG for a TypeBuilder the name is not escaped
814 public override string Namespace
818 // for some reason, TypeBuilder doesn't return null (and mcs depends on this)
819 // note also that we don't return the declaring type namespace for nested types
824 public override TypeAttributes Attributes
826 get { return attribs; }
829 public void __SetAttributes(TypeAttributes attributes)
831 this.attribs = attributes;
834 public override Type[] __GetDeclaredInterfaces()
836 return Util.ToArray(interfaces, Type.EmptyTypes);
839 public override MethodBase[] __GetDeclaredMethods()
841 MethodBase[] methods = new MethodBase[this.methods.Count];
842 for (int i = 0; i < methods.Length; i++)
844 MethodBuilder mb = this.methods[i];
845 if (mb.IsConstructor)
847 methods[i] = new ConstructorInfoImpl(mb);
857 public override StructLayoutAttribute StructLayoutAttribute
862 switch (attribs & TypeAttributes.LayoutMask)
864 case TypeAttributes.ExplicitLayout:
865 layout = LayoutKind.Explicit;
867 case TypeAttributes.SequentialLayout:
868 layout = LayoutKind.Sequential;
871 layout = LayoutKind.Auto;
874 StructLayoutAttribute attr = new StructLayoutAttribute(layout);
875 attr.Pack = (ushort)pack;
877 switch (attribs & TypeAttributes.StringFormatMask)
879 case TypeAttributes.AutoClass:
880 attr.CharSet = CharSet.Auto;
882 case TypeAttributes.UnicodeClass:
883 attr.CharSet = CharSet.Unicode;
885 case TypeAttributes.AnsiClass:
886 attr.CharSet = CharSet.Ansi;
889 attr.CharSet = CharSet.None;
896 public override Type DeclaringType
898 get { return owner as TypeBuilder; }
901 public override bool IsGenericType
903 get { return IsGenericTypeDefinition; }
906 public override bool IsGenericTypeDefinition
908 get { return (typeFlags & TypeFlags.IsGenericTypeDefinition) != 0; }
911 public override int MetadataToken
913 get { return token; }
916 public FieldBuilder DefineUninitializedData(string name, int size, FieldAttributes attributes)
918 return DefineInitializedData(name, new byte[size], attributes);
921 public FieldBuilder DefineInitializedData(string name, byte[] data, FieldAttributes attributes)
923 Type fieldType = this.ModuleBuilder.GetType("$ArrayType$" + data.Length);
924 if (fieldType == null)
926 TypeBuilder tb = this.ModuleBuilder.DefineType("$ArrayType$" + data.Length, TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.ExplicitLayout, this.Module.universe.System_ValueType, PackingSize.Size1, data.Length);
930 FieldBuilder fb = DefineField(name, fieldType, attributes | FieldAttributes.Static);
931 fb.__SetDataAndRVA(data);
935 public static MethodInfo GetMethod(Type type, MethodInfo method)
937 return new GenericMethodInstance(type, method, null);
940 public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor)
942 return new ConstructorInfoImpl(GetMethod(type, constructor.GetMethodInfo()));
945 public static FieldInfo GetField(Type type, FieldInfo field)
947 return new GenericFieldInstance(type, field);
950 public override Module Module
952 get { return owner.ModuleBuilder; }
955 public TypeToken TypeToken
957 get { return new TypeToken(token); }
960 internal void WriteTypeDefRecord(MetadataWriter mw, ref int fieldList, ref int methodList)
962 mw.Write((int)attribs);
963 mw.WriteStringIndex(typeName);
964 mw.WriteStringIndex(typeNameSpace);
965 mw.WriteTypeDefOrRef(extends);
966 mw.WriteField(fieldList);
967 mw.WriteMethodDef(methodList);
968 methodList += methods.Count;
969 fieldList += fields.Count;
972 internal void WriteMethodDefRecords(int baseRVA, MetadataWriter mw, ref int paramList)
974 foreach (MethodBuilder mb in methods)
976 mb.WriteMethodDefRecord(baseRVA, mw, ref paramList);
980 internal void ResolveMethodAndFieldTokens(ref int methodToken, ref int fieldToken, ref int parameterToken)
982 foreach (MethodBuilder method in methods)
984 method.FixupToken(methodToken++, ref parameterToken);
986 foreach (FieldBuilder field in fields)
988 field.FixupToken(fieldToken++);
992 internal void WriteParamRecords(MetadataWriter mw)
994 foreach (MethodBuilder mb in methods)
996 mb.WriteParamRecords(mw);
1000 internal void WriteFieldRecords(MetadataWriter mw)
1002 foreach (FieldBuilder fb in fields)
1004 fb.WriteFieldRecords(mw);
1008 internal ModuleBuilder ModuleBuilder
1010 get { return owner.ModuleBuilder; }
1013 ModuleBuilder ITypeOwner.ModuleBuilder
1015 get { return owner.ModuleBuilder; }
1018 internal override int GetModuleBuilderToken()
1023 internal bool HasNestedTypes
1025 get { return (typeFlags & TypeFlags.HasNestedTypes) != 0; }
1028 // helper for ModuleBuilder.ResolveMethod()
1029 internal MethodBase LookupMethod(int token)
1031 foreach (MethodBuilder method in methods)
1033 if (method.MetadataToken == token)
1041 public bool IsCreated()
1043 return (typeFlags & TypeFlags.Baked) != 0;
1046 internal override void CheckBaked()
1048 if ((typeFlags & TypeFlags.Baked) == 0)
1050 throw new NotSupportedException();
1054 public override Type[] __GetDeclaredTypes()
1056 if (this.HasNestedTypes)
1058 List<Type> types = new List<Type>();
1059 List<int> classes = this.ModuleBuilder.NestedClass.GetNestedClasses(token);
1060 foreach (int nestedClass in classes)
1062 types.Add(this.ModuleBuilder.ResolveType(nestedClass));
1064 return types.ToArray();
1068 return Type.EmptyTypes;
1072 public override FieldInfo[] __GetDeclaredFields()
1074 return Util.ToArray(fields, Empty<FieldInfo>.Array);
1077 public override EventInfo[] __GetDeclaredEvents()
1079 return Util.ToArray(events, Empty<EventInfo>.Array);
1082 public override PropertyInfo[] __GetDeclaredProperties()
1084 return Util.ToArray(properties, Empty<PropertyInfo>.Array);
1087 internal override bool IsModulePseudoType
1089 get { return token == 0x02000001; }
1092 internal override bool IsBaked
1094 get { return IsCreated(); }
1098 sealed class BakedType : Type
1100 internal BakedType(TypeBuilder typeBuilder)
1105 public override string AssemblyQualifiedName
1107 get { return underlyingType.AssemblyQualifiedName; }
1110 public override Type BaseType
1112 get { return underlyingType.BaseType; }
1115 public override string __Name
1117 get { return underlyingType.__Name; }
1120 public override string __Namespace
1122 get { return underlyingType.__Namespace; }
1125 public override string Name
1127 // we need to escape here, because TypeBuilder.Name does not escape
1128 get { return TypeNameParser.Escape(underlyingType.__Name); }
1131 public override string FullName
1133 get { return GetFullName(); }
1136 public override TypeAttributes Attributes
1138 get { return underlyingType.Attributes; }
1141 public override Type[] __GetDeclaredInterfaces()
1143 return underlyingType.__GetDeclaredInterfaces();
1146 public override MethodBase[] __GetDeclaredMethods()
1148 return underlyingType.__GetDeclaredMethods();
1151 public override __MethodImplMap __GetMethodImplMap()
1153 return underlyingType.__GetMethodImplMap();
1156 public override FieldInfo[] __GetDeclaredFields()
1158 return underlyingType.__GetDeclaredFields();
1161 public override EventInfo[] __GetDeclaredEvents()
1163 return underlyingType.__GetDeclaredEvents();
1166 public override PropertyInfo[] __GetDeclaredProperties()
1168 return underlyingType.__GetDeclaredProperties();
1171 public override Type[] __GetDeclaredTypes()
1173 return underlyingType.__GetDeclaredTypes();
1176 public override Type DeclaringType
1178 get { return underlyingType.DeclaringType; }
1181 public override StructLayoutAttribute StructLayoutAttribute
1183 get { return underlyingType.StructLayoutAttribute; }
1186 public override Type[] GetGenericArguments()
1188 return underlyingType.GetGenericArguments();
1191 internal override Type GetGenericTypeArgument(int index)
1193 return underlyingType.GetGenericTypeArgument(index);
1196 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
1198 return underlyingType.__GetGenericArgumentsCustomModifiers();
1201 public override bool IsGenericType
1203 get { return underlyingType.IsGenericType; }
1206 public override bool IsGenericTypeDefinition
1208 get { return underlyingType.IsGenericTypeDefinition; }
1211 public override bool ContainsGenericParameters
1213 get { return underlyingType.ContainsGenericParameters; }
1216 public override int MetadataToken
1218 get { return underlyingType.MetadataToken; }
1221 public override Module Module
1223 get { return underlyingType.Module; }
1226 internal override int GetModuleBuilderToken()
1228 return underlyingType.GetModuleBuilderToken();
1231 internal override bool IsBaked
1233 get { return true; }