@@ -46,6 +46,11 @@ public CSharpProperty(CSharpIdentifier type, string name)
46
46
/// </summary>
47
47
public List < CSharpAttribute > Attributes { get ; } = new List < CSharpAttribute > ( ) ;
48
48
49
+ /// <summary>
50
+ /// The property's initializer (sets default value).
51
+ /// </summary>
52
+ public CSharpConstructor ? Initializer { get ; set ; }
53
+
49
54
/// <summary>
50
55
/// An expression body for the property's getter.
51
56
/// </summary>
@@ -61,14 +66,20 @@ public CSharpProperty(CSharpIdentifier type, string name)
61
66
/// </summary>
62
67
internal IEnumerable < string > GetNamespaces ( )
63
68
{
69
+ foreach ( string ns in Type . GetNamespaces ( ) )
70
+ yield return ns ;
71
+
64
72
foreach ( string ? ns in Attributes . Select ( x => x . Identifier . Namespace ) )
65
73
{
66
74
if ( ns != null )
67
75
yield return ns ;
68
76
}
69
77
70
- foreach ( string ns in Type . GetNamespaces ( ) )
71
- yield return ns ;
78
+ if ( Initializer != null )
79
+ {
80
+ foreach ( string ns in Initializer . GetNamespaces ( ) )
81
+ yield return ns ;
82
+ }
72
83
73
84
if ( GetterExpression != null )
74
85
{
@@ -83,28 +94,41 @@ internal IEnumerable<string> GetNamespaces()
83
94
/// <param name="makePublic">Controls whether to make the property public or not.</param>
84
95
internal PropertyDeclarationSyntax ToSyntax ( bool makePublic = false )
85
96
{
86
- var propertyDeclaration =
87
- PropertyDeclaration ( Type . ToSyntax ( ) , Identifier ( Name ) ) ;
97
+ var declaration = PropertyDeclaration ( Type . ToSyntax ( ) , Identifier ( Name ) ) ;
88
98
89
99
if ( makePublic )
90
- propertyDeclaration = propertyDeclaration . AddModifiers ( Token ( SyntaxKind . PublicKeyword ) ) ;
100
+ declaration = declaration . AddModifiers ( Token ( SyntaxKind . PublicKeyword ) ) ;
91
101
92
- propertyDeclaration = propertyDeclaration
93
- . WithAttributeLists ( List ( Attributes . Select ( x => x . ToSyntax ( ) ) ) )
94
- . WithDocumentation ( Summary ) ;
102
+ declaration = declaration . WithAttributeLists ( List ( Attributes . Select ( x => x . ToSyntax ( ) ) ) )
103
+ . WithDocumentation ( Summary ) ;
95
104
96
- return ( GetterExpression == null )
97
- ? propertyDeclaration . WithAccessorList ( AccessorList ( List ( GetAccessors ( ) ) ) )
98
- : propertyDeclaration . WithExpressionBody ( ArrowExpressionClause ( GetterExpression . ToInvocationSyntax ( ) ) )
99
- . WithSemicolonToken ( Token ( SyntaxKind . SemicolonToken ) ) ;
100
- }
105
+ if ( GetterExpression != null )
106
+ {
107
+ if ( Initializer != null )
108
+ throw new InvalidOperationException ( $ "{ nameof ( GetterExpression ) } and { nameof ( Initializer ) } may not be both set for the same { nameof ( CSharpProperty ) } .") ;
101
109
102
- private IEnumerable < AccessorDeclarationSyntax > GetAccessors ( )
103
- {
104
- AccessorDeclarationSyntax Declaration ( SyntaxKind kind ) => AccessorDeclaration ( kind ) . WithSemicolonToken ( Token ( SyntaxKind . SemicolonToken ) ) ;
110
+ if ( HasSetter )
111
+ throw new InvalidOperationException ( $ "{ nameof ( GetterExpression ) } and { nameof ( HasSetter ) } may not be both set for the same { nameof ( CSharpProperty ) } .") ;
112
+
113
+ declaration = declaration . WithExpressionBody ( ArrowExpressionClause ( GetterExpression . ToInvocationSyntax ( ) ) )
114
+ . WithSemicolonToken ( Token ( SyntaxKind . SemicolonToken ) ) ;
115
+ }
116
+ else
117
+ {
118
+ var accessors = new List < SyntaxKind > { SyntaxKind . GetAccessorDeclaration } ;
119
+ if ( HasSetter ) accessors . Add ( SyntaxKind . SetAccessorDeclaration ) ;
120
+
121
+ declaration = declaration . WithAccessorList ( AccessorList ( List (
122
+ accessors . Select ( x => AccessorDeclaration ( x ) . WithSemicolonToken ( Token ( SyntaxKind . SemicolonToken ) ) ) ) ) ) ;
123
+ }
124
+
125
+ if ( Initializer != null )
126
+ {
127
+ declaration = declaration . WithInitializer ( EqualsValueClause ( Initializer . ToInvocationSyntax ( ) ) )
128
+ . WithSemicolonToken ( Token ( SyntaxKind . SemicolonToken ) ) ;
129
+ }
105
130
106
- yield return Declaration ( SyntaxKind . GetAccessorDeclaration ) ;
107
- if ( HasSetter ) yield return Declaration ( SyntaxKind . SetAccessorDeclaration ) ;
131
+ return declaration ;
108
132
}
109
133
110
134
/// <summary>
0 commit comments