@@ -30,56 +30,64 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
3030
3131 context . RegisterSourceOutput ( compilationAndClasses , static ( spc , source ) =>
3232 {
33- var ( compilation , classNodes ) = source ;
33+ var ( compilation , classNodes ) = source ;
3434
35- foreach ( var classNode in classNodes )
36- {
37- var model = compilation . GetSemanticModel ( classNode . SyntaxTree ) ;
38- if ( model . GetDeclaredSymbol ( classNode ) is not INamedTypeSymbol symbol ) continue ;
35+ foreach ( var classNode in classNodes )
36+ {
37+ var model = compilation . GetSemanticModel ( classNode . SyntaxTree ) ;
38+ if ( model . GetDeclaredSymbol ( classNode ) is not INamedTypeSymbol symbol ) continue ;
3939
40- if ( ! symbol . GetAttributes ( ) . Any ( attr => attr . AttributeClass ? . Name == "ViewModelAttribute" ) ) continue ;
40+ if ( ! symbol . GetAttributes ( ) . Any ( attr => attr . AttributeClass ? . Name == "ViewModelAttribute" ) ) continue ;
4141
42- var className = symbol . Name ;
43- var generatedName = $ "{ className } ";
44- var namespaceName = symbol . ContainingNamespace . ToDisplayString ( ) ;
42+ var className = symbol . Name ;
43+ var generatedName = $ "{ className } ";
44+ var namespaceName = symbol . ContainingNamespace . ToDisplayString ( ) ;
4545
46- var bindFields = symbol . GetMembers ( )
47- . OfType < IFieldSymbol > ( )
48- . Where ( f => f . GetAttributes ( ) . Any ( attr => attr . AttributeClass ? . Name == "BindAttribute" ) )
49- . ToList ( ) ;
46+ var bindFields = symbol . GetMembers ( )
47+ . OfType < IFieldSymbol > ( )
48+ . Where ( f => f . GetAttributes ( ) . Any ( attr => attr . AttributeClass ? . Name == "BindAttribute" ) )
49+ . ToList ( ) ;
5050
51- var commandMethods = symbol . GetMembers ( )
52- . OfType < IMethodSymbol > ( )
53- . Where ( m => m . MethodKind == MethodKind . Ordinary &&
54- m . GetAttributes ( ) . Any ( attr => attr . AttributeClass ? . Name == "CommandAttribute" ) )
55- . ToList ( ) ;
51+ var commandMethods = symbol . GetMembers ( )
52+ . OfType < IMethodSymbol > ( )
53+ . Where ( m => m . MethodKind == MethodKind . Ordinary &&
54+ m . GetAttributes ( ) . Any ( attr => attr . AttributeClass ? . Name == "CommandAttribute" ) )
55+ . ToList ( ) ;
5656
57- // Generate ViewModel partial class
58- var viewModelBuilder = new StringBuilder ( $@ "
59- using SimpleViewModel.BaseClasses;
57+ // Generate ViewModel partial class
58+ var viewModelBuilder = new StringBuilder (
59+ $@ " using SimpleViewModel.BaseClasses;
6060
6161namespace { namespaceName }
6262{{
6363 public partial class { className } : BaseViewModel
64- {{
65- " ) ;
64+ {{" ) ;
6665
67- foreach ( var field in bindFields )
68- {
69- viewModelBuilder . AppendLine ( $ " public { field . Type } { ToPascal ( field . Name ) } {{ get; set; }}") ;
70- }
66+ foreach ( var field in bindFields )
67+ {
68+ viewModelBuilder . AppendLine ( $ " public { field . Type } { ToPascal ( field . Name ) } {{ get; set; }}") ;
69+ }
70+
71+ viewModelBuilder . AppendLine ( ) ;
72+
73+ foreach ( var field in bindFields )
74+ {
75+ var fieldType = field . Type . ToDisplayString ( ) ;
76+ var fieldName = ToPascal ( field . Name ) ;
7177
72- viewModelBuilder . AppendLine ( ) ;
78+ viewModelBuilder . AppendLine ( $ " public { fieldType } { fieldName } {{ get => { field . Name } ; set => SetProperty(ref { field . Name } , value); }}") ;
79+ }
7380
74- foreach ( var method in commandMethods )
75- {
76- var commandClassName = $ "Command_{ method . Name } ";
77- var commandFieldName = $ "{ ToPascal ( method . Name ) } Command";
78- viewModelBuilder . AppendLine ( $ " public { commandClassName } { commandFieldName } {{ get; }} = new { commandClassName } (this);") ;
81+ foreach ( var method in commandMethods )
82+ {
83+ var commandClassName = $ "Command_{ method . Name } ";
84+ var commandFieldName = $ "{ ToPascal ( method . Name ) } Command";
85+ viewModelBuilder . AppendLine ( $ " private { commandClassName } _{ commandFieldName } {{ get; }} = new { commandClassName } (this);") ;
86+ viewModelBuilder . AppendLine ( $ " public { commandClassName } { commandFieldName } => _{ commandFieldName } ??= new(this);") ;
7987
8088 // Generate command class per method
81- var commandBuilder = new StringBuilder ( $@ "
82- using SimpleViewModel.BaseClasses;
89+ var commandBuilder = new StringBuilder (
90+ $@ " using SimpleViewModel.BaseClasses;
8391
8492namespace { namespaceName }
8593{{
0 commit comments