public override bool Equals(object obj)
        {
            if ((obj == null) || !(obj is __ExceptionInstance))
            {
                return(false);
            }
            __ExceptionInstance instance = (__ExceptionInstance)obj;

            return(((((instance.m_exceptionClass == this.m_exceptionClass) && (instance.m_startAddress == this.m_startAddress)) && ((instance.m_endAddress == this.m_endAddress) && (instance.m_filterAddress == this.m_filterAddress))) && (instance.m_handleAddress == this.m_handleAddress)) && (instance.m_handleEndAddress == this.m_handleEndAddress));
        }
 // Satisfy JVC's value class requirements
 public override bool Equals(Object obj)
 {
     if (obj != null && (obj is __ExceptionInstance))
     {
         __ExceptionInstance that = (__ExceptionInstance)obj;
         return(that.m_exceptionClass == m_exceptionClass &&
                that.m_startAddress == m_startAddress && that.m_endAddress == m_endAddress &&
                that.m_filterAddress == m_filterAddress &&
                that.m_handleAddress == m_handleAddress && that.m_handleEndAddress == m_handleEndAddress);
     }
     else
     {
         return(false);
     }
 }
        /**********************************************
        * Sets the IL of the method.  An ILGenerator is passed as an argument and the method
        * queries this instance to get all of the information which it needs.
        * @param il An ILGenerator with some il defined.
        * @exception ArgumentException if <EM>il</EM> is null.
        **********************************************/
        internal void CreateMethodBodyHelper(ILGenerator il)
        {
            __ExceptionInfo[] excp;
            int counter = 0;

            int[]         filterAddrs;
            int[]         catchAddrs;
            int[]         catchEndAddrs;
            Type[]        catchClass;
            int[]         type;
            int           numCatch;
            int           start, end;
            ModuleBuilder dynMod = (ModuleBuilder)m_module;

            m_containingType.ThrowIfCreated();

            if (m_bIsBaked)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MethodHasBody"));
            }

            if (il == null)
            {
                throw new ArgumentNullException("il");
            }

            if (il.m_methodBuilder != this && il.m_methodBuilder != null)
            {
                // you don't need to call CreateMethodBody when you get your ILGenerator
                // through MethodBuilder::GetILGenerator.
                //

                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_BadILGeneratorUsage"));
            }
            if ((m_dwMethodImplFlags & MethodImplAttributes.PreserveSig) != 0 ||
                (m_dwMethodImplFlags & MethodImplAttributes.CodeTypeMask) != MethodImplAttributes.IL ||
                (m_dwMethodImplFlags & MethodImplAttributes.Unmanaged) != 0 ||
                (m_iAttributes & MethodAttributes.PinvokeImpl) != 0)
            {
                // cannot attach method body if methodimpl is marked not marked as managed IL
                // @todo: what error should we throw here?
                //
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ShouldNotHaveMethodBody"));
            }

            if (il.m_ScopeTree.m_iOpenScopeCount != 0)
            {
                // There are still unclosed local scope
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_OpenLocalVariableScope"));
            }


            m_ubBody = il.BakeByteArray();

            m_RVAFixups       = il.GetRVAFixups();
            mm_mdMethodFixups = il.GetTokenFixups();

            //Okay, now the fun part.  Calculate all of the exceptions.
            excp            = il.GetExceptions();
            m_numExceptions = CalculateNumberOfExceptions(excp);
            if (m_numExceptions > 0)
            {
                m_exceptions = new __ExceptionInstance[m_numExceptions];

                for (int i = 0; i < excp.Length; i++)
                {
                    filterAddrs   = excp[i].GetFilterAddresses();
                    catchAddrs    = excp[i].GetCatchAddresses();
                    catchEndAddrs = excp[i].GetCatchEndAddresses();
                    catchClass    = excp[i].GetCatchClass();


                    // track the reference of the catch class
                    for (int j = 0; j < catchClass.Length; j++)
                    {
                        if (catchClass[j] != null)
                        {
                            dynMod.GetTypeToken(catchClass[j]);
                        }
                    }

                    numCatch = excp[i].GetNumberOfCatches();
                    start    = excp[i].GetStartAddress();
                    end      = excp[i].GetEndAddress();
                    type     = excp[i].GetExceptionTypes();
                    for (int j = 0; j < numCatch; j++)
                    {
                        int tkExceptionClass = 0;
                        if (catchClass[j] != null)
                        {
                            tkExceptionClass = dynMod.GetTypeToken(catchClass[j]).Token;
                        }
                        switch (type[j])
                        {
                        case __ExceptionInfo.None:
                        case __ExceptionInfo.Fault:
                        case __ExceptionInfo.Filter:
                            m_exceptions[counter++] = new __ExceptionInstance(start, end, filterAddrs[j], catchAddrs[j], catchEndAddrs[j], type[j], tkExceptionClass);
                            break;

                        case __ExceptionInfo.Finally:
                            m_exceptions[counter++] = new __ExceptionInstance(start, excp[i].GetFinallyEndAddress(), filterAddrs[j], catchAddrs[j], catchEndAddrs[j], type[j], tkExceptionClass);
                            break;
                        }
                    }
                }
            }


            m_bIsBaked = true;

            if (dynMod.GetSymWriter() != null)
            {
                // set the debugging information such as scope and line number
                // if it is in a debug module
                //
                SymbolToken   tk        = new SymbolToken(m_mdMethod.Token);
                ISymbolWriter symWriter = dynMod.GetSymWriter();

                // call OpenMethod to make this method the current method
                symWriter.OpenMethod(tk);

                // call OpenScope because OpenMethod no longer implicitly creating
                // the top-levelsmethod scope
                //
                symWriter.OpenScope(0);
                if (m_localSymInfo != null)
                {
                    m_localSymInfo.EmitLocalSymInfo(symWriter);
                }
                il.m_ScopeTree.EmitScopeTree(symWriter);
                il.m_LineNumberInfo.EmitLineNumberInfo(symWriter);
                symWriter.CloseScope(il.m_length);
                symWriter.CloseMethod();
            }
        }
        internal void CreateMethodBodyHelper(ILGenerator il)
        {
            // Sets the IL of the method.  An ILGenerator is passed as an argument and the method
            // queries this instance to get all of the information which it needs.

            __ExceptionInfo[]   excp;
            int                 counter=0;
            int[]               filterAddrs;
            int[]               catchAddrs;
            int[]               catchEndAddrs;
            Type[]              catchClass;
            int[]               type;
            int                 numCatch;
            int                 start, end;
            ModuleBuilder       dynMod = (ModuleBuilder) m_module;

            m_containingType.ThrowIfCreated();

            if (m_bIsBaked)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MethodHasBody"));
            }

            if (il==null)
            {
                throw new ArgumentNullException("il");
            }

            if (il.m_methodBuilder != this && il.m_methodBuilder != null)
            {
                // you don't need to call CreateMethodBody when you get your ILGenerator
                // through MethodBuilder::GetILGenerator.
                //

                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_BadILGeneratorUsage"));
            }
            
            ThrowIfShouldNotHaveBody();

            if (il.m_ScopeTree.m_iOpenScopeCount != 0)
            {
                // There are still unclosed local scope
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_OpenLocalVariableScope"));
            }


            m_ubBody = il.BakeByteArray();

            m_RVAFixups = il.GetRVAFixups();
            m_mdMethodFixups = il.GetTokenFixups();

            //Okay, now the fun part.  Calculate all of the exceptions.
            excp = il.GetExceptions();
            m_numExceptions = CalculateNumberOfExceptions(excp);
            if (m_numExceptions>0)
            {

                m_exceptions = new __ExceptionInstance[m_numExceptions];

                for (int i=0; i<excp.Length; i++) {
                    filterAddrs = excp[i].GetFilterAddresses();
                    catchAddrs = excp[i].GetCatchAddresses();
                    catchEndAddrs = excp[i].GetCatchEndAddresses();
                    catchClass = excp[i].GetCatchClass();


                    // track the reference of the catch class
                    for (int j=0; j < catchClass.Length; j++)
                    {
                        if (catchClass[j] != null)
                            dynMod.GetTypeToken(catchClass[j]);
                    }

                    numCatch = excp[i].GetNumberOfCatches();
                    start = excp[i].GetStartAddress();
                    end = excp[i].GetEndAddress();
                    type = excp[i].GetExceptionTypes();
                    for (int j=0; j<numCatch; j++) {
                        int tkExceptionClass = 0;
                        if (catchClass[j] != null)
                            tkExceptionClass = dynMod.GetTypeToken(catchClass[j]).Token;
                        switch (type[j]) {
                        case __ExceptionInfo.None:
                        case __ExceptionInfo.Fault:
                        case __ExceptionInfo.Filter:
                            m_exceptions[counter++]=new __ExceptionInstance(start, end, filterAddrs[j], catchAddrs[j], catchEndAddrs[j], type[j], tkExceptionClass);
                            break;

                        case __ExceptionInfo.Finally:
                            m_exceptions[counter++]=new __ExceptionInstance(start, excp[i].GetFinallyEndAddress(), filterAddrs[j], catchAddrs[j], catchEndAddrs[j], type[j], tkExceptionClass);
                            break;
                        }
                    }

                }
            }


            m_bIsBaked=true;

            if (dynMod.GetSymWriter() != null)
            {

                // set the debugging information such as scope and line number
                // if it is in a debug module
                //
                SymbolToken  tk = new SymbolToken(MetadataTokenInternal);
                ISymbolWriter symWriter = dynMod.GetSymWriter();

                // call OpenMethod to make this method the current method
                symWriter.OpenMethod(tk);

                // call OpenScope because OpenMethod no longer implicitly creating
                // the top-levelsmethod scope
                //
                symWriter.OpenScope(0);
                
                if (m_symCustomAttrs != null)
                {
                    foreach(SymCustomAttr symCustomAttr in m_symCustomAttrs)
                        dynMod.GetSymWriter().SetSymAttribute(
                        new SymbolToken (MetadataTokenInternal), 
                            symCustomAttr.m_name, 
                            symCustomAttr.m_data);
                }
                
                if (m_localSymInfo != null)
                    m_localSymInfo.EmitLocalSymInfo(symWriter);
                il.m_ScopeTree.EmitScopeTree(symWriter);
                il.m_LineNumberInfo.EmitLineNumberInfo(symWriter);
                symWriter.CloseScope(il.m_length);
                symWriter.CloseMethod();
            }
        }
 private static extern void SetMethodIL(RuntimeModule module, int tk, bool isInitLocals, byte[] body, int bodyLength, byte[] LocalSig, int sigLength, int maxStackSize, __ExceptionInstance[] exceptions, int numExceptions, int[] tokenFixups, int numTokenFixups, int[] rvaFixups, int numRvaFixups);
        private Type CreateTypeNoLock()
        {
            
            if (IsCreated())
                return m_runtimeType;

            ThrowIfGeneric();
            ThrowIfCreated();

            if (m_typeInterfaces == null)
                m_typeInterfaces = new Type[0];

            int[] interfaceTokens = new int[m_typeInterfaces.Length];
            for(int i = 0; i < m_typeInterfaces.Length; i++)
            {
                interfaceTokens[i] = m_module.GetTypeToken(m_typeInterfaces[i]).Token;
            }

            int tkParent = 0;
            if (m_typeParent != null)
                tkParent = m_module.GetTypeToken(m_typeParent).Token;

            if (IsGenericParameter)
            {
                int[] constraints = new int[m_typeInterfaces.Length];

                if (m_typeParent != null)
                {
                    constraints = new int[m_typeInterfaces.Length + 1];
                    constraints[constraints.Length - 1] = tkParent;
                }

                for(int i = 0; i < m_typeInterfaces.Length; i++)
                {
                    constraints[i] = m_module.GetTypeToken(m_typeInterfaces[i]).Token;
                }

                int declMember = m_declMeth == null ? m_DeclaringType.m_tdType.Token : m_declMeth.GetToken().Token;
                m_tdType = new TypeToken(InternalDefineGenParam(
                    m_strName, declMember, m_genParamPos, (int)m_genParamAttributes, constraints, m_module, 0));

                if (m_ca != null)
                {
                    foreach (CustAttr ca in m_ca)
                        ca.Bake(m_module, MetadataTokenInternal);
                }

                m_hasBeenCreated = true;
                return this;
            }
            else
            {
                // Check for global typebuilder
                if (((m_tdType.Token & 0x00FFFFFF) != 0) && ((tkParent & 0x00FFFFFF) != 0))
                    InternalSetParentType(m_tdType.Token, tkParent, m_module);
            
                if (m_inst != null)
                    foreach (Type tb in m_inst)
                        if (tb is GenericTypeParameterBuilder)
                            ((GenericTypeParameterBuilder)tb).m_type.CreateType();
            }

            byte [] body;
            MethodAttributes methodAttrs;
            int maxstack;
                            
            if (!m_isHiddenGlobalType)
            {
                // create a public default constructor if this class has no constructor.
                // except if the type is Interface, ValueType, Enum, or a static class.
                if (m_constructorCount == 0 && ((m_iAttr & TypeAttributes.Interface) == 0) && !IsValueType && ((m_iAttr & (TypeAttributes.Abstract | TypeAttributes.Sealed)) != (TypeAttributes.Abstract | TypeAttributes.Sealed)))
                {
                    DefineDefaultConstructor(MethodAttributes.Public);
                }
            }

            int size = m_listMethods.Count;

            for(int i = 0; i < size; i++)
            {
                MethodBuilder meth =(MethodBuilder)m_listMethods[i];


                if (meth.IsGenericMethodDefinition)
                    meth.GetToken(); // Doubles as "CreateMethod" for MethodBuilder -- analagous to CreateType()

                methodAttrs = meth.Attributes;

                // Any of these flags in the implemenation flags is set, we will not attach the IL method body
                if (((meth.GetMethodImplementationFlags() &(MethodImplAttributes.CodeTypeMask|MethodImplAttributes.PreserveSig|MethodImplAttributes.Unmanaged)) != MethodImplAttributes.IL) ||
                    ((methodAttrs & MethodAttributes.PinvokeImpl) !=(MethodAttributes) 0))
                {
                    continue;
                }

                int sigLength;
                byte[] LocalSig= meth.GetLocalsSignature().InternalGetSignature(out sigLength);

                 
                // Check that they haven't declared an abstract method on a non-abstract class
                if (((methodAttrs & MethodAttributes.Abstract) != 0) &&((m_iAttr & TypeAttributes.Abstract) == 0))
                {
                    throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_BadTypeAttributesNotAbstract"));
                }

                body = meth.GetBody();

                // If this is an abstract method or an interface, we don't need to set the IL.

                if ((methodAttrs & MethodAttributes.Abstract) != 0)
                {
                    // We won't check on Interface because we can have class static initializer on interface.
                    // We will just let EE or validator to catch the problem.

                    //((m_iAttr & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface))

                    if (body != null)
                        throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_BadMethodBody"));
                }
                else if (body == null || body.Length == 0)
                {
                    // If it's not an abstract or an interface, set the IL.
                    if (meth.m_ilGenerator != null)
                    {
                        // we need to bake the method here.
                        meth.CreateMethodBodyHelper(meth.GetILGenerator());
                    }

                    body = meth.GetBody();

                    if ((body == null || body.Length == 0) && !meth.m_canBeRuntimeImpl)
                        throw new InvalidOperationException(
                            String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidOperation_BadEmptyMethodBody"), meth.Name) ); 
                }

                if (meth.m_ilGenerator != null)
                {
                    maxstack = meth.m_ilGenerator.GetMaxStackSize();
                }
                else
                {
                    // this is the case when client provide an array of IL byte stream rather than going through ILGenerator.
                    maxstack = 16;
                }

                __ExceptionInstance[] Exceptions = meth.GetExceptionInstances();
                int[] TokenFixups = meth.GetTokenFixups();
                int[] RVAFixups = meth.GetRVAFixups();


                __ExceptionInstance[] localExceptions = null;
                int[] localTokenFixups = null;
                int[] localRVAFixups = null;
                
                if (Exceptions != null)
                {
                    localExceptions = new __ExceptionInstance[Exceptions.Length];
                    Array.Copy(Exceptions, localExceptions, Exceptions.Length);
                }

                if (TokenFixups != null)
                {
                    localTokenFixups = new int[TokenFixups.Length];
                    Array.Copy(TokenFixups, localTokenFixups, TokenFixups.Length);
                }
                
                if (RVAFixups != null)
                {
                    localRVAFixups = new int[RVAFixups.Length];
                    Array.Copy(RVAFixups, localRVAFixups, RVAFixups.Length);
                }

                InternalSetMethodIL(meth.GetToken().Token, meth.InitLocals, body, LocalSig, sigLength, maxstack,
                    meth.GetNumberOfExceptions(), localExceptions, localTokenFixups, localRVAFixups, m_module);
            }

            m_hasBeenCreated = true;

            // Terminate the process.
            Type cls = TermCreateClass(m_tdType.Token, m_module);

            if (!m_isHiddenGlobalType)
            {
                m_runtimeType =(RuntimeType) cls;
              
                // if this type is a nested type, we need to invalidate the cached nested runtime type on the nesting type
                if (m_DeclaringType != null &&(RuntimeType)m_DeclaringType.m_runtimeType != null)
                {
                   ((RuntimeType)m_DeclaringType.m_runtimeType).InvalidateCachedNestedType();
                }

                return cls;
            }
            else
            {
                return null;
            }
        }
 internal static extern void InternalSetMethodIL(int methodHandle, bool isInitLocals, byte[] body, byte[] LocalSig, 
     int sigLength, int maxStackSize, int numExceptions, __ExceptionInstance[] exceptions, int []tokenFixups, 
     int []rvaFixups, Module module);