Skip to content

Commit 967f2bb

Browse files
authored
Add OAPH From Partial Property (#174)
* Add OAPH From Partial Property * Update Readme, update version for release
1 parent 64c4c6d commit 967f2bb

File tree

36 files changed

+740
-421
lines changed

36 files changed

+740
-421
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ This documentation covers using ReactiveUI Source Generators to simplify and enh
1212
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:
1313

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

301+
### Usage ObservableAsPropertyHelper with partial Property and a default value
302+
```csharp
303+
using ReactiveUI.SourceGenerators;
304+
305+
public partial class MyReactiveClass : ReactiveObject
306+
{
307+
public MyReactiveClass()
308+
{
309+
// The value of MyProperty will be "Default Value" until the Observable is initialized
310+
_myPrpertyHelper = MyPropertyObservable()
311+
.ToProperty(this, nameof(MyProperty));
312+
}
313+
314+
[ObservableAsProperty(InitialValue = "Default Value")]
315+
public string MyProperty { get; }
316+
317+
public IObservable<string> MyPropertyObservable() => Observable.Return("Test Value");
318+
}
319+
```
320+
299321
## Usage ReactiveCommand `[ReactiveCommand]`
300322

301323
### Usage ReactiveCommand without parameter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//HintName: ReactiveUI.SourceGenerators.ObservableAsPropertyAttribute.g.cs
2+
// Copyright (c) 2025 .NET Foundation and Contributors. All rights reserved.
3+
// Licensed to the .NET Foundation under one or more agreements.
4+
// The .NET Foundation licenses this file to you under the MIT license.
5+
// See the LICENSE file in the project root for full license information.
6+
7+
using System;
8+
9+
// <auto-generated/>
10+
#pragma warning disable
11+
#nullable enable
12+
namespace ReactiveUI.SourceGenerators;
13+
14+
/// <summary>
15+
/// ObservableAsPropertyAttribute.
16+
/// </summary>
17+
/// <seealso cref="Attribute" />
18+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
19+
internal sealed class ObservableAsPropertyAttribute : Attribute
20+
{
21+
/// <summary>
22+
/// Gets the name of the property.
23+
/// </summary>
24+
/// <value>
25+
/// The name of the property.
26+
/// </value>
27+
public string? PropertyName { get; init; }
28+
29+
/// <summary>
30+
/// Gets the Readonly state of the OAPH property.
31+
/// </summary>
32+
/// <value>
33+
/// The is read only of the OAPH property.
34+
/// </value>
35+
public bool ReadOnly { get; init; } = true;
36+
37+
/// <summary>
38+
/// Gets the AccessModifier of the OAPH property.
39+
/// </summary>
40+
/// <value>
41+
/// The AccessModifier of the OAPH property, protected if true.
42+
/// </value>
43+
public bool UseProtected { get; init; } = false;
44+
45+
/// <summary>
46+
/// Gets the Initial value of the OAPH property.
47+
/// </summary>
48+
/// <value>
49+
/// The initial value of the OAPH property.
50+
/// </value>
51+
public string? InitialValue { get; init; }
52+
}
53+
#nullable restore
54+
#pragma warning restore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//HintName: TestNs.TestVM.ObservableAsProperties.g.cs
2+
// <auto-generated/>
3+
#pragma warning disable
4+
#nullable enable
5+
namespace TestNs
6+
{
7+
/// <summary>
8+
/// Partial class for the TestVM which contains ReactiveUI Observable As Property initialization.
9+
/// </summary>
10+
public partial class TestVM
11+
{
12+
/// <inheritdoc cref="_testPropertyHelper"/>
13+
private readonly ReactiveUI.ObservableAsPropertyHelper<double?> _testPropertyHelper;
14+
15+
/// <inheritdoc cref="_testProperty"/>
16+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
17+
[global::System.Runtime.Serialization.DataMemberAttribute()]
18+
[global::System.Text.Json.Serialization.JsonIncludeAttribute()]
19+
public double? TestProperty { get => _testProperty = (_testPropertyHelper == null ? _testProperty : _testPropertyHelper.Value); }
20+
}
21+
}
22+
#nullable restore
23+
#pragma warning restore

src/ReactiveUI.SourceGenerator.Tests/OAPH/OAPFromObservableGeneratorTests.FromObservableMethods#ReactiveUI.SourceGenerators.ObservableAsPropertyAttribute.g.verified.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ internal sealed class ObservableAsPropertyAttribute : Attribute
4141
/// The AccessModifier of the OAPH property, protected if true.
4242
/// </value>
4343
public bool UseProtected { get; init; } = false;
44+
45+
/// <summary>
46+
/// Gets the Initial value of the OAPH property.
47+
/// </summary>
48+
/// <value>
49+
/// The initial value of the OAPH property.
50+
/// </value>
51+
public string? InitialValue { get; init; }
4452
}
4553
#nullable restore
4654
#pragma warning restore

