@@ -30,7 +30,7 @@ static NativeMethods()
30
30
{
31
31
if ( Platform . IsRunningOnNetFramework ( ) || Platform . IsRunningOnNetCore ( ) )
32
32
{
33
- // Use .NET Core 3.0+ NativeLibrary when available.
33
+ // Use NativeLibrary when available.
34
34
if ( ! TryUseNativeLibrary ( ) )
35
35
{
36
36
// NativeLibrary is not available, fall back.
@@ -40,6 +40,7 @@ static NativeMethods()
40
40
// If this call succeeds further DllImports will find the library loaded and not attempt to load it again.
41
41
// If it fails the next DllImport will load the library from safe directories.
42
42
string nativeLibraryPath = GetGlobalSettingsNativeLibraryPath ( ) ;
43
+
43
44
if ( nativeLibraryPath != null )
44
45
{
45
46
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
@@ -61,89 +62,46 @@ static NativeMethods()
61
62
private static string GetGlobalSettingsNativeLibraryPath ( )
62
63
{
63
64
string nativeLibraryDir = GlobalSettings . GetAndLockNativeLibraryPath ( ) ;
65
+
64
66
if ( nativeLibraryDir == null )
65
67
{
66
68
return null ;
67
69
}
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
- }
85
70
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 ( ) ) ;
93
72
}
94
73
74
+ #if NETSTANDARD
75
+ private static bool TryUseNativeLibrary ( ) => false ;
76
+ #else
95
77
private static bool TryUseNativeLibrary ( )
96
78
{
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 ) ;
123
80
124
81
return true ;
125
82
}
126
83
127
84
private static IntPtr ResolveDll ( string libraryName , Assembly assembly , DllImportSearchPath ? searchPath )
128
85
{
129
86
IntPtr handle = IntPtr . Zero ;
87
+
130
88
if ( libraryName == libgit2 )
131
89
{
132
90
// Use GlobalSettings.NativeLibraryPath when set.
133
91
string nativeLibraryPath = GetGlobalSettingsNativeLibraryPath ( ) ;
134
- if ( nativeLibraryPath != null &&
135
- TryLoadLibrary ( nativeLibraryPath , out handle ) )
92
+
93
+ if ( nativeLibraryPath != null && NativeLibrary . TryLoad ( nativeLibraryPath , out handle ) )
136
94
{
137
95
return handle ;
138
96
}
139
97
140
98
// Use Default DllImport resolution.
141
- if ( TryLoadLibrary ( libraryName , assembly , searchPath , out handle ) )
99
+ if ( NativeLibrary . TryLoad ( libraryName , assembly , searchPath , out handle ) )
142
100
{
143
101
return handle ;
144
102
}
145
103
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
147
105
// libc/OpenSSL libraries. Try them out.
148
106
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
149
107
{
@@ -158,16 +116,19 @@ private static IntPtr ResolveDll(string libraryName, Assembly assembly, DllImpor
158
116
foreach ( var runtimeFolder in Directory . GetDirectories ( runtimesDirectory , $ "*-{ processorArchitecture } ") )
159
117
{
160
118
string libPath = Path . Combine ( runtimeFolder , "native" , $ "lib{ libraryName } .so") ;
161
- if ( TryLoadLibrary ( libPath , out handle ) )
119
+
120
+ if ( NativeLibrary . TryLoad ( libPath , out handle ) )
162
121
{
163
122
return handle ;
164
123
}
165
124
}
166
125
}
167
126
}
168
127
}
128
+
169
129
return handle ;
170
130
}
131
+ #endif
171
132
172
133
public const int RTLD_NOW = 0x002 ;
173
134
0 commit comments