Skip to content

Commit 87d21e4

Browse files
committed
Remove one more lambda with closure for Nullable<T>
Also slightly optimize creating delegates for nullable delegates
1 parent 5f5ee04 commit 87d21e4

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/WinRT.Runtime/ComWrappersSupport.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,15 +449,26 @@ private static Func<IInspectable, object> CreateAbiNullableTFactory(
449449
#endif
450450
Type implementationType)
451451
{
452+
// This method is only called when 'implementationType' has been validated to be some ABI.System.Nullable_Delegate<T>.
453+
// As such, we know that the type definitely has a method with signature 'static Nullable GetValue(IInspectable)'.
452454
var getValueMethod = implementationType.GetMethod("GetValue", BindingFlags.Static | BindingFlags.NonPublic);
455+
456+
#if NET6_0_OR_GREATER
457+
return getValueMethod.CreateDelegate<Func<IInspectable, ABI.System.Nullable>>();
458+
#else
453459
return (IInspectable obj) => getValueMethod.Invoke(null, new[] { obj });
460+
#endif
454461
}
455462

456463
private static Func<IInspectable, object> CreateArrayFactory(Type implementationType)
457464
{
458-
var getValueFunc = (Func<IInspectable, object>)implementationType.GetHelperType().GetMethod("GetValue", BindingFlags.Static | BindingFlags.NonPublic).
459-
CreateDelegate(typeof(Func<IInspectable, object>));
460-
return getValueFunc;
465+
var getValueMethod = implementationType.GetHelperType().GetMethod("GetValue", BindingFlags.Static | BindingFlags.NonPublic);
466+
467+
#if NET6_0_OR_GREATER
468+
return getValueMethod.CreateDelegate<Func<IInspectable, object>>();
469+
#else
470+
return (Func<IInspectable, object>)getValueMethod.CreateDelegate(typeof(Func<IInspectable, object>));
471+
#endif
461472
}
462473

463474
// This is used to hold the reference to the native value type object (IReference) until the actual value in it (boxed as an object) gets cleaned up by GC

src/WinRT.Runtime/TypeExtensions.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,10 @@ internal static Type GetAuthoringMetadataType(this Type type)
244244
{
245245
return null;
246246
}
247-
else
248247
#endif
249-
{
250-
// Fallback code path for back compat with previously generated projections
251-
// running without AOT.
252-
return GetAuthoringMetadataTypeFallback(type);
253-
}
248+
// Fallback code path for back compat with previously generated projections
249+
// running without AOT.
250+
return GetAuthoringMetadataTypeFallback(type);
254251
});
255252
}
256253

0 commit comments

Comments
 (0)