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
Copy file name to clipboardExpand all lines: README.md
+98Lines changed: 98 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,6 +16,7 @@ Roslyn generator that automatically implements ReadOnly interfaces for annotated
16
16
17
17
To use this generator, simply annotate a type with `readOnly.GenerateReadOnlyAttribute` and mark the type as partial, like so:
18
18
19
+
**User code:**
19
20
```cs
20
21
usingreadOnly;
21
22
@@ -31,6 +32,8 @@ public partial class Foo {
31
32
```
32
33
33
34
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:**
34
37
```cs
35
38
publicpartialinterfaceIReadOnlyFoo {
36
39
intA { get; }
@@ -41,6 +44,7 @@ public partial interface IReadOnlyFoo {
41
44
42
45
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`:
43
46
47
+
**User code:**
44
48
```cs
45
49
usingreadOnly;
46
50
@@ -54,8 +58,102 @@ public partial class Foo {
54
58
```
55
59
56
60
Now, the generated read only interface will also include this const method:
61
+
62
+
**Generated code:**
57
63
```cs
58
64
publicpartialinterfaceIReadOnlyFoo {
59
65
intB(intc);
60
66
}
61
67
```
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
+
usingreadOnly;
78
+
79
+
[GenerateReadOnly]
80
+
publicpartialclassFoo;
81
+
82
+
[GenerateReadOnly]
83
+
publicpartialclassBar {
84
+
publicFooValue { get; set; }
85
+
}
86
+
```
87
+
88
+
**Generated code:**
89
+
```cs
90
+
publicpartialinterfaceIReadOnlyFoo;
91
+
92
+
publicpartialinterfaceIReadOnlyBar {
93
+
publicIReadOnlyFooValue { 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
+
usingreadOnly;
104
+
105
+
[GenerateReadOnly]
106
+
publicpartialclassNode {
107
+
publicNodeChild { get; set; }
108
+
}
109
+
```
110
+
111
+
**Generated code:**
112
+
```cs
113
+
publicpartialinterfaceIReadOnlyNode {
114
+
publicIReadOnlyNodeChild { 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`:
0 commit comments