Skip to content

Commit ebdef69

Browse files
committed
Added short path in fallback logic for RegisterAll
1 parent 1cdc65d commit ebdef69

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

Microsoft.Toolkit.Mvvm/Messaging/IMessengerExtensions.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,20 @@ static Action<IMessenger, object, TToken> LoadRegistrationMethodsForType(Type re
153153
// The LINQ codegen bloat is not really important for the same reason.
154154
static Action<IMessenger, object, TToken> LoadRegistrationMethodsForTypeFallback(Type recipientType)
155155
{
156+
// Get the collection of validation methods
157+
MethodInfo[] registrationMethods = (
158+
from interfaceType in recipientType.GetInterfaces()
159+
where interfaceType.IsGenericType &&
160+
interfaceType.GetGenericTypeDefinition() == typeof(IRecipient<>)
161+
let messageType = interfaceType.GenericTypeArguments[0]
162+
select MethodInfos.RegisterIRecipient.MakeGenericMethod(messageType, typeof(TToken))).ToArray();
163+
164+
// Short path if there are no message handlers to register
165+
if (registrationMethods.Length == 0)
166+
{
167+
return static (_, _, _) => { };
168+
}
169+
156170
// Input parameters (IMessenger instance, non-generic recipient, token)
157171
ParameterExpression
158172
arg0 = Expression.Parameter(typeof(IMessenger)),
@@ -178,11 +192,7 @@ static Action<IMessenger, object, TToken> LoadRegistrationMethodsForTypeFallback
178192
// We also add an explicit object conversion to cast the input recipient type to
179193
// the actual specific type, so that the exposed message handlers are accessible.
180194
BlockExpression body = Expression.Block(
181-
from interfaceType in recipientType.GetInterfaces()
182-
where interfaceType.IsGenericType &&
183-
interfaceType.GetGenericTypeDefinition() == typeof(IRecipient<>)
184-
let messageType = interfaceType.GenericTypeArguments[0]
185-
let registrationMethod = MethodInfos.RegisterIRecipient.MakeGenericMethod(messageType, typeof(TToken))
195+
from registrationMethod in registrationMethods
186196
select Expression.Call(registrationMethod, new Expression[]
187197
{
188198
arg0,

0 commit comments

Comments
 (0)