src/ReactiveUI.SourceGenerator.Tests/OAPH/OAPFromObservableGeneratorTests.FromObservableMethodsWithName#ReactiveUI.SourceGenerators.ObservableAsPropertyAttribute.g.verified.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ internal sealed class ObservableAsPropertyAttribute : Attribute
4141
/// The AccessModifier of the OAPH property, protected if true.
4242
/// </value>
4343
public bool UseProtected { get; init; } = false;
44+
45+
/// <summary>
46+
/// Gets the Initial value of the OAPH property.
47+
/// </summary>
48+
/// <value>
49+
/// The initial value of the OAPH property.
50+
/// </value>
51+
public string? InitialValue { get; init; }
4452
}
4553
#nullable restore
4654
#pragma warning restore

src/ReactiveUI.SourceGenerator.Tests/OAPH/OAPFromObservableGeneratorTests.FromObservableProp#ReactiveUI.SourceGenerators.ObservableAsPropertyAttribute.g.verified.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ internal sealed class ObservableAsPropertyAttribute : Attribute
4141
/// The AccessModifier of the OAPH property, protected if true.
4242
/// </value>
4343
public bool UseProtected { get; init; } = false;
44+
45+
/// <summary>
46+
/// Gets the Initial value of the OAPH property.
47+
/// </summary>
48+
/// <value>
49+
/// The initial value of the OAPH property.
50+
/// </value>
51+
public string? InitialValue { get; init; }
4452
}
4553
#nullable restore
4654
#pragma warning restore

src/ReactiveUI.SourceGenerator.Tests/OAPH/OAPFromObservableGeneratorTests.FromObservablePropNestedClasses#ReactiveUI.SourceGenerators.ObservableAsPropertyAttribute.g.verified.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ internal sealed class ObservableAsPropertyAttribute : Attribute
4141
/// The AccessModifier of the OAPH property, protected if true.
4242
/// </value>
4343
public bool UseProtected { get; init; } = false;
44+
45+
/// <summary>
46+
/// Gets the Initial value of the OAPH property.
47+
/// </summary>
48+
/// <value>
49+
/// The initial value of the OAPH property.
50+
/// </value>
51+
public string? InitialValue { get; init; }
4452
}
4553
#nullable restore
4654
#pragma warning restore

src/ReactiveUI.SourceGenerator.Tests/OAPH/OAPFromObservableGeneratorTests.FromObservablePropertiesWithAttribute#ReactiveUI.SourceGenerators.ObservableAsPropertyAttribute.g.verified.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ internal sealed class ObservableAsPropertyAttribute : Attribute
4141
/// The AccessModifier of the OAPH property, protected if true.
4242
/// </value>
4343
public bool UseProtected { get; init; } = false;
44+
45+
/// <summary>
46+
/// Gets the Initial value of the OAPH property.
47+
/// </summary>
48+
/// <value>
49+
/// The initial value of the OAPH property.
50+
/// </value>
51+
public string? InitialValue { get; init; }
4452
}
4553
#nullable restore
4654
#pragma warning restore

src/ReactiveUI.SourceGenerator.Tests/OAPH/OAPFromObservableGeneratorTests.FromObservablePropertiesWithAttribute#TestNs.TestVM.ObservableAsPropertyFromObservable.g.verified.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public partial class TestVM
2020

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

src/ReactiveUI.SourceGenerator.Tests/OAPH/OAPFromObservableGeneratorTests.FromObservablePropertiesWithAttributeNullableRef#ReactiveUI.SourceGenerators.ObservableAsPropertyAttribute.g.verified.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ internal sealed class ObservableAsPropertyAttribute : Attribute
4141
/// The AccessModifier of the OAPH property, protected if true.
4242
/// </value>
4343
public bool UseProtected { get; init; } = false;
44+
45+
/// <summary>
46+
/// Gets the Initial value of the OAPH property.
47+
/// </summary>
48+
/// <value>
49+
/// The initial value of the OAPH property.
50+
/// </value>
51+
public string? InitialValue { get; init; }
4452
}
4553
#nullable restore
4654
#pragma warning restore

0 commit comments

Comments
 (0)