CommunityToolkit / CommunityToolkit/dotnet
Missing Collection Expressions support in attributes passed to the property
- Dominant language
- C#
- Stars
- 3.8k
- Forks
- 400
- PR merge metrics
- No merged PRs in 30d
Description
### 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:
```csharp
[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
```csharp
Parameters = new[] { 1, 2, 3 }
```
to
```csharp
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
Contributor guide
Assessment
This issue has not been assessed yet.