dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

Missing Collection Expressions support in attributes passed to the property

Open n-coelho-cerinnov opened this issue 2 years ago • 0 comments

Describe the bug

The new collection expression syntax that is going to be introduced in .NET 8.0 is not supported in attributes marked 'property' (passed from the field to the property). Because .NET 8.0 has new code refactoring suggestions it asks to change a classic array initialization like new[] { 1, 2, 3 } to [1, 2, 3] (compiler suggestion IDE0300). If the code is changed the compiler complains with MVVMTK0037: The field annotated with [ObservableProperty] is using attribute with an invalid expression (are you passing any incorrect parameters to the attribute constructor?)

Regression

No response

Steps to reproduce

To reproduce this bug, using the latest version of CommunityToolkit.Mvvm (8.2.1), create a .NET 8.0 app with the following code:

[AttributeUsage(AttributeTargets.Property)]
public class ArrayAttribute : Attribute
{
    public int[]? Parameters { get; set; }
}

public partial class ViewModel : ObservableObject
{
    [ObservableProperty]
    [property: Array(Parameters = new[] { 1, 2, 3 })]
    private int _test;
}

Changing

Parameters = new[] { 1, 2, 3 }

to

Parameters = [1, 2, 3]

causes the error.

Expected behavior

The attribute should be passed to the property without causing any errors.

Screenshots

No response

IDE and version

VS 2022 Preview

IDE version

17.8.0 Preview 3.0

Nuget packages

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

Nuget package version(s)

8.2.1

Additional context

I've been experimenting with source generators and I had to resolve a similar situation in one of my source generators. I had to verify if the attribute argument expression is of the new CollectionExpressionSyntax type, so I guess this problem is related to that.

Help us help you

No, just wanted to report this

n-coelho-cerinnov avatar Oct 12 '23 15:10 n-coelho-cerinnov