Skip to content

Commit aa69497

Browse files
committed
Added overloads for the SmartFormatter.AddExtensions function to accept explicitly ISource or IFormatter instead of a mixture of both.
Marked the old method SmartFormatter.AddExtensions(params object) as obsolete and removed all usages.
1 parent 6187e94 commit aa69497

4 files changed

Lines changed: 63 additions & 27 deletions

File tree

src/SmartFormat.Tests/CodeProjectExampleTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,8 @@ public void EscapingSlashBraces()
464464
{
465465
var Smart = new SmartFormatter();
466466
Smart.Parser.UseAlternativeEscapeChar('\\');
467-
Smart.AddExtensions(
468-
new DefaultFormatter(),
469-
new DefaultSource(Smart));
467+
Smart.AddExtensions(new DefaultFormatter());
468+
Smart.AddExtensions(new DefaultSource(Smart));
470469

471470
var args = new object[] { "Zero", "One", "Two", 3 };
472471

src/SmartFormat.Tests/PerformanceTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public void PerformanceTest_ComparedTo_StringFormat()
5656
FormatCache cache = null;
5757
FormatCache cache2 = null;
5858
var NoExtensions = new SmartFormatter();
59-
NoExtensions.AddExtensions(new DefaultFormatter(), new DefaultSource(NoExtensions));
59+
NoExtensions.AddExtensions(new DefaultSource(NoExtensions));
60+
NoExtensions.AddExtensions(new DefaultFormatter());
6061

6162
var formatters = new[] {
6263
new {

src/SmartFormat/Smart.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,21 @@ public static SmartFormatter CreateDefaultSmartFormat()
6666
// Add all extensions:
6767
// Note, the order is important; the extensions
6868
// will be executed in this order:
69+
70+
var listFormatter = new ListFormatter(result);
71+
6972
result.AddExtensions(
70-
new ListFormatter(result),
71-
new PluralLocalizationFormatter("en"),
72-
new ConditionalFormatter(),
73-
new TimeFormatter("en"),
73+
(ISource)listFormatter,
7474
new ReflectionSource(result),
7575
new DictionarySource(result),
7676
// These default extensions reproduce the String.Format behavior:
77-
new DefaultSource(result),
77+
new DefaultSource(result)
78+
);
79+
result.AddExtensions(
80+
(IFormatter)listFormatter,
81+
new PluralLocalizationFormatter("en"),
82+
new ConditionalFormatter(),
83+
new TimeFormatter("en"),
7884
new DefaultFormatter()
7985
);
8086

src/SmartFormat/SmartFormatter.cs

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,59 @@ public SmartFormatter(ErrorAction errorAction)
4040

4141
public List<ISource> SourceExtensions { get; private set; }
4242
public List<IFormatter> FormatterExtensions { get; private set; }
43+
44+
/// <summary>
45+
/// Adds each extensions to this formatter.
46+
/// Each extension must implement ISource, IFormatter, or both.
47+
///
48+
/// An exception will be thrown if the extension doesn't implement those interfaces.
49+
/// </summary>
50+
/// <param name="extensions"></param>
51+
[Obsolete("Please use the specific overloads of AddExtensions().")]
52+
public void AddExtensions(params object[] extensions)
53+
{
54+
foreach (var extension in extensions)
55+
{
56+
// We need to filter each extension to the correct list:
57+
var source = extension as ISource;
58+
var formatter = extension as IFormatter;
59+
60+
// If this object ISN'T a extension, throw an exception:
61+
if (source == null && formatter == null)
62+
throw new ArgumentException(string.Format("{0} does not implement ISource nor IFormatter.", extension.GetType().FullName), "extensions");
63+
64+
if (source != null)
65+
SourceExtensions.Add(source);
66+
if (formatter != null)
67+
FormatterExtensions.Add(formatter);
68+
}
69+
}
70+
71+
/// <summary>
72+
/// Adds each extensions to this formatter.
73+
/// Each extension must implement ISource.
74+
/// </summary>
75+
/// <param name="sourceExtensions"></param>
76+
public void AddExtensions(params ISource[] sourceExtensions)
77+
{
78+
foreach (var extension in sourceExtensions)
79+
{
80+
if (extension != null)
81+
SourceExtensions.Add(extension);
82+
}
83+
}
84+
4385
/// <summary>
4486
/// Adds each extensions to this formatter.
45-
/// Each extension must implement ISource, IFormatter, or both.
46-
///
47-
/// An exception will be thrown if the extension doesn't implement those interfaces.
87+
/// Each extension must implement IFormatter.
4888
/// </summary>
49-
/// <param name="extensions"></param>
50-
public void AddExtensions(params object[] extensions)
89+
/// <param name="formatterExtensions"></param>
90+
public void AddExtensions(params IFormatter[] formatterExtensions)
5191
{
52-
foreach (var extension in extensions)
92+
foreach (var extension in formatterExtensions)
5393
{
54-
// We need to filter each extension to the correct list:
55-
var source = extension as ISource;
56-
var formatter = extension as IFormatter;
57-
58-
// If this object ISN'T a extension, throw an exception:
59-
if (source == null && formatter == null)
60-
throw new ArgumentException(string.Format("{0} does not implement ISource nor IFormatter.", extension.GetType().FullName), "extensions");
61-
62-
if (source != null)
63-
SourceExtensions.Add(source);
64-
if (formatter != null)
65-
FormatterExtensions.Add(formatter);
94+
if (extension != null)
95+
FormatterExtensions.Add(extension);
6696
}
6797
}
6898

0 commit comments

Comments
 (0)