How do I escape the dot in the indexer? The first DataBinder.Eval works as expected. The second throws an exception.
System.ArgumentException: DataBinding: 'dict['a' is not a valid indexed expression. Dictionary<string, int> dict = new Dictionary<string, int>(); dict.Add('aaa', 111); dict.Add('bbb', 222); dict.Add('ccc', 333); dict.Add('ddd', 444); dict.Add('a.aa', 555); var blah = new { dict = dict, date = DateTime.Now }; Console.WriteLine(DataBinder.Eval(blah, 'dict[\'aaa\']')); // 111 Console.WriteLine(DataBinder.Eval(blah, 'dict[\'a.aa\']')); // System.ArgumentException: DataBinding: 'dict['a' is not a valid indexed expression.
DataBinder.Eval first splits a string up by expression parts via a set array of char tokens (which it defines in a static constructor as):
It then passes these parts into a private Eval method that will either use DataBinder.GetPropertyValue or DataBinder.GetIndexedPropertyValue as needed to further determine the value of the expression.
To bypass this, just use GetIndexedPropertyValue directly with your string expression like so:
Also note that you don’t need the extra quotes… they are overkill.