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 95be96d86858..2633bd26e273 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,15 @@ namespace System.Runtime.InteropServices
///
public static class MarshalEx
{
- public static TSafeHandle CreateSafeHandle()
+ ///
+ /// 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
{
if (typeof(TSafeHandle).IsAbstract || typeof(TSafeHandle).GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance | BindingFlags.Instance, null, Type.EmptyTypes, null) == null)
@@ -21,6 +30,11 @@ public static TSafeHandle CreateSafeHandle()
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 });