@@ -80,14 +80,20 @@ private static IEnumerable<MemberDeclarationSyntax> GenerateMembers(DependencyPr
8080 continue ;
8181 }
8282
83- TypeSyntax propertyTypeSyntax = propertyType . GetSyntax ( ) ;
83+ TypeSyntax propertyTypeSyntaxWithoutNullabilityAnnotation = propertyType . GetSyntax ( ) ;
84+ TypeSyntax propertyTypeSyntax = propertyTypeSyntaxWithoutNullabilityAnnotation ;
85+
86+ if ( ! attribute . HasNamedArgument ( "NotNull" , true ) )
87+ {
88+ propertyTypeSyntax = NullableType ( propertyTypeSyntax ) ;
89+ }
8490
8591 // Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata)
8692 // RegisterAttached(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata)
8793 SeparatedSyntaxList < ArgumentSyntax > registerArguments = SeparatedList (
8894 [
89- Argument ( NameOfExpression ( IdentifierName ( name ) ) ) , // name
90- Argument ( TypeOfExpression ( propertyTypeSyntax ) ) , // propertyType
95+ Argument ( StringLiteralExpression ( name ) ) , // name
96+ Argument ( TypeOfExpression ( propertyTypeSyntaxWithoutNullabilityAnnotation ) ) , // propertyType
9197 Argument ( TypeOfExpression ( IdentifierName ( context . Hierarchy . Hierarchy [ 0 ] . MinimallyQualifiedName ) ) ) // ownerType
9298 ] ) ;
9399
@@ -97,7 +103,7 @@ private static IEnumerable<MemberDeclarationSyntax> GenerateMembers(DependencyPr
97103 // PropertyMetadata.Create(CreateDefaultValueCallback createDefaultValueCallback, PropertyChangedCallback propertyChangedCallback)
98104
99105 SeparatedSyntaxList < ArgumentSyntax > createArguments = SeparatedList < ArgumentSyntax > ( ) ;
100- if ( attribute . TryGetNamedArgument ( "CreateDefaultValueCallback " , out string ? createDefaultValueCallbackName ) )
106+ if ( attribute . TryGetNamedArgument ( "CreateDefaultValueCallbackName " , out string ? createDefaultValueCallbackName ) )
101107 {
102108 createArguments = createArguments . Add ( Argument ( IdentifierName ( createDefaultValueCallbackName ) ) ) ;
103109 }
@@ -106,10 +112,10 @@ private static IEnumerable<MemberDeclarationSyntax> GenerateMembers(DependencyPr
106112 bool hasDefaultValue = attribute . TryGetNamedArgument ( "DefaultValue" , out TypedConstantInfo ? defaultValue ) ;
107113 createArguments = createArguments . Add ( hasDefaultValue
108114 ? Argument ( defaultValue ! . GetSyntax ( ) )
109- : Argument ( NullLiteralExpression ) ) ;
115+ : Argument ( DefaultExpression ( ObjectType ) ) ) ;
110116 }
111117
112- if ( attribute . TryGetNamedArgument ( "PropertyChangedCallback " , out string ? propertyChangedCallbackName ) )
118+ if ( attribute . TryGetNamedArgument ( "PropertyChangedCallbackName " , out string ? propertyChangedCallbackName ) )
113119 {
114120 createArguments = createArguments . Add ( Argument ( IdentifierName ( propertyChangedCallbackName ) ) ) ;
115121 }
@@ -135,19 +141,20 @@ private static IEnumerable<MemberDeclarationSyntax> GenerateMembers(DependencyPr
135141 SimpleMemberAccessExpression (
136142 NameOfMicrosoftUIXaml ,
137143 IdentifierName ( "DependencyProperty" ) ) ,
138- IdentifierName ( isAttached ? "Register" : "RegisterAttached" ) ) )
139- . WithArgumentList ( ArgumentList ( registerArguments ) ) ) ) ) ) ) ;
144+ IdentifierName ( isAttached ? "RegisterAttached" : "Register" ) ) )
145+ . WithArgumentList ( ArgumentList ( registerArguments ) ) ) ) ) ) )
146+ . WithModifiers ( PrivateStaticReadonlyTokenList ) ;
140147
141148 if ( ! isAttached )
142149 {
143150 // Generate a property for non-attached properties
144- yield return PropertyDeclaration ( dependencyPropertyType , Identifier ( name ) )
151+ yield return PropertyDeclaration ( propertyTypeSyntax , Identifier ( name ) )
145152 . WithModifiers ( PublicTokenList )
146153 . WithIdentifier ( Identifier ( name ) )
147154 . WithAccessorList ( AccessorList ( List (
148155 [
149156 GetAccessorDeclaration ( ) . WithExpressionBody ( ArrowExpressionClause ( CastExpression (
150- propertyTypeSyntax ,
157+ propertyTypeSyntaxWithoutNullabilityAnnotation ,
151158 InvocationExpression ( IdentifierName ( "GetValue" ) )
152159 . WithArgumentList ( ArgumentList ( SingletonSeparatedList (
153160 Argument ( IdentifierName ( propertyName ) ) ) ) ) ) ) ) ,
@@ -162,35 +169,49 @@ private static IEnumerable<MemberDeclarationSyntax> GenerateMembers(DependencyPr
162169 }
163170 else
164171 {
172+ TypeSyntax targetTypeSyntax ;
173+ if ( attribute . TryGetNamedArgument ( "TargetType" , out TypedConstantInfo ? targetType ) &&
174+ targetType is TypedConstantInfo . Type type )
175+ {
176+ targetTypeSyntax = NullableType ( ParseName ( type . FullyQualifiedTypeName ) ) ;
177+ }
178+ else
179+ {
180+ targetTypeSyntax = NullableType ( QualifiedName ( NameOfMicrosoftUIXaml , IdentifierName ( "DependencyObject" ) ) ) ;
181+ }
182+
165183 // Generate static methods for attached properties
166184 yield return MethodDeclaration ( propertyTypeSyntax , Identifier ( $ "Get{ name } ") )
167185 . WithModifiers ( PublicStaticTokenList )
168186 . WithParameterList ( ParameterList ( SeparatedList (
169187 [
170- Parameter ( NullableType ( QualifiedName ( NameOfMicrosoftUIXaml , IdentifierName ( "DependencyObject" ) ) ) , Identifier ( "obj" ) )
188+ Parameter ( targetTypeSyntax , Identifier ( "obj" ) )
171189 ] ) ) )
172190 . WithBody ( Block ( SingletonList (
173- ReturnStatement ( CastExpression ( propertyTypeSyntax ,
174- InvocationExpression ( IdentifierName ( "GetValue" ) )
175- . WithArgumentList ( ArgumentList ( SingletonSeparatedList (
176- Argument ( IdentifierName ( propertyName ) ) ) ) ) ) ) ) ) ) ;
191+ ReturnStatement ( CastExpression (
192+ propertyTypeSyntax ,
193+ ConditionalAccessExpression (
194+ IdentifierName ( "obj" ) ,
195+ InvocationExpression ( MemberBindingExpression ( IdentifierName ( "GetValue" ) ) )
196+ . WithArgumentList ( ArgumentList ( SingletonSeparatedList (
197+ Argument ( IdentifierName ( propertyName ) ) ) ) ) ) ) ) ) ) ) ;
177198
178199 yield return MethodDeclaration ( VoidType , Identifier ( $ "Set{ name } ") )
179200 . WithModifiers ( PublicStaticTokenList )
180201 . WithParameterList ( ParameterList ( SeparatedList (
181202 [
182- Parameter ( NullableType ( QualifiedName ( NameOfMicrosoftUIXaml , IdentifierName ( "DependencyObject" ) ) ) , Identifier ( "obj" ) ) ,
203+ Parameter ( targetTypeSyntax , Identifier ( "obj" ) ) ,
183204 Parameter ( propertyTypeSyntax , Identifier ( "value" ) )
184205 ] ) ) )
185206 . WithBody ( Block ( SingletonList (
186- ExpressionStatement ( InvocationExpression ( SimpleMemberAccessExpression (
207+ ExpressionStatement ( ConditionalAccessExpression (
187208 IdentifierName ( "obj" ) ,
188- IdentifierName ( "SetValue" ) ) )
189- . WithArgumentList ( ArgumentList ( SeparatedList (
209+ InvocationExpression ( MemberBindingExpression ( IdentifierName ( "SetValue" ) ) )
210+ . WithArgumentList ( ArgumentList ( SeparatedList (
190211 [
191212 Argument ( IdentifierName ( propertyName ) ) ,
192213 Argument ( IdentifierName ( "value" ) )
193- ] ) ) ) ) ) ) ) ;
214+ ] ) ) ) ) ) ) ) ) ;
194215 }
195216 }
196217 }
0 commit comments