@@ -79,7 +79,7 @@ semanticModel is { } &&
7979 {
8080 context . RegisterCodeFix (
8181 "Implement IMultiValueConverter.ConvertBack for one way bindings." ,
82- ( editor , _ ) => editor . AddMethod ( classDeclaration , IMultiValueConverter . ConvertBack ( classDeclaration . Identifier . ValueText ) ) ,
82+ ( editor , _ ) => editor . AddMethod ( classDeclaration , IMultiValueConverter . ConvertBack ( classDeclaration . Identifier . ValueText , editor . Generator , nullableContext ) ) ,
8383 "Implement IMultiValueConverter" ,
8484 diagnostic ) ;
8585 }
@@ -116,6 +116,13 @@ private static bool HasInterface(ClassDeclarationSyntax classDeclaration, Qualif
116116 _ => SyntaxFactory . PredefinedType ( SyntaxFactory . Token ( SyntaxKind . ObjectKeyword ) ) ,
117117 } ;
118118
119+ private static TypeSyntax ArrayType ( TypeSyntax type ) => SyntaxFactory . ArrayType (
120+ type ,
121+ SyntaxFactory . SingletonList (
122+ SyntaxFactory . ArrayRankSpecifier (
123+ SyntaxFactory . SingletonSeparatedList < ExpressionSyntax > (
124+ SyntaxFactory . OmittedArraySizeExpression ( ) ) ) ) ) ;
125+
119126 private static class IValueConverter
120127 {
121128 internal static MethodDeclarationSyntax Convert ( SyntaxGenerator generator , NullableContext nullableContext )
@@ -189,30 +196,55 @@ internal static MethodDeclarationSyntax Convert(SyntaxGenerator generator, Nulla
189196 name : "Convert" ,
190197 parameters : new [ ]
191198 {
192- generator . ParameterDeclaration ( "value " , SyntaxFactory . ArrayType ( Object ( nullableContext ) ) ) ,
199+ generator . ParameterDeclaration ( "values " , ArrayType ( Object ( nullableContext ) ) ) ,
193200 generator . ParameterDeclaration ( "targetType" , ParseTypeName ( "System.Type" ) ) ,
194201 generator . ParameterDeclaration ( "parameter" , Object ( nullableContext ) ) ,
195202 generator . ParameterDeclaration ( "culture" , ParseTypeName ( "System.Globalization.CultureInfo" ) ) ,
196203 } ,
197204 statements : new [ ] { generator . ThrowStatement ( generator . ObjectCreationExpression ( ParseTypeName ( "System.NotImplementedException" ) ) ) } ) ;
198205 }
199206
200- internal static MethodDeclarationSyntax ConvertBack ( string containingTypeName )
207+ internal static MethodDeclarationSyntax ConvertBack ( string containingTypeName , SyntaxGenerator generator , NullableContext nullableContext )
201208 {
202- var code = StringBuilderPool . Borrow ( )
203- . AppendLine ( " object[] System.Windows.Data.IMultiValueConverter.ConvertBack(object value, System.Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)" )
204- . AppendLine ( " {" )
205- . AppendLine ( $ " throw new System.NotSupportedException($\" {{nameof({ containingTypeName } )}} can only be used in OneWay bindings\" );")
206- . AppendLine ( " }" )
207- . Return ( ) ;
208- return ParseMethod ( code ) ;
209-
210- static MethodDeclarationSyntax ParseMethod ( string code )
209+ return ( ( MethodDeclarationSyntax ) generator . MethodDeclaration (
210+ returnType : ArrayType ( Object ( nullableContext ) ) ,
211+ name : "ConvertBack" ,
212+ parameters : new [ ]
213+ {
214+ generator . ParameterDeclaration ( "value" , Object ( nullableContext ) ) ,
215+ generator . ParameterDeclaration ( "targetTypes" , ArrayType ( ParseTypeName ( "System.Type" ) ) ) ,
216+ generator . ParameterDeclaration ( "parameter" , Object ( nullableContext ) ) ,
217+ generator . ParameterDeclaration ( "culture" , ParseTypeName ( "System.Globalization.CultureInfo" ) ) ,
218+ } ,
219+ statements : new [ ] { Throw ( ) } ) )
220+ . WithExplicitInterfaceSpecifier ( SyntaxFactory . ExplicitInterfaceSpecifier ( SyntaxFactory . ParseName ( "System.Windows.Data.IMultiValueConverter" ) . WithSimplifiedNames ( ) ) ) ;
221+
222+ ThrowStatementSyntax Throw ( )
211223 {
212- return Parse . MethodDeclaration ( code )
213- . WithSimplifiedNames ( )
214- . WithLeadingTrivia ( SyntaxFactory . ElasticMarker )
215- . WithTrailingTrivia ( SyntaxFactory . ElasticMarker ) ;
224+ return ( ThrowStatementSyntax ) generator . ThrowStatement (
225+ generator . ObjectCreationExpression (
226+ ParseTypeName ( "System.NotSupportedException" ) ,
227+ SyntaxFactory . Argument (
228+ expression : SyntaxFactory . InterpolatedStringExpression (
229+ stringStartToken : SyntaxFactory . Token ( SyntaxKind . InterpolatedStringStartToken ) ,
230+ contents : SyntaxFactory . List (
231+ new InterpolatedStringContentSyntax [ ]
232+ {
233+ SyntaxFactory . Interpolation (
234+ openBraceToken : SyntaxFactory . Token ( SyntaxKind . OpenBraceToken ) ,
235+ expression : ( ExpressionSyntax ) generator . NameOfExpression ( SyntaxFactory . ParseTypeName ( containingTypeName ) ) ,
236+ alignmentClause : default ,
237+ formatClause : default ,
238+ closeBraceToken : SyntaxFactory . Token ( SyntaxKind . CloseBraceToken ) ) ,
239+ SyntaxFactory . InterpolatedStringText (
240+ textToken : SyntaxFactory . Token (
241+ leading : default ,
242+ kind : SyntaxKind . InterpolatedStringTextToken ,
243+ text : " can only be used in OneWay bindings" ,
244+ valueText : " can only be used in OneWay bindings" ,
245+ trailing : default ) ) ,
246+ } ) ,
247+ stringEndToken : SyntaxFactory . Token ( SyntaxKind . InterpolatedStringEndToken ) ) ) ) ) ;
216248 }
217249 }
218250 }
0 commit comments