We need you! We're working hard on the next version of Developer Fusion - Let us know what you think we should be up to!

The Labs \ Source Viewer \ SSCLI \ System.Reflection.Emit \ PackingSize

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. namespace System.Reflection.Emit
  16. {
  17.    
  18.     using System;
  19.     using System.Runtime.Remoting.Activation;
  20.     using System.Reflection;
  21.     using System.Security;
  22.     using System.Security.Permissions;
  23.     using System.Diagnostics;
  24.     using System.Runtime.InteropServices;
  25.     using System.Runtime.CompilerServices;
  26.     using ArrayList = System.Collections.ArrayList;
  27.     using CultureInfo = System.Globalization.CultureInfo;
  28.     using System.Threading;
  29.    
  30.    
  31.     [Serializable()]
  32.     [System.Runtime.InteropServices.ComVisible(true)]
  33.     public enum PackingSize
  34.     {
  35.         Unspecified = 0,
  36.         Size1 = 1,
  37.         Size2 = 2,
  38.         Size4 = 4,
  39.         Size8 = 8,
  40.         Size16 = 16,
  41.         Size32 = 32,
  42.         Size64 = 64,
  43.         Size128 = 128
  44.     }
  45.    
  46.    
  47.     [HostProtection(MayLeakOnAbort = true)]
  48.     [ClassInterface(ClassInterfaceType.None)]
  49.     [ComDefaultInterface(typeof(_TypeBuilder))]
  50.     [System.Runtime.InteropServices.ComVisible(true)]
  51.     public sealed class TypeBuilder : Type, _TypeBuilder
  52.     {
  53.         #region Declarations
  54.         internal class CustAttr
  55.         {
  56.             private ConstructorInfo m_con;
  57.             private byte[] m_binaryAttribute;
  58.             private CustomAttributeBuilder m_customBuilder;
  59.            
  60.             public CustAttr(ConstructorInfo con, byte[] binaryAttribute)
  61.             {
  62.                 if (con == null)
  63.                     throw new ArgumentNullException("con");
  64.                
  65.                 if (binaryAttribute == null)
  66.                     throw new ArgumentNullException("binaryAttribute");
  67.                
  68.                 m_con = con;
  69.                 m_binaryAttribute = binaryAttribute;
  70.             }
  71.            
  72.             public CustAttr(CustomAttributeBuilder customBuilder)
  73.             {
  74.                 if (customBuilder == null)
  75.                     throw new ArgumentNullException("customBuilder");
  76.                
  77.                 m_customBuilder = customBuilder;
  78.             }
  79.            
  80.             public void Bake(ModuleBuilder module, int token)
  81.             {
  82.                 if (m_customBuilder == null) {
  83.                     TypeBuilder.InternalCreateCustomAttribute(token, module.GetConstructorToken(m_con).Token, m_binaryAttribute, module, false);
  84.                 }
  85.                 else {
  86.                     m_customBuilder.CreateCustomAttribute(module, token);
  87.                 }
  88.             }
  89.         }
  90.         #endregion
  91.        
  92.         #region Public Static Methods
  93.         public static MethodInfo GetMethod(Type type, MethodInfo method)
  94.         {
  95.             if (!(type is TypeBuilder) && !(type is TypeBuilderInstantiation))
  96.                 throw new ArgumentException(Environment.GetResourceString("Argument_MustBeTypeBuilder"));
  97.            
  98.             // The following checks establishes invariants that more simply put require type to be generic and
  99.             // method to be a generic method definition declared on the generic type definition of type.
  100.             // To create generic method G<Foo>.M<Bar> these invariants require that G<Foo>.M<S> be created by calling
  101.             // this function followed by MakeGenericMethod on the resulting MethodInfo to finally get G<Foo>.M<Bar>.
  102.             // We could also allow G<T>.M<Bar> to be created before G<Foo>.M<Bar> (BindGenParm followed by this method)
  103.             // if we wanted to but that just complicates things so these checks are designed to prevent that scenario.
  104.            
  105.             if (method.IsGenericMethod && !method.IsGenericMethodDefinition)
  106.                 throw new ArgumentException(Environment.GetResourceString("Argument_NeedGenericMethodDefinition"), "method");
  107.            
  108.             if (method.DeclaringType == null || !method.DeclaringType.IsGenericTypeDefinition)
  109.                 throw new ArgumentException(Environment.GetResourceString("Argument_MethodNeedGenericDeclaringType"), "method");
  110.            
  111.             if (type.GetGenericTypeDefinition() != method.DeclaringType)
  112.                 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidMethodDeclaringType"), "type");
  113.            
  114.             // The following converts from Type or TypeBuilder of G<T> to TypeBuilderInstantiation G<T>. These types
  115.             // both logically represent the same thing. The runtime displays a similar convention by having
  116.             // G<M>.M() be encoded by a typeSpec whose parent is the typeDef for G<M> and whose instantiation is also G<M>.
  117.             if (type.IsGenericTypeDefinition)
  118.                 type = type.MakeGenericType(type.GetGenericArguments());
  119.            
  120.             if (!(type is TypeBuilderInstantiation))
  121.                 throw new ArgumentException(Environment.GetResourceString("Argument_NeedNonGenericType"), "type");
  122.            
  123.             return MethodOnTypeBuilderInstantiation.GetMethod(method, type as TypeBuilderInstantiation);
  124.         }
  125.         public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor)
  126.         {
  127.             if (!(type is TypeBuilder) && !(type is TypeBuilderInstantiation))
  128.                 throw new ArgumentException(Environment.GetResourceString("Argument_MustBeTypeBuilder"));
  129.            
  130.             if (!constructor.DeclaringType.IsGenericTypeDefinition)
  131.                 throw new ArgumentException(Environment.GetResourceString("Argument_ConstructorNeedGenericDeclaringType"), "constructor");
  132.            
  133.             if (!(type is TypeBuilderInstantiation))
  134.                 throw new ArgumentException(Environment.GetResourceString("Argument_NeedNonGenericType"), "type");
  135.            
  136.             // TypeBuilder G<T> ==> TypeBuilderInstantiation G<T>
  137.             if (type is TypeBuilder && type.IsGenericTypeDefinition)
  138.                 type = type.MakeGenericType(type.GetGenericArguments());
  139.            
  140.             if (type.GetGenericTypeDefinition() != constructor.DeclaringType)
  141.                 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidConstructorDeclaringType"), "type");
  142.            
  143.             return ConstructorOnTypeBuilderInstantiation.GetConstructor(constructor, type as TypeBuilderInstantiation);
  144.         }
  145.         public static FieldInfo GetField(Type type, FieldInfo field)
  146.         {
  147.             if (!(type is TypeBuilder) && !(type is TypeBuilderInstantiation))
  148.                 throw new ArgumentException(Environment.GetResourceString("Argument_MustBeTypeBuilder"));
  149.            
  150.             if (!field.DeclaringType.IsGenericTypeDefinition)
  151.                 throw new ArgumentException(Environment.GetResourceString("Argument_FieldNeedGenericDeclaringType"), "field");
  152.            
  153.             if (!(type is TypeBuilderInstantiation))
  154.                 throw new ArgumentException(Environment.GetResourceString("Argument_NeedNonGenericType"), "type");
  155.            
  156.             // TypeBuilder G<T> ==> TypeBuilderInstantiation G<T>
  157.             if (type is TypeBuilder && type.IsGenericTypeDefinition)
  158.                 type = type.MakeGenericType(type.GetGenericArguments());
  159.            
  160.             if (type.GetGenericTypeDefinition() != field.DeclaringType)
  161.                 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFieldDeclaringType"), "type");
  162.            
  163.             return FieldOnTypeBuilderInstantiation.GetField(field, type as TypeBuilderInstantiation);
  164.         }
  165.         #endregion
  166.        
  167.         #region Public Const
  168.         public const int UnspecifiedTypeSize = 0;
  169.         #endregion
  170.        
  171.         #region Private Static FCalls
  172.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  173.         private static extern void InternalSetParentType(int tdTypeDef, int tkParent, Module module);
  174.        
  175.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  176.         private static extern void InternalAddInterfaceImpl(int tdTypeDef, int tkInterface, Module module);
  177.         #endregion
  178.        
  179.         #region Internal Static FCalls
  180.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  181.         static internal extern int InternalDefineMethod(int handle, string name, byte[] signature, int sigLength, MethodAttributes attributes, Module module);
  182.        
  183.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  184.         static internal extern int InternalDefineMethodSpec(int handle, byte[] signature, int sigLength, Module module);
  185.        
  186.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  187.         static internal extern int InternalDefineField(int handle, string name, byte[] signature, int sigLength, FieldAttributes attributes, Module module);
  188.        
  189.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  190.         static internal extern void InternalSetMethodIL(int methodHandle, bool isInitLocals, byte[] body, byte[] LocalSig, int sigLength, int maxStackSize, int numExceptions, __ExceptionInstance[] exceptions, int[] tokenFixups, int[] rvaFixups,
  191.         Module module);
  192.        
  193.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  194.         static internal extern void InternalCreateCustomAttribute(int tkAssociate, int tkConstructor, byte[] attr, Module module, bool toDisk, bool updateCompilerFlags);
  195.        
  196.         static internal void InternalCreateCustomAttribute(int tkAssociate, int tkConstructor, byte[] attr, Module module, bool toDisk)
  197.         {
  198.             byte[] localAttr = null;
  199.            
  200.             if (attr != null) {
  201.                 localAttr = new byte[attr.Length];
  202.                 Array.Copy(attr, localAttr, attr.Length);
  203.             }
  204.             TypeBuilder.InternalCreateCustomAttribute(tkAssociate, tkConstructor, localAttr, module, toDisk, false);
  205.         }
  206.        
  207.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  208.         static internal extern void InternalSetPInvokeData(Module module, string DllName, string name, int token, int linkType, int linkFlags);
  209.        
  210.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  211.         static internal extern int InternalDefineProperty(Module module, int handle, string name, int attributes, byte[] signature, int sigLength, int notifyChanging, int notifyChanged);
  212.        
  213.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  214.         static internal extern int InternalDefineEvent(Module module, int handle, string name, int attributes, int tkEventType);
  215.        
  216.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  217.         static internal extern void InternalDefineMethodSemantics(Module module, int tkAssociation, MethodSemanticsAttributes semantics, int tkMethod);
  218.        
  219.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  220.         static internal extern void InternalDefineMethodImpl(ModuleBuilder module, int tkType, int tkBody, int tkDecl);
  221.        
  222.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  223.         static internal extern void InternalSetMethodImpl(Module module, int tkMethod, MethodImplAttributes MethodImplAttributes);
  224.        
  225.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  226.         static internal extern int InternalSetParamInfo(Module module, int tkMethod, int iSequence, ParameterAttributes iParamAttributes, string strParamName);
  227.        
  228.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  229.         static internal extern int InternalGetTokenFromSig(Module module, byte[] signature, int sigLength);
  230.        
  231.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  232.         static internal extern void InternalSetFieldOffset(Module module, int fdToken, int iOffset);
  233.        
  234.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  235.         static internal extern void InternalSetClassLayout(Module module, int tdToken, PackingSize iPackingSize, int iTypeSize);
  236.        
  237.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  238.         static internal extern void InternalSetMarshalInfo(Module module, int tk, byte[] ubMarshal, int ubSize);
  239.        
  240.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  241.         static internal extern void InternalSetConstantValue(Module module, int tk, ref Variant var);
  242.        
  243.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  244.         static internal extern void InternalAddDeclarativeSecurity(Module module, int parent, SecurityAction action, byte[] blob);
  245.         #endregion
  246.        
  247.         #region Internal\Private Static Members
  248.         private static bool IsPublicComType(Type type)
  249.         {
  250.             // Internal Helper to determine if a type should be added to ComType table.
  251.             // A top level type should be added if it is Public.
  252.             // A nested type should be added if the top most enclosing type is Public
  253.             // and all the enclosing types are NestedPublic
  254.            
  255.             Type enclosingType = type.DeclaringType;
  256.             if (enclosingType != null) {
  257.                 if (IsPublicComType(enclosingType)) {
  258.                     if ((type.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
  259.                         return true;
  260.                     }
  261.                 }
  262.             }
  263.             else {
  264.                 if ((type.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public) {
  265.                     return true;
  266.                 }
  267.             }
  268.            
  269.             return false;
  270.         }
  271.        
  272.         static internal bool IsTypeEqual(Type t1, Type t2)
  273.         {
  274.             // Maybe we are lucky that they are equal in the first place
  275.             if (t1 == t2)
  276.                 return true;
  277.             TypeBuilder tb1 = null;
  278.             TypeBuilder tb2 = null;
  279.             Type runtimeType1 = null;
  280.             Type runtimeType2 = null;
  281.            
  282.             // set up the runtimeType and TypeBuilder type corresponding to t1 and t2
  283.             if (t1 is TypeBuilder) {
  284.                 tb1 = (TypeBuilder)t1;
  285.                 // This will be null if it is not baked.
  286.                 runtimeType1 = tb1.m_runtimeType;
  287.             }
  288.             else {
  289.                 runtimeType1 = t1;
  290.             }
  291.            
  292.             if (t2 is TypeBuilder) {
  293.                 tb2 = (TypeBuilder)t2;
  294.                 // This will be null if it is not baked.
  295.                 runtimeType2 = tb2.m_runtimeType;
  296.             }
  297.             else {
  298.                 runtimeType2 = t2;
  299.             }
  300.            
  301.             // If the type builder view is eqaul then it is equal
  302.             if (tb1 != null && tb2 != null && tb1 == tb2)
  303.                 return true;
  304.            
  305.             // if the runtimetype view is eqaul than it is equal
  306.             if (runtimeType1 != null && runtimeType2 != null && runtimeType1 == runtimeType2)
  307.                 return true;
  308.            
  309.             return false;
  310.         }
  311.        
  312.         static internal void SetConstantValue(Module module, int tk, Type destType, object value)
  313.         {
  314.             // This is a helper function that is used by ParameterBuilder, PropertyBuilder,
  315.             // and FieldBuilder to validate a default value and save it in the meta-data.
  316.            
  317.             if (value == null) {
  318.                 if (destType.IsValueType)
  319.                     throw new ArgumentException(Environment.GetResourceString("Argument_ConstantNull"));
  320.             }
  321.             else {
  322.                 // client is going to set non-null constant value
  323.                 Type type = value.GetType();
  324.                
  325.                 // The default value on the enum typed field/parameter is the underlying type.
  326.                 if (destType.IsEnum == false) {
  327.                     if (destType != type)
  328.                         throw new ArgumentException(Environment.GetResourceString("Argument_ConstantDoesntMatch"));
  329.                    
  330.                     switch (Type.GetTypeCode(type)) {
  331.                         case TypeCode.Boolean:
  332.                         case TypeCode.Char:
  333.                         case TypeCode.SByte:
  334.                         case TypeCode.Byte:
  335.                         case TypeCode.Int16:
  336.                         case TypeCode.UInt16:
  337.                         case TypeCode.Int32:
  338.                         case TypeCode.UInt32:
  339.                         case TypeCode.Int64:
  340.                         case TypeCode.UInt64:
  341.                         case TypeCode.Single:
  342.                         case TypeCode.Double:
  343.                         case TypeCode.Decimal:
  344.                         case TypeCode.String:
  345.                             break;
  346.                         default:
  347.                            
  348.                             {
  349.                                 if (type != typeof(System.DateTime))
  350.                                     throw new ArgumentException(Environment.GetResourceString("Argument_ConstantNotSupported"));
  351.                                 break;
  352.                             }
  353.                             break;
  354.                        
  355.                     }
  356.                 }
  357.                 else {
  358.                     // The constant value supplied should match the underlying type of the enum
  359.                     if (destType.UnderlyingSystemType != type)
  360.                         throw new ArgumentException(Environment.GetResourceString("Argument_ConstantDoesntMatch"));
  361.                 }
  362.             }
  363.            
  364.             // pass the variant by reference to avoid woes with passing valuetypes into FCALLs
  365.             Variant var = new Variant(value);
  366.            
  367.             InternalSetConstantValue(module, tk, ref var);
  368.         }
  369.        
  370.         #endregion
  371.        
  372.         #region Private Data Members
  373.         internal ArrayList m_ca;
  374.         internal MethodBuilder m_currentMethod;
  375.         private TypeToken m_tdType;
  376.         private ModuleBuilder m_module;
  377.         internal string m_strName;
  378.         private string m_strNameSpace;
  379.         private string m_strFullQualName;
  380.         private Type m_typeParent;
  381.         private Type[] m_typeInterfaces;
  382.         internal TypeAttributes m_iAttr;
  383.         internal GenericParameterAttributes m_genParamAttributes;
  384.         internal ArrayList m_listMethods;
  385.         private int m_constructorCount;
  386.         private int m_iTypeSize;
  387.         private PackingSize m_iPackingSize;
  388.         private TypeBuilder m_DeclaringType;
  389.         private Type m_underlyingSystemType;
  390.         // set when user calls UnderlyingSystemType on TypeBuilder if TypeBuilder is an Enum
  391.         internal bool m_isHiddenGlobalType;
  392.         internal bool m_isHiddenType;
  393.         internal bool m_hasBeenCreated;
  394.         internal RuntimeType m_runtimeType;
  395.        
  396.         private int m_genParamPos;
  397.         private GenericTypeParameterBuilder[] m_inst;
  398.         private bool m_bIsGenParam;
  399.         private bool m_bIsGenTypeDef;
  400.         private MethodBuilder m_declMeth;
  401.         private TypeBuilder m_genTypeDef;
  402.         #endregion
  403.        
  404.         #region Constructor
  405.         private TypeBuilder(TypeBuilder genTypeDef, GenericTypeParameterBuilder[] inst)
  406.         {
  407.             m_genTypeDef = genTypeDef;
  408.             m_DeclaringType = genTypeDef.m_DeclaringType;
  409.             m_typeParent = genTypeDef.m_typeParent;
  410.             m_runtimeType = genTypeDef.m_runtimeType;
  411.             m_tdType = genTypeDef.m_tdType;
  412.             m_strName = genTypeDef.m_strName;
  413.             m_bIsGenParam = false;
  414.             m_bIsGenTypeDef = false;
  415.             m_module = genTypeDef.m_module;
  416.             m_inst = inst;
  417.             m_hasBeenCreated = true;
  418.         }
  419.        
  420.         internal TypeBuilder(string szName, int genParamPos, MethodBuilder declMeth)
  421.         {
  422.             m_declMeth = declMeth;
  423.             m_DeclaringType = (TypeBuilder)m_declMeth.DeclaringType;
  424.             m_module = (ModuleBuilder)declMeth.Module;
  425.             InitAsGenericParam(szName, genParamPos);
  426.         }
  427.        
  428.         private TypeBuilder(string szName, int genParamPos, TypeBuilder declType)
  429.         {
  430.             m_DeclaringType = declType;
  431.             m_module = (ModuleBuilder)declType.Module;
  432.             InitAsGenericParam(szName, genParamPos);
  433.         }
  434.        
  435.         private void InitAsGenericParam(string szName, int genParamPos)
  436.         {
  437.             m_strName = szName;
  438.             m_genParamPos = genParamPos;
  439.             m_bIsGenParam = true;
  440.             m_bIsGenTypeDef = false;
  441.             m_typeInterfaces = new Type[0];
  442.         }
  443.        
  444.         internal TypeBuilder(string name, TypeAttributes attr, Type parent, Module module, PackingSize iPackingSize, int iTypeSize, TypeBuilder enclosingType)
  445.         {
  446.             Init(name, attr, parent, null, module, iPackingSize, iTypeSize, enclosingType);
  447.         }
  448.        
  449.         internal TypeBuilder(string name, TypeAttributes attr, Type parent, Type[] interfaces, Module module, PackingSize iPackingSize, TypeBuilder enclosingType)
  450.         {
  451.             Init(name, attr, parent, interfaces, module, iPackingSize, UnspecifiedTypeSize, enclosingType);
  452.         }
  453.        
  454.         internal TypeBuilder(ModuleBuilder module)
  455.         {
  456.             m_tdType = new TypeToken(SignatureHelper.mdtTypeDef);
  457.             m_isHiddenGlobalType = true;
  458.             m_module = (ModuleBuilder)module;
  459.             m_listMethods = new ArrayList();
  460.         }
  461.        
  462.         private void Init(string fullname, TypeAttributes attr, Type parent, Type[] interfaces, Module module, PackingSize iPackingSize, int iTypeSize, TypeBuilder enclosingType)
  463.         {
  464.             int i;
  465.             int[] interfaceTokens;
  466.             m_bIsGenTypeDef = false;
  467.             interfaceTokens = null;
  468.             m_bIsGenParam = false;
  469.             m_hasBeenCreated = false;
  470.             m_runtimeType = null;
  471.             m_isHiddenGlobalType = false;
  472.             m_isHiddenType = false;
  473.             m_module = (ModuleBuilder)module;
  474.             m_DeclaringType = enclosingType;
  475.             Assembly containingAssem = m_module.Assembly;
  476.             m_underlyingSystemType = null;
  477.             // used when client use TypeBuilder to define Enum
  478.             if (fullname == null)
  479.                 throw new ArgumentNullException("fullname");
  480.            
  481.             if (fullname.Length == 0)
  482.                 throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "fullname");
  483.            
  484.             if (fullname[0] == '\0')
  485.                 throw new ArgumentException(Environment.GetResourceString("Argument_IllegalName"), "fullname");
  486.            
  487.            
  488.             if (fullname.Length > 1023)
  489.                 throw new ArgumentException(Environment.GetResourceString("Argument_TypeNameTooLong"), "fullname");
  490.            
  491.             // cannot have two types within the same assembly of the same name
  492.             containingAssem.m_assemblyData.CheckTypeNameConflict(fullname, enclosingType);
  493.            
  494.             if (enclosingType != null) {
  495.                 // Nested Type should have nested attribute set.
  496.                 // If we are renumbering TypeAttributes' bit, we need to change the logic here.
  497.                 if (((attr & TypeAttributes.VisibilityMask) == TypeAttributes.Public) || ((attr & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
  498.                     throw new ArgumentException(Environment.GetResourceString("Argument_BadNestedTypeFlags"), "attr");
  499.             }
  500.            
  501.             if (interfaces != null) {
  502.                 for (i = 0; i < interfaces.Length; i++) {
  503.                     if (interfaces[i] == null) {
  504.                         // cannot contain null in the interface list
  505.                         throw new ArgumentNullException("interfaces");
  506.                     }
  507.                 }
  508.                 interfaceTokens = new int[interfaces.Length];
  509.                 for (i = 0; i < interfaces.Length; i++) {
  510.                     interfaceTokens[i] = m_module.GetTypeToken(interfaces[i]).Token;
  511.                 }
  512.             }
  513.            
  514.             int iLast = fullname.LastIndexOf('.');
  515.             if (iLast == -1 || iLast == 0) {
  516.                 // no name space
  517.                 m_strNameSpace = String.Empty;
  518.                 m_strName = fullname;
  519.             }
  520.             else {
  521.                 // split the name space
  522.                 m_strNameSpace = fullname.Substring(0, iLast);
  523.                 m_strName = fullname.Substring(iLast + 1);
  524.             }
  525.            
  526.             VerifyTypeAttributes(attr);
  527.            
  528.             m_iAttr = attr;
  529.            
  530.             SetParent(parent);
  531.            
  532.             m_listMethods = new ArrayList();
  533.            
  534.             SetInterfaces(interfaces);
  535.            
  536.             m_constructorCount = 0;
  537.            
  538.             int tkParent = 0;
  539.             if (m_typeParent != null)
  540.                 tkParent = m_module.GetTypeToken(m_typeParent).Token;
  541.            
  542.             int tkEnclosingType = 0;
  543.             if (enclosingType != null) {
  544.                 tkEnclosingType = enclosingType.m_tdType.Token;
  545.             }
  546.            
  547.             m_tdType = new TypeToken(InternalDefineClass(fullname, tkParent, interfaceTokens, m_iAttr, m_module, Guid.Empty, tkEnclosingType, 0));
  548.            
  549.             m_iPackingSize = iPackingSize;
  550.             m_iTypeSize = iTypeSize;
  551.             if ((m_iPackingSize != 0) || (m_iTypeSize != 0))
  552.                 InternalSetClassLayout(Module, m_tdType.Token, m_iPackingSize, m_iTypeSize);
  553.            
  554.             // If the type is public and it is contained in a assemblyBuilder,
  555.             // update the public COMType list.
  556.             if (IsPublicComType(this)) {
  557.                 if (containingAssem is AssemblyBuilder) {
  558.                     AssemblyBuilder assemBuilder = (AssemblyBuilder)containingAssem;
  559.                     if (assemBuilder.IsPersistable() && m_module.IsTransient() == false) {
  560.                         assemBuilder.m_assemblyData.AddPublicComType(this);
  561.                     }
  562.                 }
  563.             }
  564.         }
  565.        
  566.         #endregion
  567.        
  568.         #region Private Members
  569.         private MethodBuilder DefinePInvokeMethodHelper(string name, string dllName, string importName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers,
  570.         Type[][] parameterTypeOptionalCustomModifiers, CallingConvention nativeCallConv, CharSet nativeCharSet)
  571.         {
  572.             CheckContext(returnType);
  573.             CheckContext(returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes);
  574.             CheckContext(parameterTypeRequiredCustomModifiers);
  575.             CheckContext(parameterTypeOptionalCustomModifiers);
  576.            
  577.             if (Module.Assembly.m_assemblyData.m_isSynchronized) {
  578.                 lock (Module.Assembly.m_assemblyData) {
  579.                     return DefinePInvokeMethodHelperNoLock(name, dllName, importName, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers,
  580.                     parameterTypeOptionalCustomModifiers, nativeCallConv, nativeCharSet);
  581.                 }
  582.             }
  583.             else {
  584.                 return DefinePInvokeMethodHelperNoLock(name, dllName, importName, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers,
  585.                 parameterTypeOptionalCustomModifiers, nativeCallConv, nativeCharSet);
  586.             }
  587.         }
  588.        
  589.         private MethodBuilder DefinePInvokeMethodHelperNoLock(string name, string dllName, string importName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers,
  590.         Type[][] parameterTypeOptionalCustomModifiers, CallingConvention nativeCallConv, CharSet nativeCharSet)
  591.         {
  592.            
  593.             ThrowIfCreated();
  594.            
  595.             if (name == null)
  596.                 throw new ArgumentNullException("name");
  597.            
  598.             if (name.Length == 0)
  599.                 throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "name");
  600.            
  601.             if (dllName == null)
  602.                 throw new ArgumentNullException("dllName");
  603.            
  604.             if (dllName.Length == 0)
  605.                 throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "dllName");
  606.            
  607.             if (importName == null)
  608.                 throw new ArgumentNullException("importName");
  609.            
  610.             if (importName.Length == 0)
  611.                 throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "importName");
  612.            
  613.             if ((m_iAttr & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface)
  614.                 throw new ArgumentException(Environment.GetResourceString("Argument_BadPInvokeOnInterface"));
  615.            
  616.             if ((attributes & MethodAttributes.Abstract) != 0)
  617.                 throw new ArgumentException(Environment.GetResourceString("Argument_BadPInvokeMethod"));
  618.            
  619.            
  620.            
  621.            
  622.            
  623.             attributes = attributes | MethodAttributes.PinvokeImpl;
  624.             MethodBuilder method = new MethodBuilder(name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, m_module,
  625.             this, false);
  626.            
  627.             int sigLength;
  628.             byte[] sigBytes = method.GetMethodSignature().InternalGetSignature(out sigLength);
  629.            
  630.             if (m_listMethods.Contains(method)) {
  631.                 throw new ArgumentException(Environment.GetResourceString("Argument_MethodRedefined"));
  632.             }
  633.             m_listMethods.Add(method);
  634.            
  635.             MethodToken token = method.GetToken();
  636.            
  637.             int linkFlags = 0;
  638.             switch (nativeCallConv) {
  639.                 case CallingConvention.Winapi:
  640.                     linkFlags = (int)PInvokeMap.CallConvWinapi;
  641.                     break;
  642.                 case CallingConvention.Cdecl:
  643.                     linkFlags = (int)PInvokeMap.CallConvCdecl;
  644.                     break;
  645.                 case CallingConvention.StdCall:
  646.                     linkFlags = (int)PInvokeMap.CallConvStdcall;
  647.                     break;
  648.                 case CallingConvention.ThisCall:
  649.                     linkFlags = (int)PInvokeMap.CallConvThiscall;
  650.                     break;
  651.                 case CallingConvention.FastCall:
  652.                     linkFlags = (int)PInvokeMap.CallConvFastcall;
  653.                     break;
  654.             }
  655.             switch (nativeCharSet) {
  656.                 case CharSet.None:
  657.                     linkFlags |= (int)PInvokeMap.CharSetNotSpec;
  658.                     break;
  659.                 case CharSet.Ansi:
  660.                     linkFlags |= (int)PInvokeMap.CharSetAnsi;
  661.                     break;
  662.                 case CharSet.Unicode:
  663.                     linkFlags |= (int)PInvokeMap.CharSetUnicode;
  664.                     break;
  665.                 case CharSet.Auto:
  666.                     linkFlags |= (int)PInvokeMap.CharSetAuto;
  667.                     break;
  668.             }
  669.            
  670.             InternalSetPInvokeData(m_module, dllName, importName, token.Token, 0, linkFlags);
  671.             method.SetToken(token);
  672.            
  673.             return method;
  674.         }
  675.        
  676.         private FieldBuilder DefineDataHelper(string name, byte[] data, int size, FieldAttributes attributes)
  677.         {
  678.             string strValueClassName;
  679.             TypeBuilder valueClassType;
  680.             FieldBuilder fdBuilder;
  681.             TypeAttributes typeAttributes;
  682.            
  683.             if (name == nu