You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ObservableAsPropertyHelper is used to create a read-only property from an IObservable. The generated code will create a backing field and a property that returns the value of the backing field. The backing field is initialized with the value of the IObservable when the class is instantiated.
90
+
91
+
A private field is created with the name of the property prefixed with an underscore. The field is initialized with the value of the IObservable when the class is instantiated. The property is created with the same name as the field without the underscore. The property returns the value of the field until initialized, then it returns the value of the IObservable.
92
+
93
+
You can define the name of the property by using the PropertyName parameter. If you do not define the PropertyName, the property name will be the same as the field name without the underscore.
94
+
89
95
### Usage ObservableAsPropertyHelper with Field
90
96
```csharp
91
97
usingReactiveUI.SourceGenerators;
@@ -112,7 +118,10 @@ using ReactiveUI.SourceGenerators;
Console.Out.WriteLine("MyReadOnlyProperty After Init");
88
+
89
+
// setting this value should not update the _myReadOnlyPropertyHelper as the _testSubject has not been updated yet but the _myReadOnlyPropertyHelper should be updated with null upon init.
90
+
_myReadOnlyProperty=-2.0;
91
+
92
+
// null value expected as the _testSubject has not been updated yet, ignoring the private variable.
66
93
Console.Out.WriteLine(MyReadOnlyProperty);
94
+
Console.Out.WriteLine(_myReadOnlyProperty);
95
+
_testSubject.OnNext(10.0);
96
+
97
+
// expected value 10 as the _testSubject has been updated.
98
+
Console.Out.WriteLine(MyReadOnlyProperty);
99
+
Console.Out.WriteLine(_myReadOnlyProperty);
100
+
_testSubject.OnNext(null);
101
+
102
+
// expected value null as the _testSubject has been updated.
103
+
Console.Out.WriteLine(MyReadOnlyProperty);
104
+
Console.Out.WriteLine(_myReadOnlyProperty);
105
+
106
+
Console.Out.WriteLine("MyReadOnlyNonNullProperty After Init");
107
+
108
+
// setting this value should not update the _myReadOnlyNonNullProperty as the _testNonNullSubject has not been updated yet but the _myReadOnlyNonNullPropertyHelper should be updated with null upon init.
109
+
_myReadOnlyNonNullProperty=-2.0;
110
+
111
+
// 0 value expected as the _testNonNullSubject has not been updated yet, ignoring the private variable.
Copy file name to clipboardExpand all lines: src/ReactiveUI.SourceGenerators/ObservableAsProperty/ObservableAsPropertyFromObservableGenerator.Execute.cs
0 commit comments