private static CILModule LoadModule(CILReflectionContextImpl cctx, Stream stream, EmittingArguments eArgs, Func <CILModule, CILAssembly> modOwnerLoader, out ModuleReader moduleReaderOut, out MetaDataReader mdOut)
    {
        ArgumentValidator.ValidateNotNull("Reflection context", cctx);
        ArgumentValidator.ValidateNotNull("Stream", stream);
        ArgumentValidator.ValidateNotNull("Emitting arguments", eArgs);

        DLLFlags       dllFlags; TargetRuntime targetRuntime;
        ModuleReader   moduleReader;
        MetaDataReader md;
        IDictionary <String, ManifestResource> mResources;

        try
        {
            moduleReaderOut = new ModuleReader(cctx, stream, eArgs, out targetRuntime, out dllFlags, out mdOut, out mResources);
            moduleReader    = moduleReaderOut;
            md = mdOut;
            eArgs.AssemblyRefs.Clear();
            foreach (var aRef in md.assemblyRef)
            {
                eArgs.AssemblyRefs.Add(new CILAssemblyName(aRef.Item7, aRef.Item1, aRef.Item2, aRef.Item3, aRef.Item4, AssemblyHashAlgorithm.None, aRef.Item5, aRef.Item6, aRef.Item8));
            }
        }
        catch (Exception x)
        {
            if (x is BadImageFormatException)
            {
                throw;
            }
            else
            {
                throw new BadImageFormatException("Exception when loading assembly (" + x + ").", x);
            }
        }

        var retVal = cctx.Cache.NewModule(mID => new CILModuleImpl(
                                              cctx,
                                              mID,
                                              new LazyWithLock <ListProxy <CILCustomAttribute> >(() =>
        {
            // Force evaluation of module types ( to get stuff populated in module reader)
            var dummy = cctx.Cache.ResolveModuleID(mID).DefinedTypes;
            return(moduleReader.ReadModuleCustomAttributes(mID));
        }),
                                              () => modOwnerLoader(cctx.Cache.ResolveModuleID(mID)),
                                              md.module[0].Item2,
                                              () => moduleReader.GetModuleInitializer(),
                                              () => moduleReader.CreateLogicalStructure(),
                                              () => moduleReader._mscorLibRef.Value,
                                              mResources
                                              ));

        moduleReader.SetThisModule(retVal);

        eArgs.SetCLREntryPoint(() =>
        {
            CILMethod epMethod;
            if (moduleReader.HasEntryPoint())
            {
                // Initialize logical structures first
                var dummy = retVal.DefinedTypes;
                // Get the CILMethod
                epMethod = moduleReader.GetEntryPoint();
            }
            else
            {
                epMethod = null;
            }
            return(epMethod);
        });
        return(retVal);
    }