Skip to content

Commit 157436d

Browse files
authored
Merge pull request #275 from CommunityToolkit/dev/improve-command-generator-with-inheritance
Fix ICommand generator with inherited CanExecute members
2 parents f1f253e + 138f67d commit 157436d

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

CommunityToolkit.Mvvm.SourceGenerators/Extensions/INamedTypeSymbolExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static IEnumerable<ISymbol> GetAllMembers(this INamedTypeSymbol symbol)
6767
/// <returns>A sequence of all member symbols for <paramref name="symbol"/>.</returns>
6868
public static IEnumerable<ISymbol> GetAllMembers(this INamedTypeSymbol symbol, string name)
6969
{
70-
for (INamedTypeSymbol? currentSymbol = symbol; currentSymbol is not null; currentSymbol = currentSymbol.BaseType)
70+
for (INamedTypeSymbol? currentSymbol = symbol; currentSymbol is { SpecialType: not SpecialType.System_Object }; currentSymbol = currentSymbol.BaseType)
7171
{
7272
foreach (ISymbol memberSymbol in currentSymbol.GetMembers(name))
7373
{

CommunityToolkit.Mvvm.SourceGenerators/Input/ICommandGenerator.Execute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ private static bool TryGetCanExecuteExpressionType(
630630
goto Failure;
631631
}
632632

633-
ImmutableArray<ISymbol> canExecuteSymbols = methodSymbol.ContainingType!.GetMembers(memberName);
633+
ImmutableArray<ISymbol> canExecuteSymbols = methodSymbol.ContainingType!.GetAllMembers(memberName).ToImmutableArray();
634634

635635
if (canExecuteSymbols.IsEmpty)
636636
{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 CommunityToolkit.Mvvm.ComponentModel;
6+
7+
namespace CommunityToolkit.Mvvm.ExternalAssembly;
8+
9+
/// <summary>
10+
/// Test viewmodel for https://github.com/CommunityToolkit/dotnet/issues/222.
11+
/// </summary>
12+
public abstract partial class ModelWithObservablePropertyAndMethod : ObservableObject
13+
{
14+
[ObservableProperty]
15+
private bool canSave;
16+
17+
/// <summary>
18+
/// Base method to then generate a command.
19+
/// </summary>
20+
public abstract void Save();
21+
}

tests/CommunityToolkit.Mvvm.UnitTests/Test_ObservablePropertyAttribute.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,24 @@ public void Test_ObservableProperty_ModelWithObservablePropertyInRootNamespace()
747747
CollectionAssert.AreEqual(propertyNames, new[] { nameof(model.Number) });
748748
}
749749

750+
// See https://github.com/CommunityToolkit/dotnet/issues/272
751+
[TestMethod]
752+
public void Test_ObservableProperty_WithCommandReferencingGeneratedPropertyFromOtherAssembly()
753+
{
754+
ModelWithOverriddenCommandMethodFromExternalBaseModel model = new();
755+
756+
Assert.IsFalse(model.HasSaved);
757+
Assert.IsFalse(model.SaveCommand.CanExecute(null));
758+
759+
model.CanSave = true;
760+
761+
Assert.IsTrue(model.SaveCommand.CanExecute(null));
762+
763+
model.SaveCommand.Execute(null);
764+
765+
Assert.IsTrue(model.HasSaved);
766+
}
767+
750768
public abstract partial class BaseViewModel : ObservableObject
751769
{
752770
public string? Content { get; set; }
@@ -1175,4 +1193,15 @@ public partial class ModelWithAdditionalDataAnnotationAttributes : ObservableVal
11751193
[ScaffoldColumn(true)]
11761194
private string? name;
11771195
}
1196+
1197+
public partial class ModelWithOverriddenCommandMethodFromExternalBaseModel : ModelWithObservablePropertyAndMethod
1198+
{
1199+
public bool HasSaved { get; private set; }
1200+
1201+
[ICommand(CanExecute = nameof(CanSave))]
1202+
public override void Save()
1203+
{
1204+
HasSaved = true;
1205+
}
1206+
}
11781207
}

0 commit comments

Comments
 (0)