Skip to content

Commit 6188d3d

Browse files
committed
Improved constructor definition for [AlsoNotifyForAttribute] type
1 parent c0d4f00 commit 6188d3d

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,36 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration(GeneratorExec
146146
// Add dependent property notifications, if needed
147147
if (SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, alsoNotifyForAttributeSymbol))
148148
{
149-
foreach (TypedConstant attributeArgument in attributeData.ConstructorArguments[0].Values)
149+
foreach (TypedConstant attributeArgument in attributeData.ConstructorArguments)
150150
{
151-
if (attributeArgument.Value is string dependentPropertyName)
151+
if (attributeArgument.IsNull)
152+
{
153+
continue;
154+
}
155+
156+
if (attributeArgument.Kind == TypedConstantKind.Primitive &&
157+
attributeArgument.Value is string dependentPropertyName)
152158
{
153159
// OnPropertyChanged("OtherPropertyName");
154160
dependentPropertyNotificationStatements.Add(ExpressionStatement(
155161
InvocationExpression(IdentifierName("OnPropertyChanged"))
156162
.AddArgumentListArguments(Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(dependentPropertyName))))));
157163
}
164+
else if (attributeArgument.Kind == TypedConstantKind.Array)
165+
{
166+
foreach (TypedConstant nestedAttributeArgument in attributeArgument.Values)
167+
{
168+
if (nestedAttributeArgument.IsNull)
169+
{
170+
continue;
171+
}
172+
173+
// Additional property names
174+
dependentPropertyNotificationStatements.Add(ExpressionStatement(
175+
InvocationExpression(IdentifierName("OnPropertyChanged"))
176+
.AddArgumentListArguments(Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal((string)nestedAttributeArgument.Value!))))));
177+
}
178+
}
158179
}
159180
}
160181
else if (validationAttributeSymbol is not null &&

Microsoft.Toolkit.Mvvm/ComponentModel/Attributes/AlsoNotifyForAttribute.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using System;
88
using System.ComponentModel;
9+
using System.Linq;
910

1011
namespace Microsoft.Toolkit.Mvvm.ComponentModel
1112
{
@@ -68,10 +69,23 @@ public sealed class AlsoNotifyForAttribute : Attribute
6869
/// <summary>
6970
/// Initializes a new instance of the <see cref="AlsoNotifyForAttribute"/> class.
7071
/// </summary>
71-
/// <param name="propertyNames">The property names to also notify when the annotated property changes.</param>
72-
public AlsoNotifyForAttribute(params string[] propertyNames)
72+
/// <param name="propertyName">The name of the property to also notify when the annotated property changes.</param>
73+
public AlsoNotifyForAttribute(string propertyName)
7374
{
74-
PropertyNames = propertyNames;
75+
PropertyNames = new[] { propertyName };
76+
}
77+
78+
/// <summary>
79+
/// Initializes a new instance of the <see cref="AlsoNotifyForAttribute"/> class.
80+
/// </summary>
81+
/// <param name="propertyName">The name of the property to also notify when the annotated property changes.</param>
82+
/// <param name="otherPropertyNames">
83+
/// The other property names to also notify when the annotated property changes. This parameter can optionally
84+
/// be used to indicate a series of dependent properties from the same attribute, to keep the code more compact.
85+
/// </param>
86+
public AlsoNotifyForAttribute(string propertyName, string[] otherPropertyNames)
87+
{
88+
PropertyNames = new[] { propertyName }.Concat(otherPropertyNames).ToArray();
7589
}
7690

7791
/// <summary>

0 commit comments

Comments
 (0)