8
8
using System . Linq ;
9
9
using System . Threading ;
10
10
using Microsoft . CodeAnalysis ;
11
+ using Microsoft . CodeAnalysis . CSharp ;
11
12
using Microsoft . CodeAnalysis . CSharp . Syntax ;
12
13
using ReactiveUI . SourceGenerators . Extensions ;
13
14
using ReactiveUI . SourceGenerators . Helpers ;
@@ -59,14 +60,60 @@ public sealed partial class ReactiveGenerator
59
60
60
61
token . ThrowIfCancellationRequested ( ) ;
61
62
62
- var accessModifier = $ "{ propertySymbol . SetMethod ? . DeclaredAccessibility } set". ToLower ( ) ;
63
- if ( accessModifier . StartsWith ( "public" , StringComparison . Ordinal ) )
63
+ // Get Property AccessModifier.
64
+ var propertyAccessModifier = propertySymbol . DeclaredAccessibility . ToString ( ) . ToLower ( ) ;
65
+ if ( propertyAccessModifier ? . Contains ( "and" ) == true )
64
66
{
65
- accessModifier = "set" ;
67
+ propertyAccessModifier = propertyAccessModifier . Replace ( "and" , " " ) ;
66
68
}
67
- else if ( accessModifier . Contains ( "and" ) )
69
+ else if ( propertyAccessModifier ? . Contains ( "or" ) == true )
68
70
{
69
- accessModifier = accessModifier . Replace ( "and" , " " ) ;
71
+ propertyAccessModifier = propertyAccessModifier . Replace ( "or" , " " ) ;
72
+ }
73
+
74
+ token . ThrowIfCancellationRequested ( ) ;
75
+
76
+ // Get Set AccessModifier.
77
+ var setAccessModifier = $ "{ propertySymbol . SetMethod ? . DeclaredAccessibility } set". ToLower ( ) ;
78
+ if ( setAccessModifier . StartsWith ( "public" , StringComparison . Ordinal ) )
79
+ {
80
+ setAccessModifier = "set" ;
81
+ }
82
+ else if ( setAccessModifier ? . Contains ( "and" ) == true )
83
+ {
84
+ if ( setAccessModifier . Contains ( "protectedandinternal" ) )
85
+ {
86
+ setAccessModifier = setAccessModifier . Replace ( "protectedandinternal" , "private protected" ) ;
87
+ }
88
+ else
89
+ {
90
+ setAccessModifier = setAccessModifier . Replace ( "and" , " " ) ;
91
+ }
92
+ }
93
+ else if ( setAccessModifier ? . Contains ( "or" ) == true )
94
+ {
95
+ setAccessModifier = setAccessModifier . Replace ( "or" , " " ) ;
96
+ }
97
+
98
+ if ( propertyAccessModifier == "private" && setAccessModifier == "private set" )
99
+ {
100
+ setAccessModifier = "set" ;
101
+ }
102
+ else if ( propertyAccessModifier == "internal" && setAccessModifier == "internal set" )
103
+ {
104
+ setAccessModifier = "set" ;
105
+ }
106
+ else if ( propertyAccessModifier == "protected" && setAccessModifier == "protected set" )
107
+ {
108
+ setAccessModifier = "set" ;
109
+ }
110
+ else if ( propertyAccessModifier == "protected internal" && setAccessModifier == "protected internal set" )
111
+ {
112
+ setAccessModifier = "set" ;
113
+ }
114
+ else if ( propertyAccessModifier == "private protected" && setAccessModifier == "private protected set" )
115
+ {
116
+ setAccessModifier = "set" ;
70
117
}
71
118
72
119
token . ThrowIfCancellationRequested ( ) ;
@@ -78,6 +125,12 @@ public sealed partial class ReactiveGenerator
78
125
79
126
var typeNameWithNullabilityAnnotations = propertySymbol . Type . GetFullyQualifiedNameWithNullabilityAnnotations ( ) ;
80
127
var fieldName = propertySymbol . GetGeneratedFieldName ( ) ;
128
+
129
+ if ( context . SemanticModel . Compilation is CSharpCompilation compilation && compilation . LanguageVersion == LanguageVersion . Preview )
130
+ {
131
+ fieldName = "field" ;
132
+ }
133
+
81
134
var propertyName = propertySymbol . Name ;
82
135
83
136
// Get the nullability info for the property
@@ -111,10 +164,11 @@ public sealed partial class ReactiveGenerator
111
164
isReferenceTypeOrUnconstraindTypeParameter ,
112
165
includeMemberNotNullOnSetAccessor ,
113
166
forwardedAttributesString ,
114
- accessModifier ,
167
+ setAccessModifier ! ,
115
168
inheritance ,
116
169
useRequired ,
117
- true ) ,
170
+ true ,
171
+ propertyAccessModifier ! ) ,
118
172
builder . ToImmutable ( ) ) ;
119
173
}
120
174
#endif
@@ -156,12 +210,12 @@ public sealed partial class ReactiveGenerator
156
210
157
211
// Get AccessModifier enum value from the attribute
158
212
attributeData . TryGetNamedArgument ( "SetModifier" , out int accessModifierArgument ) ;
159
- var accessModifier = accessModifierArgument switch
213
+ var setAccessModifier = accessModifierArgument switch
160
214
{
161
215
1 => "protected set" ,
162
216
2 => "internal set" ,
163
217
3 => "private set" ,
164
- 4 => "internal protected set" ,
218
+ 4 => "protected internal set" ,
165
219
5 => "private protected set" ,
166
220
6 => "init" ,
167
221
_ => "set" ,
@@ -236,10 +290,11 @@ public sealed partial class ReactiveGenerator
236
290
isReferenceTypeOrUnconstraindTypeParameter ,
237
291
includeMemberNotNullOnSetAccessor ,
238
292
forwardedAttributesString ,
239
- accessModifier ,
293
+ setAccessModifier ,
240
294
inheritance ,
241
295
useRequired ,
242
- false ) ,
296
+ false ,
297
+ "public" ) ,
243
298
builder . ToImmutable ( ) ) ;
244
299
}
245
300
@@ -255,7 +310,7 @@ public sealed partial class ReactiveGenerator
255
310
private static string GenerateSource ( string containingTypeName , string containingNamespace , string containingClassVisibility , string containingType , PropertyInfo [ ] properties )
256
311
{
257
312
// Get Parent class details from properties.ParentInfo
258
- var ( parentClassDeclarationsString , closingBrackets ) = TargetInfo . GenerateParentClassDeclarations ( properties . Select ( p => p . TargetInfo . ParentInfo ) . ToArray ( ) ) ;
313
+ var ( parentClassDeclarationsString , closingBrackets ) = TargetInfo . GenerateParentClassDeclarations ( [ .. properties . Select ( p => p . TargetInfo . ParentInfo ) ] ) ;
259
314
260
315
var classes = GenerateClassWithProperties ( containingTypeName , containingNamespace , containingClassVisibility , containingType , properties ) ;
261
316
@@ -315,43 +370,47 @@ private static string GetPropertySyntax(PropertyInfo propertyInfo)
315
370
return string . Empty ;
316
371
}
317
372
318
- var fieldName = propertyInfo . FieldName ;
373
+ var setFieldName = propertyInfo . FieldName ;
374
+ var getFieldName = propertyInfo . FieldName ;
319
375
if ( propertyInfo . FieldName == "value" )
320
376
{
321
- fieldName = "this.value" ;
377
+ setFieldName = "this.value" ;
322
378
}
323
379
324
380
var fieldSyntax = string . Empty ;
325
381
var partialModifier = propertyInfo . IsProperty ? "partial " : string . Empty ;
326
- if ( propertyInfo . IsProperty )
382
+ if ( propertyInfo . IsProperty && propertyInfo . FieldName != "field" )
327
383
{
328
384
fieldSyntax = $ "private { propertyInfo . TypeNameWithNullabilityAnnotations } { propertyInfo . FieldName } ;";
329
385
}
330
386
387
+ var accessModifier = propertyInfo . PropertyAccessModifier ;
388
+ var setAccessModifier = propertyInfo . SetAccessModifier ;
389
+
331
390
var propertyAttributes = string . Join ( "\n " , AttributeDefinitions . ExcludeFromCodeCoverage . Concat ( propertyInfo . ForwardedAttributes ) ) ;
332
391
333
392
if ( propertyInfo . IncludeMemberNotNullOnSetAccessor || propertyInfo . IsReferenceTypeOrUnconstrainedTypeParameter )
334
393
{
335
394
return
336
395
$$ """
337
396
{{ fieldSyntax }}
338
- /// <inheritdoc cref="{{ fieldName }} "/>
397
+ /// <inheritdoc cref="{{ setFieldName }} "/>
339
398
{{ propertyAttributes }}
340
- {{ propertyInfo . TargetInfo . TargetVisibility }} {{ propertyInfo . Inheritance }} {{ propertyInfo . UseRequired }} {{ partialModifier }} {{ propertyInfo . TypeNameWithNullabilityAnnotations }} {{ propertyInfo . PropertyName }}
399
+ {{ accessModifier }} {{ propertyInfo . Inheritance }} {{ propertyInfo . UseRequired }} {{ partialModifier }} {{ propertyInfo . TypeNameWithNullabilityAnnotations }} {{ propertyInfo . PropertyName }}
341
400
{
342
- get => {{ propertyInfo . FieldName }} ;
343
- [global::System.Diagnostics.CodeAnalysis.MemberNotNull("{{ fieldName }} ")]
344
- {{ propertyInfo . AccessModifier }} => this.RaiseAndSetIfChanged(ref {{ fieldName }} , value);
401
+ get => {{ getFieldName }} ;
402
+ [global::System.Diagnostics.CodeAnalysis.MemberNotNull("{{ setFieldName }} ")]
403
+ {{ setAccessModifier }} => this.RaiseAndSetIfChanged(ref {{ setFieldName }} , value);
345
404
}
346
405
""" ;
347
406
}
348
407
349
408
return
350
409
$$ """
351
410
{{ fieldSyntax }}
352
- /// <inheritdoc cref="{{ fieldName }} "/>
411
+ /// <inheritdoc cref="{{ setFieldName }} "/>
353
412
{{ propertyAttributes }}
354
- {{ propertyInfo . TargetInfo . TargetVisibility }} {{ propertyInfo . Inheritance }} {{ partialModifier }} {{ propertyInfo . UseRequired }} {{ propertyInfo . TypeNameWithNullabilityAnnotations }} {{ propertyInfo . PropertyName }} { get => {{ propertyInfo . FieldName }} ; {{ propertyInfo . AccessModifier }} => this.RaiseAndSetIfChanged(ref {{ fieldName }} , value); }
413
+ {{ accessModifier }} {{ propertyInfo . Inheritance }} {{ partialModifier }} {{ propertyInfo . UseRequired }} {{ propertyInfo . TypeNameWithNullabilityAnnotations }} {{ propertyInfo . PropertyName }} { get => {{ getFieldName }} ; {{ setAccessModifier }} => this.RaiseAndSetIfChanged(ref {{ setFieldName }} , value); }
355
414
""" ;
356
415
}
357
416
}
0 commit comments