From 9d969efddc75f23f9805803f3bcffaa60ff03ce3 Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Fri, 20 Nov 2020 12:30:51 -0800 Subject: [PATCH 1/2] Add DynamicallyAccessedMembers to CreateSafeHandle API --- DllImportGenerator/Ancillary.Interop/MarshalEx.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DllImportGenerator/Ancillary.Interop/MarshalEx.cs b/DllImportGenerator/Ancillary.Interop/MarshalEx.cs index 95be96d86858..2d309d848243 100644 --- a/DllImportGenerator/Ancillary.Interop/MarshalEx.cs +++ b/DllImportGenerator/Ancillary.Interop/MarshalEx.cs @@ -1,4 +1,5 @@ +using System.Diagnostics.CodeAnalysis; using System.Reflection; namespace System.Runtime.InteropServices @@ -9,7 +10,7 @@ namespace System.Runtime.InteropServices /// public static class MarshalEx { - public static TSafeHandle CreateSafeHandle() + public static TSafeHandle CreateSafeHandle<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.NonPublicConstructors)]TSafeHandle>() where TSafeHandle : SafeHandle { if (typeof(TSafeHandle).IsAbstract || typeof(TSafeHandle).GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance | BindingFlags.Instance, null, Type.EmptyTypes, null) == null) From e89e68d2d600b6b07d2b2bc94224fde40c47adb9 Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Fri, 20 Nov 2020 14:02:50 -0800 Subject: [PATCH 2/2] Add comments --- .../GeneratedDllImportAttribute.cs | 9 ++++++++- DllImportGenerator/Ancillary.Interop/MarshalEx.cs | 13 +++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/DllImportGenerator/Ancillary.Interop/GeneratedDllImportAttribute.cs b/DllImportGenerator/Ancillary.Interop/GeneratedDllImportAttribute.cs index 42c15094915a..73f012f86a41 100644 --- a/DllImportGenerator/Ancillary.Interop/GeneratedDllImportAttribute.cs +++ b/DllImportGenerator/Ancillary.Interop/GeneratedDllImportAttribute.cs @@ -1,7 +1,14 @@  namespace System.Runtime.InteropServices { - // [TODO] Remove once the attribute has been added to the BCL + /// + /// Indicates that method will be generated at compile time and invoke into an unmanaged library entry point + /// + /// + /// IL linker/trimming currently has special handling of P/Invokes (pinvokeimpl): + /// - https://github.com/mono/linker/blob/bfab847356063d21eb15e79f2b6c03df5bd6ef3d/src/linker/Linker.Steps/MarkStep.cs#L2623 + /// We may want to make the linker aware of this attribute as well. + /// [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] public sealed class GeneratedDllImportAttribute : Attribute { diff --git a/DllImportGenerator/Ancillary.Interop/MarshalEx.cs b/DllImportGenerator/Ancillary.Interop/MarshalEx.cs index 2d309d848243..2633bd26e273 100644 --- a/DllImportGenerator/Ancillary.Interop/MarshalEx.cs +++ b/DllImportGenerator/Ancillary.Interop/MarshalEx.cs @@ -10,6 +10,14 @@ namespace System.Runtime.InteropServices /// public static class MarshalEx { + /// + /// Create an instance of the given . + /// + /// Type of the SafeHandle + /// New instance of + /// + /// The must be non-abstract and have a parameterless constructor. + /// public static TSafeHandle CreateSafeHandle<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.NonPublicConstructors)]TSafeHandle>() where TSafeHandle : SafeHandle { @@ -22,6 +30,11 @@ public static class MarshalEx return safeHandle; } + /// + /// Sets the handle of to the specified . + /// + /// instance to update + /// Pre-existing handle public static void SetHandle(SafeHandle safeHandle, IntPtr handle) { typeof(SafeHandle).GetMethod("SetHandle", BindingFlags.NonPublic | BindingFlags.Instance)!.Invoke(safeHandle, new object[] { handle });