Skip to content

Commit

Permalink
Fix [ObservableRecipient] with inherited base attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Feb 14, 2022
1 parent 0813779 commit 4ca716f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ protected override bool ValidateTargetType(INamedTypeSymbol typeSymbol, Observab
// In order to use [ObservableRecipient], the target type needs to inherit from ObservableObject,
// or be annotated with [ObservableObject] or [INotifyPropertyChanged] (with additional helpers).
if (!typeSymbol.InheritsFrom("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObject") &&
!typeSymbol.GetAttributes().Any(static a => a.AttributeClass?.HasFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute") == true) &&
!typeSymbol.GetAttributes().Any(static a =>
!typeSymbol.HasOrInheritsAttribute(static a => a.AttributeClass?.HasFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute") == true) &&
!typeSymbol.HasOrInheritsAttribute(static a =>
a.AttributeClass?.HasFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute") == true &&
!a.HasNamedArgument("IncludeAdditionalHelperMethods", false)))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;

Expand Down Expand Up @@ -62,4 +64,23 @@ public static bool InheritsFrom(this INamedTypeSymbol typeSymbol, string name)

return false;
}

/// <summary>
/// Checks whether or not a given <see cref="INamedTypeSymbol"/> has or inherits a specified attribute.
/// </summary>
/// <param name="typeSymbol">The target <see cref="INamedTypeSymbol"/> instance to check.</param>
/// <param name="predicate">The predicate used to match available attributes.</param>
/// <returns>Whether or not <paramref name="typeSymbol"/> has an attribute matching <paramref name="predicate"/>.</returns>
public static bool HasOrInheritsAttribute(this INamedTypeSymbol typeSymbol, Func<AttributeData, bool> predicate)
{
for (INamedTypeSymbol? currentType = typeSymbol; currentType is not null; currentType = currentType.BaseType)
{
if (currentType.GetAttributes().Any(predicate))
{
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,25 @@ private partial void OnDeactivated()
OnDeactivatedResult = true;
}
}

// See https://github.com/CommunityToolkit/dotnet/issues/109
[TestMethod]
public void Test_ObservableRecipientAttribute_WorksWithBaseClassWithObservableObjectAttribute()
{
ViewModelWithOnlyObservableRecipientAttribute model = new();

// This test method really only needs the two classes below to compile at all
Assert.IsTrue(model is INotifyPropertyChanged); // From [ObservableObject]
Assert.IsFalse(model.IsActive); // From [ObservableRecipient]
}

[ObservableObject]
public partial class BaseViewModelWithObservableObjectAttribute
{
}

[ObservableRecipient]
public partial class ViewModelWithOnlyObservableRecipientAttribute : BaseViewModelWithObservableObjectAttribute
{
}
}

0 comments on commit 4ca716f

Please sign in to comment.