From 89313ca06932bdb69c53965ec1e37b67f920ee95 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Thu, 27 May 2021 16:42:12 -0700 Subject: [PATCH] Marshal.PtrToStructure fixes (#53360) - Fix reflection annotations. PtrToStructure calls private constructors. - Move PtrToStructure overload that creates objects to shared CoreLib. - Fix Mono implementation to call private constructor. - Fix Mono error handling to match CoreCLR. --- .../Runtime/InteropServices/Marshal.CoreCLR.cs | 8 ------ .../src/System.Diagnostics.EventLog.csproj | 2 ++ .../src/System/Diagnostics/Reader/NativeWrapper.cs | 3 ++- .../src/System/Runtime/InteropServices/Marshal.cs | 29 +++++++++++++++++++--- .../ref/System.Runtime.InteropServices.cs | 4 +-- .../InteropServices/Marshal/PtrToStructureTests.cs | 9 ++++--- .../System/Runtime/InteropServices/Marshal.Mono.cs | 11 ++------ 7 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs index c1604da..688e4f8 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs @@ -209,14 +209,6 @@ namespace System.Runtime.InteropServices [MethodImpl(MethodImplOptions.InternalCall)] public static extern void StructureToPtr(object structure, IntPtr ptr, bool fDeleteOld); - private static object PtrToStructureHelper(IntPtr ptr, Type structureType) - { - var rt = (RuntimeType)structureType; - object structure = rt.CreateInstanceDefaultCtor(publicOnly: false, wrapExceptions: true)!; - PtrToStructureHelper(ptr, structure, allowValueClasses: true); - return structure; - } - /// /// Helper function to copy a pointer into a preallocated structure. /// diff --git a/src/libraries/System.Diagnostics.EventLog/src/System.Diagnostics.EventLog.csproj b/src/libraries/System.Diagnostics.EventLog/src/System.Diagnostics.EventLog.csproj index 3343628..57cef21 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/System.Diagnostics.EventLog.csproj +++ b/src/libraries/System.Diagnostics.EventLog/src/System.Diagnostics.EventLog.csproj @@ -129,6 +129,8 @@ + +