You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
boolisNotifyPropertyChanging=fieldSymbol.ContainingType.AllInterfaces.Any(static i =>i.HasFullyQualifiedName("global::System.ComponentModel.INotifyPropertyChanging"));
41
-
boolhasObservableObjectAttribute=fieldSymbol.ContainingType.GetAttributes().Any(static a =>a.AttributeClass?.HasFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute")==true);
@@ -124,6 +132,30 @@ internal static class Execute
124
132
validationAttributes.ToImmutable());
125
133
}
126
134
135
+
/// <summary>
136
+
/// Validates the containing type for a given field being annotated.
137
+
/// </summary>
138
+
/// <param name="fieldSymbol">The input <see cref="IFieldSymbol"/> instance to process.</param>
139
+
/// <param name="shouldInvokeOnPropertyChanging">Whether or not property changing events should also be raised.</param>
140
+
/// <returns>Whether or not the containing type for <paramref name="fieldSymbol"/> is valid.</returns>
141
+
privatestaticboolIsTargetTypeValid(
142
+
IFieldSymbolfieldSymbol,
143
+
outboolshouldInvokeOnPropertyChanging)
144
+
{
145
+
// The [ObservableProperty] attribute can only be used in types that are known to expose the necessary OnPropertyChanged and OnPropertyChanging methods.
146
+
// That means that the containing type for the field needs to match one of the following conditions:
147
+
// - It inherits from ObservableObject (in which case it also implements INotifyPropertyChanging).
148
+
// - It has the [ObservableObject] attribute (on itself or any of its base types).
149
+
// - It has the [INotifyPropertyChanged] attribute (on itself or any of its base types).
description:$"The name of fields annotated with [ObservableProperty] should use \"lowerCamel\", \"_lowerCamel\" or \"m_lowerCamel\" pattern to avoid collisions with the generated properties.",
236
+
description:"The name of fields annotated with [ObservableProperty] should use \"lowerCamel\", \"_lowerCamel\" or \"m_lowerCamel\" pattern to avoid collisions with the generated properties.",
237
237
helpLinkUri:"https://aka.ms/mvvmtoolkit");
238
238
239
239
/// <summary>
@@ -281,7 +281,7 @@ internal static class DiagnosticDescriptors
description:$"Cannot apply [INotifyPropertyChanged] to a type that already has this attribute or [ObservableObject] applied to it (including base types).",
284
+
description:"Cannot apply [INotifyPropertyChanged] to a type that already has this attribute or [ObservableObject] applied to it (including base types).",
285
285
helpLinkUri:"https://aka.ms/mvvmtoolkit");
286
286
287
287
/// <summary>
@@ -297,6 +297,22 @@ internal static class DiagnosticDescriptors
description:$"Cannot apply [ObservableObject] to a type that already has this attribute or [INotifyPropertyChanged] applied to it (including base types).",
300
+
description:"Cannot apply [ObservableObject] to a type that already has this attribute or [INotifyPropertyChanged] applied to it (including base types).",
301
+
helpLinkUri:"https://aka.ms/mvvmtoolkit");
302
+
303
+
/// <summary>
304
+
/// Gets a <see cref="DiagnosticDescriptor"/> indicating when <c>[ObservableProperty]</c> is applied to a field in an invalid type.
305
+
/// <para>
306
+
/// Format: <c>"The field {0}.{1} cannot be used to generate an observable property, as its containing type doesn't inherit from ObservableObject, nor does it use [ObservableObject] or [INotifyPropertyChanged]"</c>.
title:$"Invalid containing type for [ObservableProperty] field",
312
+
messageFormat:$"The field {{0}}.{{1}} cannot be used to generate an observable property, as its containing type doesn't inherit from ObservableObject, nor does it use [ObservableObject] or [INotifyPropertyChanged]",
description:"Fields annotated with [ObservableProperty] must be contained in a type that inherits from ObservableObject or that is annotated with [ObservableObject] or [INotifyPropertyChanged] (including base types).",
0 commit comments