CommunityToolkit / CommunityToolkit/dotnet

ObservableObjectAttribute does not work on a record

Open
#948 2 comments 1 reaction 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

The purpose of the `ObservableObjectAttribute` is, to add Member of `ObservableObject` to a type, that itself cannot derive from `ObservableObject`.

That is actually the case for record classes‌.

Because `ObservableObject` is a class and not a record class, a record class cannot derive from it.

But if I put `[ObservableObject]` on my `partial record class` there is no source generated.

### Regression

_No response_

### Steps to reproduce

```text
1. Multi-Target: net472 and net6.0 (with LangVersion=12), project Type WinExe with WPF, VS 17.11.0
2. Use the following Code:
namespace Test;

using CommunityToolkit.Mvvm.ComponentModel;

[ObservableObject]
public partial record class MyObservableRecord(string Name)
{
private string _Name = Name;
public string Name
{
get => _Name;
set
{
OnPropertyChanging(nameof(Name));
_Name = value;
OnPropertyChanged(nameof(Name));
}
}
}

3. Compile
4. Error:
> error CS0103: The name 'OnPropertyChanging' does not exist in the current context
> error CS0103: The name 'OnPropertyChanged' does not exist in the current context
5. If you replace `record class` with `class` the same code works.
```

### Expected behavior

The expected behaviour would be, that the methods "OnPropertyChanging" and "OnPropertyChanged" (among others) should be generated and be available at compile time.

### Screenshots

_No response_

### IDE and version

VS 2022

### IDE version

17.11

### Nuget packages

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

### Nuget package version(s)

8.2.2 (but also doesn’t work with 8.3.0)

### Additional context

_No response_

### Help us help you

No, just wanted to report this

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.