Skip to content

Commit 5229c87

Browse files
authored
Added more documentation.
1 parent ec81e3d commit 5229c87

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Roslyn generator that automatically implements ReadOnly interfaces for annotated
1616

1717
To use this generator, simply annotate a type with `readOnly.GenerateReadOnlyAttribute` and mark the type as partial, like so:
1818

19+
**User code:**
1920
```cs
2021
using readOnly;
2122

@@ -31,6 +32,8 @@ public partial class Foo {
3132
```
3233

3334
This will generate a read only version of the type that your type will implement automatically. This new type exposes only getters for each property by default:
35+
36+
**Generated code:**
3437
```cs
3538
public partial interface IReadOnlyFoo {
3639
int A { get; }
@@ -41,6 +44,7 @@ public partial interface IReadOnlyFoo {
4144

4245
If you have certain methods that don't change state, you can mark them as const similar to C++ by annotating them with `readOnly.ConstAttribute`:
4346

47+
**User code:**
4448
```cs
4549
using readOnly;
4650

@@ -54,8 +58,102 @@ public partial class Foo {
5458
```
5559

5660
Now, the generated read only interface will also include this const method:
61+
62+
**Generated code:**
5763
```cs
5864
public partial interface IReadOnlyFoo {
5965
int B(int c);
6066
}
6167
```
68+
69+
*Note: This generator does not verify that const methods are pure.*
70+
71+
### Interplay with other readonly types
72+
73+
If your type refers to another type with its own IReadOnly interface, it will automatically use that instead in the generated type:
74+
75+
**User code:**
76+
```cs
77+
using readOnly;
78+
79+
[GenerateReadOnly]
80+
public partial class Foo;
81+
82+
[GenerateReadOnly]
83+
public partial class Bar {
84+
public Foo Value { get; set; }
85+
}
86+
```
87+
88+
**Generated code:**
89+
```cs
90+
public partial interface IReadOnlyFoo;
91+
92+
public partial interface IReadOnlyBar {
93+
public IReadOnlyFoo Value { get; }
94+
}
95+
```
96+
97+
*This works internally by generating overrides for these read only parent properties/methods that cast the mutable version to the read only version.*
98+
99+
This also supports recursion:
100+
101+
**User code:**
102+
```cs
103+
using readOnly;
104+
105+
[GenerateReadOnly]
106+
public partial class Node {
107+
public Node Child { get; set; }
108+
}
109+
```
110+
111+
**Generated code:**
112+
```cs
113+
public partial interface IReadOnlyNode {
114+
public IReadOnlyNode Child { get; }
115+
}
116+
```
117+
118+
### Forcing mutability
119+
120+
If you don't want a type to be swapped out for its IReadOnly counterpart, you can force it to be kept by annotating the type with `readOnly.KeepMutableTypeAttribute`:
121+
122+
**User code:**
123+
```cs
124+
using readOnly;
125+
126+
[GenerateReadOnly]
127+
public partial class Foo;
128+
129+
[GenerateReadOnly]
130+
public partial class Bar {
131+
[Const]
132+
[KeepMutableType]
133+
public Foo Value { get; set; }
134+
135+
[Const]
136+
[KeepMutableType]
137+
public Foo KeepInReturn() {
138+
...
139+
}
140+
141+
[Const]
142+
public void KeepInParam([KeepMutableType] Foo value) {
143+
...
144+
}
145+
}
146+
```
147+
148+
**Generated code:**
149+
```cs
150+
public partial interface IReadOnlyFoo;
151+
152+
public partial interface IReadOnlyBar {
153+
Foo Value { get; }
154+
155+
Foo KeepInReturn();
156+
157+
void KeepInParam(Foo value);
158+
}
159+
```

0 commit comments

Comments
 (0)