@@ -142,10 +142,18 @@ public static PropertyInfo GetInfo(IFieldSymbol fieldSymbol, out ImmutableArray<
142
142
/// </summary>
143
143
/// <param name="propertyInfo">The input <see cref="PropertyInfo"/> instance to process.</param>
144
144
/// <returns>The generated <see cref="MemberDeclarationSyntax"/> instance for <paramref name="propertyInfo"/>.</returns>
145
- public static MemberDeclarationSyntax GetSyntax ( PropertyInfo propertyInfo )
145
+ public static MemberDeclarationSyntax GetPropertySyntax ( PropertyInfo propertyInfo )
146
146
{
147
147
ImmutableArray < StatementSyntax > . Builder setterStatements = ImmutableArray . CreateBuilder < StatementSyntax > ( ) ;
148
148
149
+ // Add the OnPropertyChanging() call first:
150
+ //
151
+ // On<PROPERTY_NAME>Changing(value);
152
+ setterStatements . Add (
153
+ ExpressionStatement (
154
+ InvocationExpression ( IdentifierName ( $ "On{ propertyInfo . PropertyName } Changing") )
155
+ . AddArgumentListArguments ( Argument ( IdentifierName ( "value" ) ) ) ) ) ;
156
+
149
157
// Gather the statements to notify dependent properties
150
158
foreach ( string propertyName in propertyInfo . PropertyChangingNames )
151
159
{
@@ -192,6 +200,14 @@ public static MemberDeclarationSyntax GetSyntax(PropertyInfo propertyInfo)
192
200
Argument ( LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( propertyInfo . PropertyName ) ) ) ) ) ) ;
193
201
}
194
202
203
+ // Add the OnPropertyChanged() call:
204
+ //
205
+ // On<PROPERTY_NAME>Changed(value);
206
+ setterStatements . Add (
207
+ ExpressionStatement (
208
+ InvocationExpression ( IdentifierName ( $ "On{ propertyInfo . PropertyName } Changed") )
209
+ . AddArgumentListArguments ( Argument ( IdentifierName ( "value" ) ) ) ) ) ;
210
+
195
211
// Gather the statements to notify dependent properties
196
212
foreach ( string propertyName in propertyInfo . PropertyChangedNames )
197
213
{
@@ -292,6 +308,57 @@ public static MemberDeclarationSyntax GetSyntax(PropertyInfo propertyInfo)
292
308
. WithBody ( Block ( setterIfStatement ) ) ) ;
293
309
}
294
310
311
+ /// <summary>
312
+ /// Gets the <see cref="MemberDeclarationSyntax"/> instances for the <c>OnPropertyChanging</c> and <c>OnPropertyChanged</c> methods for the input field.
313
+ /// </summary>
314
+ /// <param name="propertyInfo">The input <see cref="PropertyInfo"/> instance to process.</param>
315
+ /// <returns>The generated <see cref="MemberDeclarationSyntax"/> instances for the <c>OnPropertyChanging</c> and <c>OnPropertyChanged</c> methods.</returns>
316
+ public static ImmutableArray < MemberDeclarationSyntax > GetOnPropertyChangeMethodsSyntax ( PropertyInfo propertyInfo )
317
+ {
318
+ // Get the parameter type syntax (adding the nullability annotation, if needed)
319
+ TypeSyntax parameterType = propertyInfo . IsNullableReferenceType
320
+ ? NullableType ( IdentifierName ( propertyInfo . TypeName ) )
321
+ : IdentifierName ( propertyInfo . TypeName ) ;
322
+
323
+ // Construct the generated method as follows:
324
+ //
325
+ // /// <summary>Executes the logic for when <see cref="<PROPERTY_NAME>"/> is changing.</summary>
326
+ // [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
327
+ // partial void On<PROPERTY_NAME>Changing(<PROPERTY_TYPE> value);
328
+ MemberDeclarationSyntax onPropertyChangingDeclaration =
329
+ MethodDeclaration ( PredefinedType ( Token ( SyntaxKind . VoidKeyword ) ) , Identifier ( $ "On{ propertyInfo . PropertyName } Changing") )
330
+ . AddModifiers ( Token ( SyntaxKind . PartialKeyword ) )
331
+ . AddParameterListParameters ( Parameter ( Identifier ( "value" ) ) . WithType ( parameterType ) )
332
+ . AddAttributeLists (
333
+ AttributeList ( SingletonSeparatedList (
334
+ Attribute ( IdentifierName ( "global::System.CodeDom.Compiler.GeneratedCode" ) )
335
+ . AddArgumentListArguments (
336
+ AttributeArgument ( LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( typeof ( ObservablePropertyGenerator ) . FullName ) ) ) ,
337
+ AttributeArgument ( LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( typeof ( ObservablePropertyGenerator ) . Assembly . GetName ( ) . Version . ToString ( ) ) ) ) ) ) )
338
+ . WithOpenBracketToken ( Token ( TriviaList ( Comment ( $ "/// <summary>Executes the logic for when <see cref=\" { propertyInfo . PropertyName } \" /> is changing.</summary>") ) , SyntaxKind . OpenBracketToken , TriviaList ( ) ) ) )
339
+ . WithSemicolonToken ( Token ( SyntaxKind . SemicolonToken ) ) ;
340
+
341
+ // Construct the generated method as follows:
342
+ //
343
+ // /// <summary>Executes the logic for when <see cref="<PROPERTY_NAME>"/> ust changed.</summary>
344
+ // [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
345
+ // partial void On<PROPERTY_NAME>Changed(<PROPERTY_TYPE> value);
346
+ MemberDeclarationSyntax onPropertyChangedDeclaration =
347
+ MethodDeclaration ( PredefinedType ( Token ( SyntaxKind . VoidKeyword ) ) , Identifier ( $ "On{ propertyInfo . PropertyName } Changed") )
348
+ . AddModifiers ( Token ( SyntaxKind . PartialKeyword ) )
349
+ . AddParameterListParameters ( Parameter ( Identifier ( "value" ) ) . WithType ( parameterType ) )
350
+ . AddAttributeLists (
351
+ AttributeList ( SingletonSeparatedList (
352
+ Attribute ( IdentifierName ( "global::System.CodeDom.Compiler.GeneratedCode" ) )
353
+ . AddArgumentListArguments (
354
+ AttributeArgument ( LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( typeof ( ObservablePropertyGenerator ) . FullName ) ) ) ,
355
+ AttributeArgument ( LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( typeof ( ObservablePropertyGenerator ) . Assembly . GetName ( ) . Version . ToString ( ) ) ) ) ) ) )
356
+ . WithOpenBracketToken ( Token ( TriviaList ( Comment ( $ "/// <summary>Executes the logic for when <see cref=\" { propertyInfo . PropertyName } \" /> just changed.</summary>") ) , SyntaxKind . OpenBracketToken , TriviaList ( ) ) ) )
357
+ . WithSemicolonToken ( Token ( SyntaxKind . SemicolonToken ) ) ;
358
+
359
+ return ImmutableArray . Create ( onPropertyChangingDeclaration , onPropertyChangedDeclaration ) ;
360
+ }
361
+
295
362
/// <summary>
296
363
/// Gets a <see cref="CompilationUnitSyntax"/> instance with the cached args of a specified type.
297
364
/// </summary>
0 commit comments