Skip to content

Commit

Permalink
Explain the __reftype usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Jun 22, 2024
1 parent 6eccc61 commit b035cde
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ZeroLog.Impl.Full/Support/TypeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ public static IntPtr GetTypeHandleSlow(Type type)

private static Func<IntPtr, Type?> BuildGetTypeFromHandleFunc()
{
// The GetTypeFromHandleUnsafe method is the preferred way to get a Type from a handle before .NET 7, as it dates back to the .NET Framework.
var method = typeof(Type).GetMethod("GetTypeFromHandleUnsafe", BindingFlags.Static | BindingFlags.NonPublic, null, [typeof(IntPtr)], null);
if (method is not null)
{
var param = Parameter(typeof(IntPtr));
return Lambda<Func<IntPtr, Type?>>(Call(method, param), param).Compile();
}

// The GetTypeFromHandleUnsafe method can get trimmed away on .NET 6: ArgIterator is the only type which uses this internal method of the core library,
// and since varargs are only supported on non-ARM Windows, GetTypeFromHandleUnsafe will get removed on other platforms such as Linux.
// To get around this, we use __reftype to convert the handle, but we need to build a TypedReference equivalent manually.
return static handle =>
{
IL.Push(new TypedReferenceLayout { Value = default, Type = handle });
Expand Down

0 comments on commit b035cde

Please sign in to comment.