axuno / SmartFormat

A lightweight text templating library written in C# which can be a drop-in replacement for string.Format
Other
1.1k stars 105 forks source link

Dictionary element with string key that has period is not found #53

Closed DDtMM closed 7 years ago

DDtMM commented 9 years ago

Best explained with example:

var a = Smart.Format("{SomeKey}", new Dictionary<string, object>() { { "SomeKey", 999 } }); Output: "999"

var b = Smart.Format("{Some.Key}", new Dictionary<string, object>(){ { "Some.Key", 999 } } ); Output: "", Expected Output: "999"

It seems the presence of ., [, or ] cause the engine to check for a property within that path instead of trying to find an existing key. The best behavior would probably be for it to try both if a dictionary is keyed by strings.

To make matters slightly more complicated if I have a key of Count, I get the Property value. It seems to me that in the presence of a dictionary that the existence of a key should be checked first.

var b = Smart.Format("{Count}", new Dictionary<string, object>(){ { "Count", 999 } } ); Output: "1", Expected Output: "999"

axunonb commented 7 years ago

With all the flexibility SmartFormat.Net has included, there are some restrictions. This one mentioned here should (an will) be mentioned in the docs: Don't use a dot in a Dictionary key.