Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ This documentation covers using ReactiveUI Source Generators to simplify and enh
ReactiveUI Source Generators automatically generate ReactiveUI objects to streamline your code. These Source Generators are designed to work with ReactiveUI V19.5.31+ and support the following features:

- `[Reactive]` With field and access modifiers, partial property support (C# 13 Visual Studio Version 17.12.0)
- `[ObservableAsProperty]`
- `[ObservableAsProperty]` With field, method, Observable property and partial property support (C# 13 Visual Studio Version 17.12.0)
- `[ObservableAsProperty(ReadOnly = false)]` Removes readonly keyword from the generated helper field
- `[ObservableAsProperty(PropertyName = "ReadOnlyPropertyName")]`
- `[ObservableAsProperty(InitialValue = "Default Value")]` Only valid for partial properties using (C# 13 Visual Studio Version 17.12.0)
- `[ReactiveCommand]`
- `[ReactiveCommand(CanExecute = nameof(IObservableBoolName))]` with CanExecute
- `[ReactiveCommand(OutputScheduler = "RxApp.MainThreadScheduler")]` using a ReactiveUI Scheduler
Expand Down Expand Up @@ -296,6 +298,26 @@ public partial class MyReactiveClass : ReactiveObject
}
```

### Usage ObservableAsPropertyHelper with partial Property and a default value
```csharp
using ReactiveUI.SourceGenerators;

public partial class MyReactiveClass : ReactiveObject
{
public MyReactiveClass()
{
// The value of MyProperty will be "Default Value" until the Observable is initialized
_myPrpertyHelper = MyPropertyObservable()
.ToProperty(this, nameof(MyProperty));
}

[ObservableAsProperty(InitialValue = "Default Value")]
public string MyProperty { get; }

public IObservable<string> MyPropertyObservable() => Observable.Return("Test Value");
}
```

## Usage ReactiveCommand `[ReactiveCommand]`

### Usage ReactiveCommand without parameter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//HintName: ReactiveUI.SourceGenerators.ObservableAsPropertyAttribute.g.cs
// Copyright (c) 2025 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System;

// <auto-generated/>
#pragma warning disable
#nullable enable
namespace ReactiveUI.SourceGenerators;

/// <summary>
/// ObservableAsPropertyAttribute.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
internal sealed class ObservableAsPropertyAttribute : Attribute
{
/// <summary>
/// Gets the name of the property.
/// </summary>
/// <value>
/// The name of the property.
/// </value>
public string? PropertyName { get; init; }

/// <summary>
/// Gets the Readonly state of the OAPH property.
/// </summary>
/// <value>
/// The is read only of the OAPH property.
/// </value>
public bool ReadOnly { get; init; } = true;

/// <summary>
/// Gets the AccessModifier of the OAPH property.
/// </summary>
/// <value>
/// The AccessModifier of the OAPH property, protected if true.
/// </value>
public bool UseProtected { get; init; } = false;

/// <summary>
/// Gets the Initial value of the OAPH property.
/// </summary>
/// <value>
/// The initial value of the OAPH property.
/// </value>
public string? InitialValue { get; init; }
}
#nullable restore
#pragma warning restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//HintName: TestNs.TestVM.ObservableAsProperties.g.cs
// <auto-generated/>
#pragma warning disable
#nullable enable
namespace TestNs
{
/// <summary>
/// Partial class for the TestVM which contains ReactiveUI Observable As Property initialization.
/// </summary>
public partial class TestVM
{
/// <inheritdoc cref="_testPropertyHelper"/>
private readonly ReactiveUI.ObservableAsPropertyHelper<double?> _testPropertyHelper;

/// <inheritdoc cref="_testProperty"/>
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Runtime.Serialization.DataMemberAttribute()]
[global::System.Text.Json.Serialization.JsonIncludeAttribute()]
public double? TestProperty { get => _testProperty = (_testPropertyHelper == null ? _testProperty : _testPropertyHelper.Value); }
}
}
#nullable restore
#pragma warning restore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ internal sealed class ObservableAsPropertyAttribute : Attribute
/// The AccessModifier of the OAPH property, protected if true.
/// </value>
public bool UseProtected { get; init; } = false;

/// <summary>
/// Gets the Initial value of the OAPH property.
/// </summary>
/// <value>
/// The initial value of the OAPH property.
/// </value>
public string? InitialValue { get; init; }
}
#nullable restore
#pragma warning restore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ internal sealed class ObservableAsPropertyAttribute : Attribute
/// The AccessModifier of the OAPH property, protected if true.
/// </value>
public bool UseProtected { get; init; } = false;

/// <summary>
/// Gets the Initial value of the OAPH property.
/// </summary>
/// <value>
/// The initial value of the OAPH property.
/// </value>
public string? InitialValue { get; init; }
}
#nullable restore
#pragma warning restore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ internal sealed class ObservableAsPropertyAttribute : Attribute
/// The AccessModifier of the OAPH property, protected if true.
/// </value>
public bool UseProtected { get; init; } = false;

/// <summary>
/// Gets the Initial value of the OAPH property.
/// </summary>
/// <value>
/// The initial value of the OAPH property.
/// </value>
public string? InitialValue { get; init; }
}
#nullable restore
#pragma warning restore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ internal sealed class ObservableAsPropertyAttribute : Attribute
/// The AccessModifier of the OAPH property, protected if true.
/// </value>
public bool UseProtected { get; init; } = false;

/// <summary>
/// Gets the Initial value of the OAPH property.
/// </summary>
/// <value>
/// The initial value of the OAPH property.
/// </value>
public string? InitialValue { get; init; }
}
#nullable restore
#pragma warning restore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ internal sealed class ObservableAsPropertyAttribute : Attribute
/// The AccessModifier of the OAPH property, protected if true.
/// </value>
public bool UseProtected { get; init; } = false;

/// <summary>
/// Gets the Initial value of the OAPH property.
/// </summary>
/// <value>
/// The initial value of the OAPH property.
/// </value>
public string? InitialValue { get; init; }
}
#nullable restore
#pragma warning restore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public partial class TestVM

/// <inheritdoc cref="_test5Property"/>
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Runtime.Serialization.DataMemberAttribute()]
[global::System.Text.Json.Serialization.JsonIncludeAttribute()]
public int Test5Property { get => _test5Property = _test5PropertyHelper?.Value ?? _test5Property; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ internal sealed class ObservableAsPropertyAttribute : Attribute
/// The AccessModifier of the OAPH property, protected if true.
/// </value>
public bool UseProtected { get; init; } = false;

/// <summary>
/// Gets the Initial value of the OAPH property.
/// </summary>
/// <value>
/// The initial value of the OAPH property.
/// </value>
public string? InitialValue { get; init; }
}
#nullable restore
#pragma warning restore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public partial class TestVM

/// <inheritdoc cref="_test7Property"/>
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Runtime.Serialization.DataMemberAttribute()]
[global::System.Text.Json.Serialization.JsonIncludeAttribute()]
public object? Test7Property { get => _test7Property = (_test7PropertyHelper == null ? _test7Property : _test7PropertyHelper.Value); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ internal sealed class ObservableAsPropertyAttribute : Attribute
/// The AccessModifier of the OAPH property, protected if true.
/// </value>
public bool UseProtected { get; init; } = false;

/// <summary>
/// Gets the Initial value of the OAPH property.
/// </summary>
/// <value>
/// The initial value of the OAPH property.
/// </value>
public string? InitialValue { get; init; }
}
#nullable restore
#pragma warning restore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public partial class TestVM

/// <inheritdoc cref="_test6Property"/>
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Runtime.Serialization.DataMemberAttribute()]
[global::System.Text.Json.Serialization.JsonIncludeAttribute()]
public object Test6Property { get => _test6Property = _test6PropertyHelper?.Value ?? _test6Property; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ internal sealed class ObservableAsPropertyAttribute : Attribute
/// The AccessModifier of the OAPH property, protected if true.
/// </value>
public bool UseProtected { get; init; } = false;

/// <summary>
/// Gets the Initial value of the OAPH property.
/// </summary>
/// <value>
/// The initial value of the OAPH property.
/// </value>
public string? InitialValue { get; init; }
}
#nullable restore
#pragma warning restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//HintName: ReactiveUI.SourceGenerators.ObservableAsPropertyAttribute.g.cs
// Copyright (c) 2025 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System;

// <auto-generated/>
#pragma warning disable
#nullable enable
namespace ReactiveUI.SourceGenerators;

/// <summary>
/// ObservableAsPropertyAttribute.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
internal sealed class ObservableAsPropertyAttribute : Attribute
{
/// <summary>
/// Gets the name of the property.
/// </summary>
/// <value>
/// The name of the property.
/// </value>
public string? PropertyName { get; init; }

/// <summary>
/// Gets the Readonly state of the OAPH property.
/// </summary>
/// <value>
/// The is read only of the OAPH property.
/// </value>
public bool ReadOnly { get; init; } = true;

/// <summary>
/// Gets the AccessModifier of the OAPH property.
/// </summary>
/// <value>
/// The AccessModifier of the OAPH property, protected if true.
/// </value>
public bool UseProtected { get; init; } = false;

/// <summary>
/// Gets the Initial value of the OAPH property.
/// </summary>
/// <value>
/// The initial value of the OAPH property.
/// </value>
public string? InitialValue { get; init; }
}
#nullable restore
#pragma warning restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//HintName: TestNs.TestVM.ObservableAsPropertyFromObservable.g.cs
// <auto-generated/>
using ReactiveUI;

#pragma warning disable
#nullable enable

namespace TestNs
{
/// <summary>
/// Partial class for the TestVM which contains ReactiveUI Observable As Property initialization.
/// </summary>
public partial class TestVM
{
/// <inheritdoc cref="TestProperty"/>
private double? _testProperty;

/// <inheritdoc cref="_testPropertyHelper"/>
private ReactiveUI.ObservableAsPropertyHelper<double?>? _testPropertyHelper;

/// <inheritdoc cref="_testProperty"/>
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Runtime.Serialization.DataMemberAttribute()]
public partial double? TestProperty { get => _testProperty = _testPropertyHelper?.Value ?? _testProperty; }

[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
protected void InitializeOAPH()
{

}
}
}
#nullable restore
#pragma warning restore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ internal sealed class ObservableAsPropertyAttribute : Attribute
/// The AccessModifier of the OAPH property, protected if true.
/// </value>
public bool UseProtected { get; init; } = false;

/// <summary>
/// Gets the Initial value of the OAPH property.
/// </summary>
/// <value>
/// The initial value of the OAPH property.
/// </value>
public string? InitialValue { get; init; }
}
#nullable restore
#pragma warning restore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ internal sealed class ObservableAsPropertyAttribute : Attribute
/// The AccessModifier of the OAPH property, protected if true.
/// </value>
public bool UseProtected { get; init; } = false;

/// <summary>
/// Gets the Initial value of the OAPH property.
/// </summary>
/// <value>
/// The initial value of the OAPH property.
/// </value>
public string? InitialValue { get; init; }
}
#nullable restore
#pragma warning restore
Loading
Loading