@@ -30,7 +30,7 @@ static NativeMethods()
3030 {
3131 if ( Platform . IsRunningOnNetFramework ( ) || Platform . IsRunningOnNetCore ( ) )
3232 {
33- // Use .NET Core 3.0+ NativeLibrary when available.
33+ // Use NativeLibrary when available.
3434 if ( ! TryUseNativeLibrary ( ) )
3535 {
3636 // NativeLibrary is not available, fall back.
@@ -40,6 +40,7 @@ static NativeMethods()
4040 // If this call succeeds further DllImports will find the library loaded and not attempt to load it again.
4141 // If it fails the next DllImport will load the library from safe directories.
4242 string nativeLibraryPath = GetGlobalSettingsNativeLibraryPath ( ) ;
43+
4344 if ( nativeLibraryPath != null )
4445 {
4546 if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
@@ -61,89 +62,46 @@ static NativeMethods()
6162 private static string GetGlobalSettingsNativeLibraryPath ( )
6263 {
6364 string nativeLibraryDir = GlobalSettings . GetAndLockNativeLibraryPath ( ) ;
65+
6466 if ( nativeLibraryDir == null )
6567 {
6668 return null ;
6769 }
68- return Path . Combine ( nativeLibraryDir , libgit2 + Platform . GetNativeLibraryExtension ( ) ) ;
69- }
70-
71- private delegate bool TryLoadLibraryByNameDelegate ( string libraryName , Assembly assembly , DllImportSearchPath ? searchPath , out IntPtr handle ) ;
72- private delegate bool TryLoadLibraryByPathDelegate ( string libraryPath , out IntPtr handle ) ;
73-
74- static TryLoadLibraryByNameDelegate _tryLoadLibraryByName ;
75- static TryLoadLibraryByPathDelegate _tryLoadLibraryByPath ;
76-
77- static bool TryLoadLibrary ( string libraryName , Assembly assembly , DllImportSearchPath ? searchPath , out IntPtr handle )
78- {
79- if ( _tryLoadLibraryByName == null )
80- {
81- throw new NotSupportedException ( ) ;
82- }
83- return _tryLoadLibraryByName ( libraryName , assembly , searchPath , out handle ) ;
84- }
8570
86- static bool TryLoadLibrary ( string libraryPath , out IntPtr handle )
87- {
88- if ( _tryLoadLibraryByPath == null )
89- {
90- throw new NotSupportedException ( ) ;
91- }
92- return _tryLoadLibraryByPath ( libraryPath , out handle ) ;
71+ return Path . Combine ( nativeLibraryDir , libgit2 + Platform . GetNativeLibraryExtension ( ) ) ;
9372 }
9473
74+ #if NETSTANDARD
75+ private static bool TryUseNativeLibrary ( ) => false ;
76+ #else
9577 private static bool TryUseNativeLibrary ( )
9678 {
97- // NativeLibrary is available in .NET Core 3.0+.
98- // We use reflection to use NativeLibrary so this library can target 'netstandard2.0'.
99-
100- Type dllImportResolverType = Type . GetType ( "System.Runtime.InteropServices.DllImportResolver, System.Runtime.InteropServices" , throwOnError : false ) ;
101- Type nativeLibraryType = Type . GetType ( "System.Runtime.InteropServices.NativeLibrary, System.Runtime.InteropServices" , throwOnError : false ) ;
102- var tryLoadLibraryByName = ( TryLoadLibraryByNameDelegate ) nativeLibraryType ? . GetMethod ( "TryLoad" ,
103- new Type [ ] { typeof ( string ) , typeof ( Assembly ) , typeof ( DllImportSearchPath ? ) , typeof ( IntPtr ) . MakeByRefType ( ) } ) ? . CreateDelegate ( typeof ( TryLoadLibraryByNameDelegate ) ) ;
104- var tryLoadLibraryByPath = ( TryLoadLibraryByPathDelegate ) nativeLibraryType ? . GetMethod ( "TryLoad" ,
105- new Type [ ] { typeof ( string ) , typeof ( IntPtr ) . MakeByRefType ( ) } ) ? . CreateDelegate ( typeof ( TryLoadLibraryByPathDelegate ) ) ;
106- MethodInfo setDllImportResolver = nativeLibraryType ? . GetMethod ( "SetDllImportResolver" , new Type [ ] { typeof ( Assembly ) , dllImportResolverType } ) ;
107-
108- if ( dllImportResolverType == null ||
109- nativeLibraryType == null ||
110- tryLoadLibraryByName == null ||
111- tryLoadLibraryByPath == null ||
112- setDllImportResolver == null )
113- {
114- return false ;
115- }
116-
117- _tryLoadLibraryByPath = tryLoadLibraryByPath ;
118- _tryLoadLibraryByName = tryLoadLibraryByName ;
119-
120- // NativeMethods.SetDllImportResolver(typeof(NativeMethods).Assembly, ResolveDll);
121- object resolveDelegate = typeof ( NativeMethods ) . GetMethod ( nameof ( ResolveDll ) , BindingFlags . NonPublic | BindingFlags . Static ) . CreateDelegate ( dllImportResolverType ) ;
122- setDllImportResolver . Invoke ( null , new object [ ] { typeof ( NativeMethods ) . Assembly , resolveDelegate } ) ;
79+ NativeLibrary . SetDllImportResolver ( typeof ( NativeMethods ) . Assembly , ResolveDll ) ;
12380
12481 return true ;
12582 }
12683
12784 private static IntPtr ResolveDll ( string libraryName , Assembly assembly , DllImportSearchPath ? searchPath )
12885 {
12986 IntPtr handle = IntPtr . Zero ;
87+
13088 if ( libraryName == libgit2 )
13189 {
13290 // Use GlobalSettings.NativeLibraryPath when set.
13391 string nativeLibraryPath = GetGlobalSettingsNativeLibraryPath ( ) ;
134- if ( nativeLibraryPath != null &&
135- TryLoadLibrary ( nativeLibraryPath , out handle ) )
92+
93+ if ( nativeLibraryPath != null && NativeLibrary . TryLoad ( nativeLibraryPath , out handle ) )
13694 {
13795 return handle ;
13896 }
13997
14098 // Use Default DllImport resolution.
141- if ( TryLoadLibrary ( libraryName , assembly , searchPath , out handle ) )
99+ if ( NativeLibrary . TryLoad ( libraryName , assembly , searchPath , out handle ) )
142100 {
143101 return handle ;
144102 }
145103
146- // We cary a number of .so files for Linux which are linked against various
104+ // We carry a number of .so files for Linux which are linked against various
147105 // libc/OpenSSL libraries. Try them out.
148106 if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
149107 {
@@ -158,16 +116,19 @@ private static IntPtr ResolveDll(string libraryName, Assembly assembly, DllImpor
158116 foreach ( var runtimeFolder in Directory . GetDirectories ( runtimesDirectory , $ "*-{ processorArchitecture } ") )
159117 {
160118 string libPath = Path . Combine ( runtimeFolder , "native" , $ "lib{ libraryName } .so") ;
161- if ( TryLoadLibrary ( libPath , out handle ) )
119+
120+ if ( NativeLibrary . TryLoad ( libPath , out handle ) )
162121 {
163122 return handle ;
164123 }
165124 }
166125 }
167126 }
168127 }
128+
169129 return handle ;
170130 }
131+ #endif
171132
172133 public const int RTLD_NOW = 0x002 ;
173134
0 commit comments