44using System ;
55using System . IO ;
66using System . Reflection ;
7+ using System . Runtime . CompilerServices ;
78using System . Runtime . InteropServices ;
89using Xunit ;
910
@@ -69,7 +70,26 @@ public static void AssemblyDirectory_Fallback_Found()
6970 Environment . CurrentDirectory = Subdirectory ;
7071
7172 // Library should not be found in the assembly directory, but should fall back to the default OS search which includes CWD on Windows
72- int sum = NativeLibraryPInvoke . Sum ( 1 , 2 ) ;
73+ int sum = NativeLibraryPInvoke . Sum_Copy ( 1 , 2 ) ;
74+ Assert . Equal ( 3 , sum ) ;
75+ }
76+ finally
77+ {
78+ Environment . CurrentDirectory = currentDirectory ;
79+ }
80+ }
81+
82+ [ Fact ]
83+ [ PlatformSpecific ( TestPlatforms . Windows ) ]
84+ public static void DefaultFlags_Found ( )
85+ {
86+ string currentDirectory = Environment . CurrentDirectory ;
87+ try
88+ {
89+ Environment . CurrentDirectory = Subdirectory ;
90+
91+ // Library should not be found in the assembly directory, but should fall back to the default OS search which includes CWD on Windows
92+ int sum = NativeLibraryPInvoke . Sum_DefaultFlags ( 1 , 2 ) ;
7393 Assert . Equal ( 3 , sum ) ;
7494 }
7595 finally
@@ -95,22 +115,38 @@ public static void AssemblyDirectory_SearchFlags_WithDependency_Found()
95115
96116public class NativeLibraryPInvoke
97117{
118+ internal const string CopyName = $ "{ NativeLibraryToLoad . Name } -copy";
119+ internal const string DefaultFlagsName = $ "{ NativeLibraryToLoad . Name } -default-flags";
120+
121+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
98122 public static int Sum ( int a , int b )
99- {
100- return NativeSum ( a , b ) ;
101- }
123+ => NativeSum ( a , b ) ;
124+
125+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
126+ public static int Sum_Copy ( int a , int b )
127+ => NativeSum_Copy ( a , b ) ;
128+
129+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
130+ public static int Sum_DefaultFlags ( int a , int b )
131+ => NativeSum_DefaultFlags ( a , b ) ;
102132
103133 [ DllImport ( NativeLibraryToLoad . Name ) ]
104134 [ DefaultDllImportSearchPaths ( DllImportSearchPath . AssemblyDirectory ) ]
105135 static extern int NativeSum ( int arg1 , int arg2 ) ;
136+
137+ [ DllImport ( CopyName , EntryPoint = nameof ( NativeSum ) ) ]
138+ [ DefaultDllImportSearchPaths ( DllImportSearchPath . AssemblyDirectory ) ]
139+ static extern int NativeSum_Copy ( int arg1 , int arg2 ) ;
140+
141+ [ DllImport ( DefaultFlagsName , EntryPoint = nameof ( NativeSum ) ) ]
142+ static extern int NativeSum_DefaultFlags ( int arg1 , int arg2 ) ;
106143}
107144
108145public class NativeLibraryPInvokeAot
109146{
147+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
110148 public static int Sum ( int a , int b )
111- {
112- return NativeSum ( a , b ) ;
113- }
149+ => NativeSum ( a , b ) ;
114150
115151 // For NativeAOT, validate the case where the native library is next to the AOT application.
116152 // The passing of DllImportSearchPath.System32 is done to ensure on Windows the runtime won't fallback
@@ -122,10 +158,9 @@ public static int Sum(int a, int b)
122158
123159public class NativeLibraryWithDependency
124160{
161+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
125162 public static int Sum ( int a , int b )
126- {
127- return CallDependencySum ( a , b ) ;
128- }
163+ => CallDependencySum ( a , b ) ;
129164
130165 // For LoadLibrary on Windows, search flags, like that represented by System32, are incompatible with
131166 // looking at a specific path (per AssemblyDirectory), so we specify both flags to validate that we do
0 commit comments