Ejemplo n.º 1
0
 /// <summary>
 /// 初始化 <see cref="CQPDynamicLibrary"/> 类的新实例, 并加载指定的动态链接库 (DLL) 和对应的 Json
 /// </summary>
 /// <param name="libFileName">要加载的动态链接库 (DLL) 的路径</param>
 /// <param name="jsonFileName">要加载的 Json 的路径</param>
 /// <exception cref="FileNotFoundException">未找到指定的 Json 文件</exception>
 public CQPDynamicLibrary(string libFileName, string jsonFileName)
     : base(libFileName)
 {
     this._jsonPath = OtherUtility.GetAbsolutePath(this.LibraryDirectory, jsonFileName);
     if (!File.Exists(this._jsonPath))
     {
         throw new FileNotFoundException("未找到指定的 Json 文件", jsonFileName);
     }
     using (StreamReader reader = new StreamReader(this._jsonPath, Encoding.UTF8))
     {
         this._appInfo = JsonSerializer.Deserialize <AppInfo> (reader.ReadToEnd(), new JsonSerializerOptions()
         {
             ReadCommentHandling = JsonCommentHandling.Skip
         });
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 初始化 <see cref="DynamicLibrary"/> 类的新实例, 并加载指定的动态链接库 (DLL)
        /// </summary>
        /// <param name="libFileName">要加载的动态链接库 (DLL) 的路径</param>
        public DynamicLibrary(string libFileName)
        {
            foreach (string item in DynamicLibrary._baseDirectories)
            {
                string fullPath = OtherUtility.GetAbsolutePath(item, libFileName);
                if (File.Exists(fullPath))
                {
                    // 初始化属性
                    this._isDispose        = false;
                    this._libraryPath      = fullPath;
                    this._libraryDirectory = Path.GetDirectoryName(fullPath);

                    // 加载动态库
                    this._hModule = Kernel32.LoadLibraryA(fullPath);
                    if (this._hModule.ToInt64() == 0)
                    {
                        throw new FileLoadException($"试图加载格式不正确的程序集 {fullPath}");
                    }
                    return;
                }
            }

            throw new FileNotFoundException("无法找到指定的程序集", libFileName);
        }