From 95c7758391428417cd8a26e566b3139d24bb41b8 Mon Sep 17 00:00:00 2001 From: sakno Date: Wed, 20 Mar 2024 19:48:26 +0200 Subject: [PATCH] Removed type cast for each call --- src/DotNext/Func.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/DotNext/Func.cs b/src/DotNext/Func.cs index 4ea604ecb..7b8515e03 100644 --- a/src/DotNext/Func.cs +++ b/src/DotNext/Func.cs @@ -95,12 +95,15 @@ public static Func Constant(T obj) // slow path - allocates a new delegate return obj is null ? Default! - : obj.UnboxAny; + : typeof(T).IsValueType + ? new BoxedConstant() { Value = obj }.GetValue + : Unsafe.As(ref obj).UnboxRefType; static T? Default() => default; } - private static T UnboxAny(this object obj) => (T)obj; + private static T UnboxRefType(this object obj) + => Unsafe.As(ref obj); private static Func Constant(bool value) { @@ -481,4 +484,11 @@ public static Result TryInvoke : StrongBox + { + internal T GetValue() => Value!; + + public override string? ToString() => Value?.ToString(); + } } \ No newline at end of file