diff --git a/src/WinRT.Runtime/Marshalers.cs b/src/WinRT.Runtime/Marshalers.cs index 2d3acb376..9a1fdadc5 100644 --- a/src/WinRT.Runtime/Marshalers.cs +++ b/src/WinRT.Runtime/Marshalers.cs @@ -1707,11 +1707,21 @@ public static T FromAbi(IntPtr nativeDelegate) internal static class Marshaler { - internal static Func ReturnParameterFunc = (object box) => box; - internal static unsafe Action CopyIntEnumFunc = - (object value, IntPtr dest) => *(int*)dest.ToPointer() = (int)Convert.ChangeType(value, typeof(int)); - internal static unsafe Action CopyUIntEnumFunc = - (object value, IntPtr dest) => *(uint*)dest.ToPointer() = (uint)Convert.ChangeType(value, typeof(uint)); + internal static readonly Func ReturnParameterFunc = ReturnParameter; + internal static readonly Action CopyIntEnumFunc = CopyIntEnum; + internal static readonly Action CopyIntEnumDirectFunc = CopyIntEnumDirect; + internal static readonly Action CopyUIntEnumFunc = CopyUIntEnum; + internal static readonly Action CopyUIntEnumDirectFunc = CopyUIntEnumDirect; + + private static object ReturnParameter(object arg) => arg; + + private static unsafe void CopyIntEnum(object value, IntPtr dest) => *(int*)dest.ToPointer() = (int)Convert.ChangeType(value, typeof(int)); + + private static unsafe void CopyIntEnumDirect(object value, IntPtr dest) => *(int*)dest.ToPointer() = (int)value; + + private static unsafe void CopyUIntEnum(object value, IntPtr dest) => *(uint*)dest.ToPointer() = (uint)Convert.ChangeType(value, typeof(uint)); + + private static unsafe void CopyUIntEnumDirect(object value, IntPtr dest) => *(uint*)dest.ToPointer() = (uint)value; } #if EMBED @@ -1900,12 +1910,12 @@ static Marshaler() if (typeof(T).GetEnumUnderlyingType() == typeof(int)) { CopyAbi = Marshaler.CopyIntEnumFunc; - CopyManaged = (T value, IntPtr dest) => Marshaler.CopyIntEnumFunc(value, dest); + CopyManaged = Marshaler.CopyIntEnumDirectFunc.WithTypedT1(); } else { CopyAbi = Marshaler.CopyUIntEnumFunc; - CopyManaged = (T value, IntPtr dest) => Marshaler.CopyUIntEnumFunc(value, dest); + CopyManaged = Marshaler.CopyUIntEnumDirectFunc.WithTypedT1(); } } CreateMarshalerArray = (T[] array) => MarshalBlittable.CreateMarshalerArray(array); diff --git a/src/cswinrt/strings/WinRT.cs b/src/cswinrt/strings/WinRT.cs index 32e3a58cc..cbf4be931 100644 --- a/src/cswinrt/strings/WinRT.cs +++ b/src/cswinrt/strings/WinRT.cs @@ -47,6 +47,11 @@ public static Action WithMarshaler2Support(this Action return action.InvokeWithMarshaler2Support; } + public static Action WithTypedT1(this Action action) + { + return action.InvokeWithTypedT1; + } + public static Func WithObjectT(this Func function) { return function.InvokeWithObjectT; @@ -98,6 +103,11 @@ private static void InvokeWithObjectParams(this Action func, object arg) { func.Invoke((T)arg); } + + private static void InvokeWithTypedT1(this Action action, T arg1, IntPtr arg2) + { + action.Invoke(arg1, arg2); + } } internal sealed class Platform