private Assembly loadComAssembly(string fileName)
        {
            string strPath = Path.GetDirectoryName(fileName) + "\\";

            if (Path.GetDirectoryName(fileName).Length <= 0)
            {
                strPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\";
            }

            //if (Path.GetDirectoryName(fileName).Length <= 0)
            //    strPath = string.Empty;

            string strSrcFileName = strPath + Path.GetFileName(fileName);

            if (!File.Exists(strSrcFileName))
            {
                return(null);
            }

            //RegComDll(m_mapComAssembly[assembly.FullName].ToString());
            RegComDll(strSrcFileName); //每次自动注册com dll

            string strDstFileName     = "interop." + Path.GetFileName(fileName);
            string strDetFullFileName = strPath + strDstFileName;
            string strNameSpace       = Path.GetFileNameWithoutExtension(fileName);//文件名作为命名空间

            //如果interop.dll的时间比dll时间早,则需要重新生成
            bool bNeedGenrate = false;

            if (File.Exists(strDetFullFileName))
            {
                DateTime dtInteropTime = File.GetLastWriteTime(strDetFullFileName);
                DateTime dtSrcTime     = File.GetLastWriteTime(strSrcFileName);
                if (dtSrcTime >= dtInteropTime)
                {
                    bNeedGenrate = true;
                }
            }
            else
            {
                bNeedGenrate = true;
            }

            if (bNeedGenrate)
            {
                Object typeLib;
                LoadTypeLibEx(fileName, RegKind.RegKind_None, out typeLib);

                if (typeLib == null)
                {
                    //throw new Exception("载入失败!");
                    return(null);
                }

                TypeLibConverter       converter    = new TypeLibConverter();
                ConversionEventHandler eventHandler = new ConversionEventHandler();
                AssemblyBuilder        ab           = converter.ConvertTypeLibToAssembly(typeLib, strDetFullFileName, 0,
                                                                                         eventHandler, null, null, strNameSpace, null);
                ab.Save(strDstFileName);
            }
            Assembly asm = Assembly.LoadFile(strDetFullFileName);//Application.StartupPath + @"\" + strDstFileName);

            //Type t = asm.GetType("interop.test.tcls1"); //old
            //Type t = asm.GetType("Prj2.tcls1"); //new, comdll=Prj2.dll
            if (asm != null)
            {
                m_mapComAssembly[asm.FullName] = strSrcFileName;
            }
            return(asm);
        }
示例#2
0
        private Assembly loadComAssembly(string fileName)
        {
            string strPath = Path.GetDirectoryName(fileName) + "\\";
            if (Path.GetDirectoryName(fileName).Length <= 0)
                strPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\";
            
            //if (Path.GetDirectoryName(fileName).Length <= 0)
            //    strPath = string.Empty;

            string strSrcFileName = strPath + Path.GetFileName(fileName);
            if (!File.Exists(strSrcFileName))
                return null;

            //RegComDll(m_mapComAssembly[assembly.FullName].ToString());
            RegComDll(strSrcFileName); //每次自动注册com dll

            string strDstFileName = "interop." + Path.GetFileName(fileName);
            string strDetFullFileName = strPath + strDstFileName;
            string strNameSpace = Path.GetFileNameWithoutExtension(fileName);//文件名作为命名空间

            //如果interop.dll的时间比dll时间早,则需要重新生成
            bool bNeedGenrate = false;
            if (File.Exists(strDetFullFileName))
            {
                DateTime dtInteropTime = File.GetLastWriteTime(strDetFullFileName);
                DateTime dtSrcTime = File.GetLastWriteTime(strSrcFileName);
                if (dtSrcTime >= dtInteropTime)
                    bNeedGenrate = true;
            }
            else
                bNeedGenrate = true;

            if (bNeedGenrate)
            {
                Object typeLib;
                LoadTypeLibEx(fileName, RegKind.RegKind_None, out typeLib);

                if (typeLib == null)
                {
                    //throw new Exception("载入失败!");
                    return null;
                }

                TypeLibConverter converter = new TypeLibConverter();
                ConversionEventHandler eventHandler = new ConversionEventHandler();
                AssemblyBuilder ab = converter.ConvertTypeLibToAssembly(typeLib, strDetFullFileName, 0,
                eventHandler, null, null, strNameSpace, null);
                ab.Save(strDstFileName);
            }
            Assembly asm = Assembly.LoadFile(strDetFullFileName);//Application.StartupPath + @"\" + strDstFileName);
            //Type t = asm.GetType("interop.test.tcls1"); //old
            //Type t = asm.GetType("Prj2.tcls1"); //new, comdll=Prj2.dll
            if (asm != null)
                m_mapComAssembly[asm.FullName] = strSrcFileName;
            return asm;
        }