CommunityToolkit / CommunityToolkit/dotnet

[MVVM] Don't add default parameter in attribute constructor

Open
#778 1 comment 0 reactions 0 assignees View on GitHub
bug :bug:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### Describe the bug

I have an Attribute, which constructor looks like this:

```csharp
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class CrossValidateAttribute : ValidationAttribute {
public CrossValidateAttribute([CallerMemberName] string? member = null) { }
}
```

The idea is to get the properties name when this gets constructed.
If I put this on a normal Property, e.g.

```csharp
[CrossValidate]
public string Foo { ... }
```

it will work and I get `Foo` as content of `member`.

But, if I put this on an `[ObservableProperty]` the generator will output this:

```csharp
[ObservableProperty]
[CrossValidate]
private string? bar;
```

```csharp
///
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::XXX.CrossValidateAttribute(null)]
public string? Bar { ... }
```

Here you can see the `XXX.CrossValidateAttribute(null)` call, which is not want I want.

Can this be done the way I want it, e.g. `XXX.CrossValidateAttribute()`?
### IDE and version

VS 2022

### IDE version

Version 17.7.6

### 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, I'd like to be assigned to work on this item

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.