示例#1
0
 /// <summary>
 /// Imports the library by name (without extensions) locating it based on platform.
 ///
 /// Use <code>suppressUnload</code> to prevent the dll from unloading at finalization,
 /// which can be useful if you need to call the imported functions in finalizers of
 /// other instances and can't predict in which order the finalization will occur
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="name"></param>
 /// <param name="suppressUnload">true to prevent unloading on finalization</param>
 /// <returns></returns>
 public static T Import <T>(string name, string version, bool suppressUnload = false) where T : class
 {
     if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
     {
         return(Importers.Import <T>(Importers.Windows, name, version, suppressUnload));
     }
     else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
     {
         return(Importers.Import <T>(Importers.Posix, name, version, suppressUnload));
     }
     else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
     {
         return(Importers.Import <T>(Importers.Posix, name, version, suppressUnload));
     }
     else
     {
         return(Importers.Import <T>(Importers.Windows, name, version, suppressUnload));
     }
 }
示例#2
0
        /// <summary>
        /// Imports the library by name (without extensions) locating it based on platform.
        ///
        /// Use <code>suppressUnload</code> to prevent the dll from unloading at finalization,
        /// which can be useful if you need to call the imported functions in finalizers of
        /// other instances and can't predict in which order the finalization will occur
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <param name="suppressUnload">true to prevent unloading on finalization</param>
        /// <returns></returns>
        public static T Import <T>(string name, bool suppressUnload = false) where T : class
        {
            var subdir = Environment.Is64BitProcess ? "amd64" : "i386";
            var path   = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "native", subdir, name);

            switch ((int)Environment.OSVersion.Platform)
            {
            case (int)PlatformID.Win32Windows: // Win9x supported?
            case (int)PlatformID.Win32S:       // Win16 NTVDM on Win x86?
            case (int)PlatformID.Win32NT:      // Windows NT
            case (int)PlatformID.WinCE:
                return(Importers.Import <T>(Importers.Windows, path + ".dll", suppressUnload));

            case (int)PlatformID.MacOSX:
            case 128:     // Mono Mac
                return(Importers.Import <T>(Importers.Posix, path + ".dylib", suppressUnload));

            case (int)PlatformID.Unix:
                return(Importers.Import <T>(Importers.Posix, path + ".so", suppressUnload));

            default:
                return(Importers.Import <T>(Importers.Windows, path, suppressUnload));
            }
        }