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