@@ -17,9 +17,37 @@ namespace Microsoft.Toolkit.Mvvm.Messaging
17
17
public static partial class MessengerExtensions
18
18
{
19
19
/// <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.
21
26
/// </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
+ }
23
51
24
52
/// <summary>
25
53
/// 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
39
67
= new ConditionalWeakTable < Type , Action < IMessenger , object , TToken > [ ] > ( ) ;
40
68
}
41
69
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
-
59
70
/// <summary>
60
71
/// Checks whether or not a given recipient has already been registered for a message.
61
72
/// </summary>
@@ -110,7 +121,7 @@ from interfaceType in type.GetInterfaces()
110
121
where interfaceType . IsGenericType &&
111
122
interfaceType . GetGenericTypeDefinition ( ) == typeof ( IRecipient < > )
112
123
let messageType = interfaceType . GenericTypeArguments [ 0 ]
113
- let registrationMethod = RegisterIRecipientMethodInfo . MakeGenericMethod ( messageType , typeof ( TToken ) )
124
+ let registrationMethod = MethodInfos . RegisterIRecipient . MakeGenericMethod ( messageType , typeof ( TToken ) )
114
125
let registrationAction = GetRegistrationAction ( type , registrationMethod )
115
126
select registrationAction ) . ToArray ( ) ;
116
127
}
0 commit comments