dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

`RelayCommandAttribute` can't be used with interface and generic class

Open Dabbel opened this issue 2 years ago • 1 comments

Describe the bug

If you have an interface, without a generic type, that requires the implementation of an ICommand property it isn't possible to use the RelayCommandAttribute on a generic class that uses the generic type as parameter on the command method. The RelayCommandAttribute generates a property of type IRelayCommand<T> that doesn't satisfy the implementation of the interface.

Regression

No response

Steps to reproduce

public interface ITest
{
    ICommand TestSomethingCommand { get; }
}

public partial class Test<T> : ITest
{
    // Generates a property of type IRelayCommand<T>, this doesn't satisfy ITest.ICommand
    [RelayCommand]
    private void TestSomething(T obj)
    {
    }
}

Expected behavior

The generated property should be of type ICommand or RelayCommandAttribute should have an option to generate the property with of type ICommand instead of IRelayCommand<T>.

Screenshots

No response

IDE and version

VS 2022, Rider

IDE version

No response

Nuget packages

  • [ ] CommunityToolkit.Common
  • [ ] CommunityToolkit.Diagnostics
  • [ ] CommunityToolkit.HighPerformance
  • [X] CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

8.2.2

Additional context

No response

Help us help you

Yes, but only if others can assist

Dabbel avatar Jan 02 '24 14:01 Dabbel

That is pretty normal, as command will be declared with type global::CommunityToolkit.Mvvm.Input.IRelayCommand<T> which is not equal to ICommand from your interface. Customization could be a good idea; to be able to choose generated property type.

By changing the generated property type, you will have extra casting if you want to notify about CanExecute changed.

I would say your ticket is more like C# language feature request than precise to MVVM Toolkit.

mklemarczyk avatar Dec 14 '24 12:12 mklemarczyk