Skip to content

Issue adding a custom selector in 3.2.1 #334

Closed

Description

@mg72266

Hello,

I am upgrading SmartFormat to 3.2.1 from 2.7.2 and I am running into an issue adding a custom selector char. This is for replacing values in an XML, where the replacement key uses the $ sign to indicate the text that will get replaced.

In 2.7.2 I have code that looks like this,

var format = "{$number1$}";
Smart.Default.Parser.AddAdditionalSelectorChars(" $");
IDictionary<string, object> templateData = new Dictionary<string, object>();
templateData.Add("$number1$", "test");
Console.WriteLine(Smart.Format(format, templateData)); // prints: test, works as expected

In 3.2.1, I have modified the code to use the new AddCustomSelectorChars, so the code looks as follows,

var format = "{$number1$}";
Smart.Default.Settings.Parser.AddCustomSelectorChars(" $".ToCharArray());
IDictionary<string, object> templateData = new Dictionary<string, object>();
templateData.Add("$number1$", "test");
Console.WriteLine(Smart.Format(format, templateData)); 
// exception on the Format function that says,
// SmartFormat.Core.Parsing.ParsingErrors: 'The format string has 2 issues:
// '0x24': Invalid character in the selector, '0x24': Invalid character in the selector
// In: "{$number1$}"

Is there away to get this to work in 3.2.1?

Activity

  1. axunonb commented on Mar 23, 2023

    @axunonb
    Member

    This does not work:

    Smart.Default.Settings.Parser.AddCustomSelectorChars(" $".ToCharArray());

    As pointed out in the Wiki, changes to settings after the instances are created won't be processed. But this is what happens when calling Smart.Default: it creates default SmartSettings.

    This works:

    Console.WriteLine(Smart.CreateDefaultSmartFormat(Smart.Default.Settings).Format(format, templateData));
  2. locked and limited conversation to collaborators on Mar 24, 2023
  3. converted this issue into a discussion #336 on Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions