Skip to content

Commit 3abc8a9

Browse files
committed
Replace linker suppressions with attributes
1 parent 8528092 commit 3abc8a9

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

CommunityToolkit.Mvvm/ComponentModel/ObservableValidator.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,9 @@ IEnumerable<ValidationResult> GetAllErrors()
545545
"Additionally, due to the usage of validation APIs, the type of the current instance cannot be statically discovered.")]
546546
protected void ValidateAllProperties()
547547
{
548-
#pragma warning disable IL2026
549548
// Fast path that tries to create a delegate from a generated type-specific method. This
550549
// is used to make this method more AOT-friendly and faster, as there is no dynamic code.
550+
[RequiresUnreferencedCode("The type of the current instance cannot be statically discovered.")]
551551
static Action<object> GetValidationAction(Type type)
552552
{
553553
if (type.Assembly.GetType("CommunityToolkit.Mvvm.ComponentModel.__Internals.__ObservableValidatorExtensions") is Type extensionsType &&
@@ -558,7 +558,6 @@ static Action<object> GetValidationAction(Type type)
558558

559559
return GetValidationActionFallback(type);
560560
}
561-
#pragma warning restore IL2026
562561

563562
// Fallback method to create the delegate with a compiled LINQ expression
564563
static Action<object> GetValidationActionFallback(Type type)
@@ -615,7 +614,9 @@ from property in validatableProperties
615614

616615
// Get or compute the cached list of properties to validate. Here we're using a static lambda to ensure the
617616
// delegate is cached by the C# compiler, see the related issue at https://github.com/dotnet/roslyn/issues/5835.
618-
EntityValidatorMap.GetValue(GetType(), static t => GetValidationAction(t))(this);
617+
EntityValidatorMap.GetValue(
618+
GetType(),
619+
[RequiresUnreferencedCode("The type of the current instance cannot be statically discovered.")] static (t) => GetValidationAction(t))(this);
619620
}
620621

621622
/// <summary>

CommunityToolkit.Mvvm/ComponentModel/__Internals/__ObservableValidatorHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.ComponentModel;
7+
using System.Diagnostics.CodeAnalysis;
78

89
namespace CommunityToolkit.Mvvm.ComponentModel.__Internals;
910

@@ -23,10 +24,9 @@ public static class __ObservableValidatorHelper
2324
/// <param name="propertyName">The name of the property to validate.</param>
2425
[EditorBrowsable(EditorBrowsableState.Never)]
2526
[Obsolete("This method is not intended to be called directly by user code")]
27+
[RequiresUnreferencedCode("The type of the current instance cannot be statically discovered.")]
2628
public static void ValidateProperty(ObservableValidator instance, object? value, string propertyName)
2729
{
28-
#pragma warning disable IL2026
2930
instance.ValidateProperty(value, propertyName);
30-
#pragma warning restore IL2026
3131
}
3232
}

CommunityToolkit.Mvvm/Messaging/IMessengerExtensions.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ public static void RegisterAll(this IMessenger messenger, object recipient)
9595
ArgumentNullException.ThrowIfNull(messenger);
9696
ArgumentNullException.ThrowIfNull(recipient);
9797

98-
#pragma warning disable IL2026
9998
// We use this method as a callback for the conditional weak table, which will handle
10099
// thread-safety for us. This first callback will try to find a generated method for the
101100
// target recipient type, and just invoke it to get the delegate to cache and use later.
101+
[RequiresUnreferencedCode("The type of the current instance cannot be statically discovered.")]
102102
static Action<IMessenger, object>? LoadRegistrationMethodsForType(Type recipientType)
103103
{
104104
if (recipientType.Assembly.GetType("CommunityToolkit.Mvvm.Messaging.__Internals.__IMessengerExtensions") is Type extensionsType &&
@@ -109,12 +109,11 @@ public static void RegisterAll(this IMessenger messenger, object recipient)
109109

110110
return null;
111111
}
112-
#pragma warning restore IL2026
113112

114113
// Try to get the cached delegate, if the generator has run correctly
115114
Action<IMessenger, object>? registrationAction = DiscoveredRecipients.RegistrationMethods.GetValue(
116115
recipient.GetType(),
117-
static t => LoadRegistrationMethodsForType(t));
116+
[RequiresUnreferencedCode("The type of the current instance cannot be statically discovered.")] static (t) => LoadRegistrationMethodsForType(t));
118117

119118
if (registrationAction is not null)
120119
{
@@ -152,11 +151,11 @@ public static void RegisterAll<TToken>(this IMessenger messenger, object recipie
152151
ArgumentNullException.ThrowIfNull(recipient);
153152
ArgumentNullException.For<TToken>.ThrowIfNull(token);
154153

155-
#pragma warning disable IL2026
156154
// We use this method as a callback for the conditional weak table, which will handle
157155
// thread-safety for us. This first callback will try to find a generated method for the
158156
// target recipient type, and just invoke it to get the delegate to cache and use later.
159157
// In this case we also need to create a generic instantiation of the target method first.
158+
[RequiresUnreferencedCode("The type of the current instance cannot be statically discovered.")]
160159
static Action<IMessenger, object, TToken> LoadRegistrationMethodsForType(Type recipientType)
161160
{
162161
if (recipientType.Assembly.GetType("CommunityToolkit.Mvvm.Messaging.__Internals.__IMessengerExtensions") is Type extensionsType &&
@@ -224,7 +223,6 @@ from registrationMethod in registrationMethods
224223

225224
return Expression.Lambda<Action<IMessenger, object, TToken>>(body, arg0, arg1, arg2).Compile();
226225
}
227-
#pragma warning restore IL2026
228226

229227
// Get or compute the registration method for the current recipient type.
230228
// As in CommunityToolkit.Diagnostics.TypeExtensions.ToTypeString, we use a lambda
@@ -233,7 +231,7 @@ from registrationMethod in registrationMethods
233231
// For more info on this, see the related issue at https://github.com/dotnet/roslyn/issues/5835.
234232
Action<IMessenger, object, TToken> registrationAction = DiscoveredRecipients<TToken>.RegistrationMethods.GetValue(
235233
recipient.GetType(),
236-
static t => LoadRegistrationMethodsForType(t));
234+
[RequiresUnreferencedCode("The type of the current instance cannot be statically discovered.")] static (t) => LoadRegistrationMethodsForType(t));
237235

238236
// Invoke the cached delegate to actually execute the message registration
239237
registrationAction(messenger, recipient, token);

0 commit comments

Comments
 (0)