@@ -17,9 +17,37 @@ namespace Microsoft.Toolkit.Mvvm.Messaging
1717 public static partial class MessengerExtensions
1818 {
1919 /// <summary>
20- /// The <see cref="MethodInfo"/> instance associated with <see cref="Register{TMessage,TToken}(IMessenger,IRecipient{TMessage},TToken)"/>.
20+ /// A class that acts as a container to load the <see cref="MethodInfo"/> instance linked to
21+ /// the <see cref="Register{TMessage,TToken}(IMessenger,IRecipient{TMessage},TToken)"/> method.
22+ /// This class is needed to avoid forcing the initialization code in the static constructor to run as soon as
23+ /// the <see cref="MessengerExtensions"/> type is referenced, even if that is done just to use methods
24+ /// that do not actually require this <see cref="MethodInfo"/> instance to be available.
25+ /// We're effectively using this type to leverage the lazy loading of static constructors done by the runtime.
2126 /// </summary>
22- private static readonly MethodInfo RegisterIRecipientMethodInfo ;
27+ private static class MethodInfos
28+ {
29+ /// <summary>
30+ /// Initializes static members of the <see cref="MethodInfos"/> class.
31+ /// </summary>
32+ static MethodInfos ( )
33+ {
34+ RegisterIRecipient = (
35+ from methodInfo in typeof ( MessengerExtensions ) . GetMethods ( )
36+ where methodInfo . Name == nameof ( Register ) &&
37+ methodInfo . IsGenericMethod &&
38+ methodInfo . GetGenericArguments ( ) . Length == 2
39+ let parameters = methodInfo . GetParameters ( )
40+ where parameters . Length == 3 &&
41+ parameters [ 1 ] . ParameterType . IsGenericType &&
42+ parameters [ 1 ] . ParameterType . GetGenericTypeDefinition ( ) == typeof ( IRecipient < > )
43+ select methodInfo ) . First ( ) ;
44+ }
45+
46+ /// <summary>
47+ /// The <see cref="MethodInfo"/> instance associated with <see cref="Register{TMessage,TToken}(IMessenger,IRecipient{TMessage},TToken)"/>.
48+ /// </summary>
49+ public static readonly MethodInfo RegisterIRecipient ;
50+ }
2351
2452 /// <summary>
2553 /// A class that acts as a static container to associate a <see cref="ConditionalWeakTable{TKey,TValue}"/> instance to each
@@ -39,23 +67,6 @@ public static readonly ConditionalWeakTable<Type, Action<IMessenger, object, TTo
3967 = new ConditionalWeakTable < Type , Action < IMessenger , object , TToken > [ ] > ( ) ;
4068 }
4169
42- /// <summary>
43- /// Initializes static members of the <see cref="MessengerExtensions"/> class.
44- /// </summary>
45- static MessengerExtensions ( )
46- {
47- RegisterIRecipientMethodInfo = (
48- from methodInfo in typeof ( MessengerExtensions ) . GetMethods ( )
49- where methodInfo . Name == nameof ( Register ) &&
50- methodInfo . IsGenericMethod &&
51- methodInfo . GetGenericArguments ( ) . Length == 2
52- let parameters = methodInfo . GetParameters ( )
53- where parameters . Length == 3 &&
54- parameters [ 1 ] . ParameterType . IsGenericType &&
55- parameters [ 1 ] . ParameterType . GetGenericTypeDefinition ( ) == typeof ( IRecipient < > )
56- select methodInfo ) . First ( ) ;
57- }
58-
5970 /// <summary>
6071 /// Checks whether or not a given recipient has already been registered for a message.
6172 /// </summary>
@@ -110,7 +121,7 @@ from interfaceType in type.GetInterfaces()
110121 where interfaceType . IsGenericType &&
111122 interfaceType . GetGenericTypeDefinition ( ) == typeof ( IRecipient < > )
112123 let messageType = interfaceType . GenericTypeArguments [ 0 ]
113- let registrationMethod = RegisterIRecipientMethodInfo . MakeGenericMethod ( messageType , typeof ( TToken ) )
124+ let registrationMethod = MethodInfos . RegisterIRecipient . MakeGenericMethod ( messageType , typeof ( TToken ) )
114125 let registrationAction = GetRegistrationAction ( type , registrationMethod )
115126 select registrationAction ) . ToArray ( ) ;
116127 }
0 commit comments