7
7
using System ;
8
8
using System . Collections . Generic ;
9
9
using System . Collections . Immutable ;
10
- using System . Data . SqlTypes ;
11
10
using System . Text ;
12
11
13
12
namespace Files . Core . SourceGenerator
14
13
{
15
14
/// <summary>
16
15
/// Generates a set of dependency property and its backing field.
17
16
/// </summary>
18
- [ Generator ]
17
+ [ Generator ( LanguageNames . CSharp ) ]
19
18
public sealed class DependencyPropertyGenerator : IIncrementalGenerator
20
19
{
21
20
/// <inheritdoc/>
@@ -65,30 +64,43 @@ private string EmitSyntaxTree(INamedTypeSymbol typeSymbol, ImmutableArray<Attrib
65
64
var fieldName = $ "{ propertyName } Property";
66
65
var isSetterPrivate = false ;
67
66
var defaultValue = "global::Microsoft.UI.Xaml.DependencyProperty.UnsetValue" ;
67
+ ExpressionSyntax ? defaultValueExpression = null ;
68
68
var isNullable = false ;
69
69
70
70
// Get values from the attribute properties
71
+ int index = 0 ;
71
72
foreach ( var namedArgument in attribute . NamedArguments )
72
73
{
73
74
if ( namedArgument . Value . Value is { } value )
74
75
{
75
76
switch ( namedArgument . Key )
76
77
{
77
- case "IsSetterPrivate" : isSetterPrivate = ( bool ) value ; break ;
78
- case "DefaultValue" : defaultValue = ( string ) value ; break ;
79
- case "IsNullable" : isNullable = ( bool ) value ; break ;
78
+ case "IsSetterPrivate" :
79
+ isSetterPrivate = ( bool ) value ;
80
+ break ;
81
+ case "DefaultValue" :
82
+ defaultValueExpression = ( ( AttributeSyntax ) attribute . ApplicationSyntaxReference ! . GetSyntax ( ) ) . ArgumentList ! . Arguments [ index ] . Expression ;
83
+ break ;
84
+ case "IsNullable" :
85
+ isNullable = ( bool ) value ;
86
+ break ;
80
87
}
81
88
}
89
+
90
+ index ++ ;
82
91
}
83
92
84
- // Emit "new PropertyMetadata(...)"
85
- var dpPropertyMetadata = EmitPMObjectCreationExpression ( SyntaxFactory . ParseExpression ( defaultValue ) ) ;
93
+ defaultValueExpression ??= SyntaxFactory . ParseExpression ( defaultValue ) ;
94
+
95
+ // Emit "new PropertyMetadata(...)" expression
96
+ var dpPropertyMetadata = EmitPMObjectCreationExpression ( defaultValueExpression ) ;
86
97
87
- // Append callback method to PropertyMetadata
98
+ // Append callback to PropertyMetadata
88
99
if ( ! string . IsNullOrEmpty ( callbackMethodName ) )
89
- dpPropertyMetadata = EmitDPCallbackParenthesizedLambdaExpression ( dpPropertyMetadata , callbackMethodName , type , isNullable , typeSymbol ) ;
100
+ dpPropertyMetadata = dpPropertyMetadata . AddArgumentListArguments (
101
+ SyntaxFactory . Argument ( EmitDPCallbackParenthesizedLambdaExpression ( callbackMethodName , type , isNullable , typeSymbol ) ) ) ;
90
102
91
- // Emit "DependencyProperty.Register(...)" invocation expression
103
+ // Emit "DependencyProperty.Register(...)" expression
92
104
var dpRegisteringExpression = EmitDPRegisterInvocationExpression ( propertyName , type , typeSymbol , dpPropertyMetadata ) ;
93
105
94
106
// Emit the backing DependencyProperty field with attributes
@@ -111,53 +123,50 @@ private string EmitSyntaxTree(INamedTypeSymbol typeSymbol, ImmutableArray<Attrib
111
123
if ( members . Count is 0 )
112
124
return string . Empty ;
113
125
114
- // Generate class lock
126
+ // Generate class block
115
127
var generatedClass = SourceGeneratorHelper . GetClassDeclaration ( typeSymbol , members ) ;
116
128
117
129
// Generate namespace block
118
130
var generatedNamespace = SourceGeneratorHelper . GetFileScopedNamespaceDeclaration ( typeSymbol , generatedClass ) ;
119
131
120
- // Generate file block ( complication uint)
132
+ // Generate complication uint
121
133
var compilationUnit = SourceGeneratorHelper . GetCompilationUnit ( generatedNamespace ) ;
122
134
123
135
// Get full syntax tree and return as UTF8 string
124
136
return SyntaxFactory . SyntaxTree ( compilationUnit , encoding : Encoding . UTF8 ) . GetText ( ) . ToString ( ) ;
125
137
}
126
138
127
- private ObjectCreationExpressionSyntax EmitDPCallbackParenthesizedLambdaExpression ( ObjectCreationExpressionSyntax expression , string callbackName , ITypeSymbol type , bool isNullable , ITypeSymbol classSymbol )
139
+ private ParenthesizedLambdaExpressionSyntax EmitDPCallbackParenthesizedLambdaExpression ( string callbackName , ITypeSymbol type , bool isNullable , ITypeSymbol classSymbol )
128
140
{
129
141
// (d, e) => ((class)d).callbackName((type)e.OldValue, (type)e.NewValue)
130
- return expression . AddArgumentListArguments (
131
- SyntaxFactory . Argument (
132
- SyntaxFactory . ParenthesizedLambdaExpression ( )
133
- . AddParameterListParameters (
134
- SyntaxFactory . Parameter ( SyntaxFactory . Identifier ( "d" ) ) ,
135
- SyntaxFactory . Parameter ( SyntaxFactory . Identifier ( "e" ) ) )
136
- . WithExpressionBody (
137
- SyntaxFactory . InvocationExpression (
138
- SyntaxFactory . MemberAccessExpression (
139
- SyntaxKind . SimpleMemberAccessExpression ,
140
- SyntaxFactory . ParenthesizedExpression (
141
- SyntaxFactory . CastExpression (
142
- classSymbol . GetTypeSyntax ( false ) ,
143
- SyntaxFactory . IdentifierName ( "d" ) ) ) ,
144
- SyntaxFactory . IdentifierName ( callbackName ) ) )
145
- . AddArgumentListArguments (
146
- SyntaxFactory . Argument (
147
- SyntaxFactory . CastExpression (
148
- type . GetTypeSyntax ( isNullable ) ,
149
- SyntaxFactory . MemberAccessExpression (
150
- SyntaxKind . SimpleMemberAccessExpression ,
151
- SyntaxFactory . IdentifierName ( "e" ) ,
152
- SyntaxFactory . IdentifierName ( "OldValue" ) ) ) ) ,
153
- SyntaxFactory . Argument (
154
- SyntaxFactory . CastExpression (
155
- type . GetTypeSyntax ( isNullable ) ,
156
- SyntaxFactory . MemberAccessExpression (
157
- SyntaxKind . SimpleMemberAccessExpression ,
158
- SyntaxFactory . IdentifierName ( "e" ) ,
159
- SyntaxFactory . IdentifierName ( "NewValue" ) ) ) )
160
- ) ) ) ) ;
142
+ return SyntaxFactory . ParenthesizedLambdaExpression ( )
143
+ . AddParameterListParameters (
144
+ SyntaxFactory . Parameter ( SyntaxFactory . Identifier ( "d" ) ) ,
145
+ SyntaxFactory . Parameter ( SyntaxFactory . Identifier ( "e" ) ) )
146
+ . WithExpressionBody (
147
+ SyntaxFactory . InvocationExpression (
148
+ SyntaxFactory . MemberAccessExpression (
149
+ SyntaxKind . SimpleMemberAccessExpression ,
150
+ SyntaxFactory . ParenthesizedExpression (
151
+ SyntaxFactory . CastExpression (
152
+ classSymbol . GetTypeSyntax ( false ) ,
153
+ SyntaxFactory . IdentifierName ( "d" ) ) ) ,
154
+ SyntaxFactory . IdentifierName ( callbackName ) ) )
155
+ . AddArgumentListArguments (
156
+ SyntaxFactory . Argument (
157
+ SyntaxFactory . CastExpression (
158
+ type . GetTypeSyntax ( isNullable ) ,
159
+ SyntaxFactory . MemberAccessExpression (
160
+ SyntaxKind . SimpleMemberAccessExpression ,
161
+ SyntaxFactory . IdentifierName ( "e" ) ,
162
+ SyntaxFactory . IdentifierName ( "OldValue" ) ) ) ) ,
163
+ SyntaxFactory . Argument (
164
+ SyntaxFactory . CastExpression (
165
+ type . GetTypeSyntax ( isNullable ) ,
166
+ SyntaxFactory . MemberAccessExpression (
167
+ SyntaxKind . SimpleMemberAccessExpression ,
168
+ SyntaxFactory . IdentifierName ( "e" ) ,
169
+ SyntaxFactory . IdentifierName ( "NewValue" ) ) ) ) ) ) ;
161
170
}
162
171
163
172
private ObjectCreationExpressionSyntax EmitPMObjectCreationExpression ( ExpressionSyntax defaultValueExpression )
0 commit comments