Skip to content

[Bug]: Inconsistend code generation for ObservableAsProperty attribute when using it at a field or partial property #184

@DarkCloud14

Description

@DarkCloud14

Describe the bug 🐞

When creating a partial property with ObservableAsProperty attribute in a class which is subclass of ReactiveObject the generated code is different from the code that gets generated when applying the ObservableAsProperty to a field.

Applying the ObservableAsProperty to a field will generate code that gives you a CS8618 warning in the classes custom constructors that the generated helper field must contain a non-null value when exiting constructor.

Applying the ObservableAsProperty to a partial property it will generate code that doesn't give you the CS8618 warning.

Step to reproduce

  1. Create a new .NET9 class library project (with Nullable enabled)
  2. Install the ReactiveUI and ReactiveUI.SourceGenerators packages
  3. Write a simple partial class which is a subclass of ReactiveObject
  4. Add a partial get-only property with [ObservableAsProperty] attribute and a field [ObservableAsProperty] attribute
  5. Add one or more constructors
public partial class TestClass : ReactiveObject
{
    [ObservableAsProperty]
    private bool _observableTestField;

    public TestClass()
    {
    }

    [ObservableAsProperty]
    public partial bool ObservableTestProperty { get; }
}
  1. See that a CS8618 warning is generated for the generated _observableTestFieldHelper field but not for the generated _observableTestPropertyHelper field.

Reproduction repository

https://github.com/DarkCloud14/ReactiveUI_SourceGen_ObservableAsProperty_Issue

Expected behavior

I would expect that in both cases code is generated which would result in a CS8618 warning if the helper field doesn't contain a non-null value when exiting the constructor.

Screenshots 🖼️

No response

IDE

Rider macOS

Operating system

macOS Sequoia 15.3.1

Version

No response

Device

No response

ReactiveUI Version

ReactiveUI 20.1.63, ReactiveUI.SourceGenerators 2.1.1

Additional information ℹ️

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions