public static IntPtr LoadLocalLibrary()

in binding/Binding.Shared/LibraryLoader.cs [34:116]


		public static IntPtr LoadLocalLibrary<T> (string libraryName)
		{
			var libraryPath = GetLibraryPath (libraryName);

			var handle = LoadLibrary (libraryPath);
			if (handle == IntPtr.Zero)
				throw new DllNotFoundException ($"Unable to load library '{libraryName}'.");

			return handle;

			static string GetLibraryPath (string libraryName)
			{
				var arch = PlatformConfiguration.Is64Bit
					? PlatformConfiguration.IsArm ? "arm64" : "x64"
					: PlatformConfiguration.IsArm ? "arm" : "x86";

				var libWithExt = libraryName;
				if (!libraryName.EndsWith (Extension, StringComparison.OrdinalIgnoreCase))
					libWithExt += Extension;

				// 1. try alongside managed assembly
				var path = typeof (T).Assembly.Location;
				if (!string.IsNullOrEmpty (path)) {
					path = Path.GetDirectoryName (path);
					if (CheckLibraryPath (path, arch, libWithExt, out var localLib))
						return localLib;
				}

				// 2. try current directory
				if (CheckLibraryPath (Directory.GetCurrentDirectory (), arch, libWithExt, out var lib))
					return lib;

				// 3. try app domain
				try {
					if (AppDomain.CurrentDomain is AppDomain domain) {
						// 3.1 RelativeSearchPath
						if (CheckLibraryPath (domain.RelativeSearchPath, arch, libWithExt, out lib))
							return lib;

						// 3.2 BaseDirectory
						if (CheckLibraryPath (domain.BaseDirectory, arch, libWithExt, out lib))
							return lib;
					}
				} catch {
					// no-op as there may not be any domain or path
				}

				// 4. use PATH or default loading mechanism
				return libWithExt;
			}

			static bool CheckLibraryPath(string root, string arch, string libWithExt, out string foundPath)
			{
				if (!string.IsNullOrEmpty (root)) {
					// a. in specific platform sub dir
					if (!string.IsNullOrEmpty (PlatformConfiguration.LinuxFlavor)) {
						var muslLib = Path.Combine (root, PlatformConfiguration.LinuxFlavor + "-" + arch, libWithExt);
						if (File.Exists (muslLib)) {
							foundPath = muslLib;
							return true;
						}
					}

					// b. in generic platform sub dir
					var searchLib = Path.Combine (root, arch, libWithExt);
					if (File.Exists (searchLib)) {
						foundPath = searchLib;
						return true;
					}

					// c. in root
					searchLib = Path.Combine (root, libWithExt);
					if (File.Exists (searchLib)) {
						foundPath = searchLib;
						return true;
					}
				}

				// d. nothing
				foundPath = null;
				return false;
			}
		}