@@ -68,9 +68,12 @@ void Execute(IncrementalValuesProvider<ISymbol> types, bool skipDiagnostics = fa
6868 if ( x . AttributeData . AttributeClass . TypeArguments . FirstOrDefault ( ) is { } typeSymbol )
6969 {
7070 var parameters = x . AttributeData . ConstructorArguments . Select ( GeneratorExtensions . PrepareParameterTypeForActivator ) . ToList ( ) ;
71- var members = typeSymbol . GetMembers ( ) . OfType < IFieldSymbol > ( ) . Select ( t => ( t . Name , t . ConstantValue ! ) ) . ToList ( ) ;
71+ var members = typeSymbol . GetMembers ( ) . OfType < IFieldSymbol > ( ) . Select ( t => new MultiChoiceOption ( t . Name , t . ToDisplayString ( ) ) ) . ToArray ( ) ;
72+ parameters . Add ( typeSymbol . ToDisplayString ( ) ) ;
7273 parameters . Add ( members ) ;
73- var multiChoiceOptionAttribute = ( ToolkitSampleMultiChoiceOptionAttribute ) Activator . CreateInstance ( typeof ( ToolkitSampleMultiChoiceOptionAttribute ) , parameters . ToArray ( ) ) ;
74+ var multiChoiceOptionAttribute = ( ToolkitSampleMultiChoiceOptionAttribute ) Activator . CreateInstance (
75+ typeof ( ToolkitSampleMultiChoiceOptionAttribute ) , BindingFlags . NonPublic | BindingFlags . Instance ,
76+ null , parameters . ToArray ( ) , null ) ;
7477 item = ( x . Symbol , multiChoiceOptionAttribute ) ;
7578 }
7679 }
@@ -298,7 +301,7 @@ private static void ReportGeneratedMultiChoiceOptionsPaneDiagnostics(SourceProdu
298301 {
299302 foreach ( var item in generatedOptionPropertyData )
300303 {
301- if ( item . Item2 is ToolkitSampleMultiChoiceOptionAttribute multiChoiceAttr && multiChoiceAttr . Choices . Length == 0 )
304+ if ( item . Item2 is ToolkitSampleMultiChoiceOptionAttribute { Choices . Length : 0 } )
302305 {
303306 ctx . ReportDiagnostic ( Diagnostic . Create ( DiagnosticDescriptors . SamplePaneMultiChoiceOptionWithNoChoices , item . Item1 . Locations . FirstOrDefault ( ) , item . Item2 . Title ) ) ;
304307 }
@@ -315,7 +318,7 @@ public static class ToolkitSampleRegistry
315318 {
316319 public static System.Collections.Generic.Dictionary<string, {{ typeof ( ToolkitSampleMetadata ) . FullName }} > Listing { get; } = new()
317320 {
318- {{ string . Join ( ",\n " , sampleMetadata . Select ( MetadataToRegistryCall ) . ToArray ( ) ) }}
321+ {{ string . Join ( ",\n " , sampleMetadata . Select ( MetadataToRegistryCall ) . ToArray ( ) ) }}
319322 };
320323 }
321324 """ ;
@@ -326,11 +329,16 @@ private static string MetadataToRegistryCall(KeyValuePair<string, ToolkitSampleR
326329 var metadata = kvp . Value ;
327330 var sampleControlTypeParam = $ "typeof({ metadata . SampleAssemblyQualifiedName } )";
328331 var sampleControlFactoryParam = $ "() => new { metadata . SampleAssemblyQualifiedName } ()";
329- var generatedSampleOptionsParam = $ "new { typeof ( IGeneratedToolkitSampleOptionViewModel ) . FullName } [] {{ { string . Join ( ", " , BuildNewGeneratedSampleOptionMetadataSource ( metadata ) . ToArray ( ) ) } }}";
330332 var sampleOptionsParam = metadata . SampleOptionsAssemblyQualifiedName is null ? "null" : $ "typeof({ metadata . SampleOptionsAssemblyQualifiedName } )";
331333 var sampleOptionsPaneFactoryParam = metadata . SampleOptionsAssemblyQualifiedName is null ? "null" : $ "x => new { metadata . SampleOptionsAssemblyQualifiedName } (({ metadata . SampleAssemblyQualifiedName } )x)";
332334
333- return @$ "[""{ kvp . Key } ""] = new { typeof ( ToolkitSampleMetadata ) . FullName } (""{ metadata . Id } "", ""{ metadata . DisplayName } "", ""{ metadata . Description } "", { sampleControlTypeParam } , { sampleControlFactoryParam } , { sampleOptionsParam } , { sampleOptionsPaneFactoryParam } , { generatedSampleOptionsParam } )";
335+ return $$ """
336+ ["{{ kvp . Key }} "] = new {{ typeof ( ToolkitSampleMetadata ) . FullName }} ("{{ metadata . Id }} ", "{{ metadata . DisplayName }} ", "{{ metadata . Description }} ", {{ sampleControlTypeParam }} , {{ sampleControlFactoryParam }} , {{ sampleOptionsParam }} , {{ sampleOptionsPaneFactoryParam }} ,
337+ new {{ typeof ( IGeneratedToolkitSampleOptionViewModel ) . FullName }} []
338+ {
339+ {{ string . Join ( ",\n " , BuildNewGeneratedSampleOptionMetadataSource ( metadata ) . ToArray ( ) ) }}
340+ })
341+ """ ;
334342 }
335343
336344 private static IEnumerable < string > BuildNewGeneratedSampleOptionMetadataSource ( ToolkitSampleRecord sample )
@@ -340,13 +348,22 @@ private static IEnumerable<string> BuildNewGeneratedSampleOptionMetadataSource(T
340348 yield return item switch
341349 {
342350 ToolkitSampleMultiChoiceOptionAttribute multiChoiceAttr =>
343- $@ "new { typeof ( ToolkitSampleMultiChoiceOptionMetadataViewModel ) . FullName } (name: ""{ multiChoiceAttr . Name } "", options: new[] {{ { string . Join ( "," , multiChoiceAttr . Choices . Select ( x => $@ "new { typeof ( MultiChoiceOption ) . FullName } (""{ x . Label } "", { ( x . Value is string ? $ "\" { x . Value } \" " : x . Value ) } )") . ToArray ( ) ) } }}, title: ""{ multiChoiceAttr . Title } "")",
351+ $$ """
352+ new {{ typeof ( ToolkitSampleMultiChoiceOptionMetadataViewModel ) . FullName }} (name: "{{ multiChoiceAttr . Name }} ",
353+ options: new[]
354+ {
355+ {{ string . Join ( ",\n " , multiChoiceAttr . Choices . Select ( x =>
356+ $ """
357+ new { typeof ( MultiChoiceOption ) . FullName } ("{ x . Label } ", { ( multiChoiceAttr . TypeName is "string" ? $ "\" { x . Value } \" " : x . Value ) } )
358+ """ ) . ToArray ( ) ) }}
359+ }, title: "{{ multiChoiceAttr . Title }} ")
360+ """ ,
344361 ToolkitSampleBoolOptionAttribute boolAttribute =>
345- $@ "new { typeof ( ToolkitSampleBoolOptionMetadataViewModel ) . FullName } (name: ""{ boolAttribute . Name } "", defaultState: { boolAttribute . DefaultState ? . ToString ( ) . ToLower ( ) } , title: ""{ boolAttribute . Title } "")",
362+ $@ " new { typeof ( ToolkitSampleBoolOptionMetadataViewModel ) . FullName } (name: ""{ boolAttribute . Name } "", defaultState: { boolAttribute . DefaultState ? . ToString ( ) . ToLower ( ) } , title: ""{ boolAttribute . Title } "")",
346363 ToolkitSampleNumericOptionAttribute numericAttribute =>
347- $@ "new { typeof ( ToolkitSampleNumericOptionMetadataViewModel ) . FullName } (name: ""{ numericAttribute . Name } "", initial: { numericAttribute . Initial } , min: { numericAttribute . Min } , max: { numericAttribute . Max } , step: { numericAttribute . Step } , showAsNumberBox: { numericAttribute . ShowAsNumberBox . ToString ( ) . ToLower ( ) } , title: ""{ numericAttribute . Title } "")",
364+ $@ " new { typeof ( ToolkitSampleNumericOptionMetadataViewModel ) . FullName } (name: ""{ numericAttribute . Name } "", initial: { numericAttribute . Initial } , min: { numericAttribute . Min } , max: { numericAttribute . Max } , step: { numericAttribute . Step } , showAsNumberBox: { numericAttribute . ShowAsNumberBox . ToString ( ) . ToLower ( ) } , title: ""{ numericAttribute . Title } "")",
348365 ToolkitSampleTextOptionAttribute textAttribute =>
349- $@ "new { typeof ( ToolkitSampleTextOptionMetadataViewModel ) . FullName } (name: ""{ textAttribute . Name } "", placeholderText: ""{ textAttribute . PlaceholderText } "", title: ""{ textAttribute . Title } "")",
366+ $@ " new { typeof ( ToolkitSampleTextOptionMetadataViewModel ) . FullName } (name: ""{ textAttribute . Name } "", placeholderText: ""{ textAttribute . PlaceholderText } "", title: ""{ textAttribute . Title } "")",
350367 _ => throw new NotSupportedException ( $ "Unsupported or unhandled type { item . GetType ( ) } .")
351368 } ;
352369 }
0 commit comments