Skip to content

Commit 32a17cf

Browse files
committed
Add AlsoBroadcastChangeAttribute type
1 parent 850bc2f commit 32a17cf

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Diagnostics;
7+
8+
namespace CommunityToolkit.Mvvm.ComponentModel;
9+
10+
/// <summary>
11+
/// An attribute that can be used to support <see cref="ObservablePropertyAttribute"/> in generated properties, when applied to fields
12+
/// contained in a type that is either inheriting from <see cref="ObservableRecipient"/>, or annotated with <see cref="ObservableRecipientAttribute"/>.
13+
/// When this attribute is used, the generated property setter will also call <see cref="ObservableRecipient.Broadcast{T}(T, T, string?)"/>.
14+
/// This allows generated properties to opt-in into broadcasting behavior without having to fallback into a full explicit observable property.
15+
/// <para>
16+
/// This attribute can be used as follows:
17+
/// <code>
18+
/// partial class MyViewModel : ObservableRecipient
19+
/// {
20+
/// [ObservableProperty]
21+
/// [AlsoBroadcastChange]
22+
/// private string username;
23+
/// }
24+
/// </code>
25+
/// </para>
26+
/// And with this, code analogous to this will be generated:
27+
/// <code>
28+
/// partial class MyViewModel
29+
/// {
30+
/// public string Username
31+
/// {
32+
/// get => username;
33+
/// set => SetProperty(ref username, value, broadcast: true);
34+
/// }
35+
/// }
36+
/// </code>
37+
/// </summary>
38+
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
39+
[Conditional("MVVMTOOLKIT_KEEP_SOURCE_GENERATOR_ATTRIBUTES")]
40+
public sealed class AlsoBroadcastChangeAttribute : Attribute
41+
{
42+
}

tests/CommunityToolkit.Mvvm.KeepSourceGeneratorAttributes.UnitTests/Test_SourceGeneratorAttributes.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void Test_AttributeMetadata()
2121
Assert.Fail();
2222
#endif
2323
Assert.AreEqual(3, typeof(TestModel).GetCustomAttributes().Count(a => a.GetType().FullName!.Contains("CommunityToolkit.Mvvm")));
24-
Assert.AreEqual(3, typeof(TestModel).GetField(nameof(TestModel.name))!.GetCustomAttributes().Count(a => a.GetType().FullName!.Contains("CommunityToolkit.Mvvm")));
24+
Assert.AreEqual(4, typeof(TestModel).GetField(nameof(TestModel.name))!.GetCustomAttributes().Count(a => a.GetType().FullName!.Contains("CommunityToolkit.Mvvm")));
2525
Assert.AreEqual(1, typeof(TestModel).GetMethod(nameof(TestModel.Test))!.GetCustomAttributes().Count(a => a.GetType().FullName!.Contains("CommunityToolkit.Mvvm")));
2626
}
2727

@@ -33,6 +33,7 @@ public class TestModel
3333
[ObservableProperty]
3434
[AlsoNotifyChangeFor("")]
3535
[AlsoNotifyCanExecuteFor("")]
36+
[AlsoBroadcastChange]
3637
public string? name;
3738

3839
[ICommand]

0 commit comments

Comments
 (0)