-
Notifications
You must be signed in to change notification settings - Fork 13
Open
Labels
Milestone
Description
At present, FormatWith supports basic property navigations in parameter keys. However, it does not support combining this with indexers to navigate into objects arrays, lists, or dictionaries.
For example, this is currently supported:
string result = "{foo.bar}".FormatWith(new
{
foo = new { bar = "test" }
});
However, we should be able to do this:
string result = "{foo[\"bar\"].Length}".FormatWith(new
{
foo = new Dictionary<string, string> { ["bar"] = "test" }
});
This is the code that will need to be changed:
FormatWith/FormatWith/Internal/FormatWithMethods.cs
Lines 320 to 361 in 3fbb5ee
| private static bool TryGetPropertyFromObject(string key, object replacementObject, out object value) | |
| { | |
| // need to split this into accessors so we can traverse nested objects | |
| var members = key.Split(new[] { "." }, StringSplitOptions.None); | |
| if (members.Length == 1) | |
| { | |
| PropertyInfo propertyInfo = replacementObject.GetType().GetProperty(key, propertyBindingFlags); | |
| if (propertyInfo == null) | |
| { | |
| value = null; | |
| return false; | |
| } | |
| else | |
| { | |
| value = propertyInfo.GetValue(replacementObject); | |
| return true; | |
| } | |
| } | |
| else | |
| { | |
| object currentObject = replacementObject; | |
| foreach (var member in members) | |
| { | |
| PropertyInfo propertyInfo = currentObject.GetType().GetProperty(member, propertyBindingFlags); | |
| if (propertyInfo == null) | |
| { | |
| value = null; | |
| return false; | |
| } | |
| else | |
| { | |
| currentObject = propertyInfo.GetValue(currentObject); | |
| } | |
| } | |
| value = currentObject; | |
| return true; | |
| } | |
| } |