@@ -240,6 +240,14 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration(
240
240
}
241
241
}
242
242
243
+ // In case the backing field is exactly named "value", we need to add the "this." prefix to ensure that comparisons and assignments
244
+ // with it in the generated setter body are executed correctly and without conflicts with the implicit value parameter.
245
+ ExpressionSyntax fieldExpression = fieldSymbol . Name switch
246
+ {
247
+ "value" => MemberAccessExpression ( SyntaxKind . SimpleMemberAccessExpression , ThisExpression ( ) , IdentifierName ( "value" ) ) ,
248
+ string name => IdentifierName ( name )
249
+ } ;
250
+
243
251
BlockSyntax setterBlock ;
244
252
245
253
if ( validationAttributes . Count > 0 )
@@ -263,10 +271,10 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration(
263
271
264
272
// Generate the inner setter block as follows:
265
273
//
266
- // if (!global::System.Collections.Generic.EqualityComparer<<FIELD_TYPE>>.Default.Equals(<FIELD_NAME>, value))
274
+ // if (!global::System.Collections.Generic.EqualityComparer<<FIELD_TYPE>>.Default.Equals(this. <FIELD_NAME>, value))
267
275
// {
268
276
// OnPropertyChanging(global::Microsoft.Toolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.PropertyNamePropertyChangingEventArgs); // Optional
269
- // <FIELD_NAME> = value;
277
+ // this. <FIELD_NAME> = value;
270
278
// OnPropertyChanged(global::Microsoft.Toolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.PropertyNamePropertyChangedEventArgs);
271
279
// ValidateProperty(value, <PROPERTY_NAME>);
272
280
// OnPropertyChanged(global::Microsoft.Toolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.Property1PropertyChangedEventArgs); // Optional
@@ -291,7 +299,7 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration(
291
299
IdentifierName ( "Default" ) ) ,
292
300
IdentifierName ( "Equals" ) ) )
293
301
. AddArgumentListArguments (
294
- Argument ( IdentifierName ( fieldSymbol . Name ) ) ,
302
+ Argument ( fieldExpression ) ,
295
303
Argument ( IdentifierName ( "value" ) ) ) ) ,
296
304
Block (
297
305
ExpressionStatement (
@@ -303,7 +311,7 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration(
303
311
ExpressionStatement (
304
312
AssignmentExpression (
305
313
SyntaxKind . SimpleAssignmentExpression ,
306
- IdentifierName ( fieldSymbol . Name ) ,
314
+ fieldExpression ,
307
315
IdentifierName ( "value" ) ) ) ,
308
316
ExpressionStatement (
309
317
InvocationExpression ( IdentifierName ( "OnPropertyChanged" ) )
@@ -346,7 +354,7 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration(
346
354
ExpressionStatement (
347
355
AssignmentExpression (
348
356
SyntaxKind . SimpleAssignmentExpression ,
349
- IdentifierName ( fieldSymbol . Name ) ,
357
+ fieldExpression ,
350
358
IdentifierName ( "value" ) ) ) ,
351
359
ExpressionStatement (
352
360
InvocationExpression ( IdentifierName ( "OnPropertyChanged" ) )
@@ -384,7 +392,7 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration(
384
392
IdentifierName ( "Default" ) ) ,
385
393
IdentifierName ( "Equals" ) ) )
386
394
. AddArgumentListArguments (
387
- Argument ( IdentifierName ( fieldSymbol . Name ) ) ,
395
+ Argument ( fieldExpression ) ,
388
396
Argument ( IdentifierName ( "value" ) ) ) ) ,
389
397
updateAndNotificationBlock ) ) ;
390
398
}
0 commit comments