Refactor DllImports in System.Reflection.Metadata (dotnet/corefx#40579)
authormadmir <1119735+madmir@users.noreply.github.com>
Mon, 21 Oct 2019 20:25:04 +0000 (22:25 +0200)
committerSantiago Fernandez Madero <safern@microsoft.com>
Mon, 21 Oct 2019 20:25:04 +0000 (13:25 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/90706ad4925bb66f83b3fcade61ddda4c7a0f4e4

src/libraries/System.Reflection.Metadata/src/System.Reflection.Metadata.csproj
src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/FileStreamReadLightUp.cs
src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/FileStreamReadLightUp.netstandard1.1.cs
src/libraries/System.Reflection.Metadata/tests/System.Reflection.Metadata.Tests.csproj

index eb2b98e..55b6dfe 100644 (file)
@@ -8,6 +8,12 @@
     <Configurations>netcoreapp-Debug;netcoreapp-Release;netstandard-Debug;netstandard-Release;netstandard1.1-Debug;netstandard1.1-Release</Configurations>
   </PropertyGroup>
   <ItemGroup>
+    <Compile Include="$(CommonPath)\Interop\Windows\Interop.Libraries.cs">
+      <Link>Common\Interop\Windows\Interop.Libraries.cs</Link>
+    </Compile>
+    <Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs">
+      <Link>Common\CoreLib\Interop\Windows\kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs</Link>
+    </Compile>
     <Compile Include="System\Reflection\Internal\Utilities\PinnedObject.cs" />
     <Compile Include="System\Reflection\Internal\Utilities\CriticalDisposableObject.cs" Condition="'$(TargetFramework)' != 'netstandard1.1'" />
     <Compile Include="System\Reflection\Internal\Utilities\CriticalDisposableObject.netstandard1.1.cs" Condition="'$(TargetFramework)' == 'netstandard1.1'" />
index dfa92e8..d0aefd4 100644 (file)
@@ -53,12 +53,12 @@ namespace System.Reflection.Internal
                 return false;
             }
 
-            bool result = false;
-            int bytesRead = 0;
+            int result;
+            int bytesRead;
 
             try
             {
-                result = ReadFile(handle, buffer, size, out bytesRead, IntPtr.Zero);
+                result = Interop.Kernel32.ReadFile(handle, buffer, size, out bytesRead, IntPtr.Zero);
             }
             catch
             {
@@ -66,7 +66,7 @@ namespace System.Reflection.Internal
                 return false;
             }
 
-            if (!result || bytesRead != size)
+            if (result == 0 || bytesRead != size)
             {
                 // We used to throw here, but this is where we land if the FileStream was
                 // opened with useAsync: true, which is currently the default on .NET Core.
@@ -78,15 +78,5 @@ namespace System.Reflection.Internal
 
             return true;
         }
-
-        [DllImport(@"kernel32.dll", EntryPoint = "ReadFile", ExactSpelling = true, SetLastError = true)]
-        [return: MarshalAs(UnmanagedType.Bool)]
-        internal static extern unsafe bool ReadFile(
-             SafeHandle fileHandle,
-             byte* buffer,
-             int byteCount,
-             out int bytesRead,
-             IntPtr overlapped
-        );
     }
 }
index 8b3aa67..87b277a 100644 (file)
@@ -100,12 +100,12 @@ namespace System.Reflection.Internal
                 return false;
             }
 
-            bool result = false;
-            int bytesRead = 0;
+            int result;
+            int bytesRead;
 
             try
             {
-                result = ReadFile(handle, buffer, size, out bytesRead, IntPtr.Zero);
+                result = Interop.Kernel32.ReadFile(handle, buffer, size, out bytesRead, IntPtr.Zero);
             }
             catch
             {
@@ -113,7 +113,7 @@ namespace System.Reflection.Internal
                 return false;
             }
 
-            if (!result || bytesRead != size)
+            if (result == 0 || bytesRead != size)
             {
                 // We used to throw here, but this is where we land if the FileStream was
                 // opened with useAsync: true, which is currently the default on .NET Core.
@@ -125,15 +125,5 @@ namespace System.Reflection.Internal
 
             return true;
         }
-
-        [DllImport(@"kernel32.dll", EntryPoint = "ReadFile", ExactSpelling = true, SetLastError = true)]
-        [return: MarshalAs(UnmanagedType.Bool)]
-        internal static extern unsafe bool ReadFile(
-             SafeHandle fileHandle,
-             byte* buffer,
-             int byteCount,
-             out int bytesRead,
-             IntPtr overlapped
-        );
     }
 }
index a2454d6..b6db7fb 100644 (file)
@@ -2,6 +2,7 @@
   <PropertyGroup>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     <ExternallyShipping>false</ExternallyShipping>
+    <NoWarn>436</NoWarn> <!-- Type conflicts on "Interop" due to InternalsVisibleTo access -->
     <Configurations>netcoreapp-Debug;netcoreapp-Release;netfx-Debug;netfx-Release</Configurations>
   </PropertyGroup>
   <ItemGroup>