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:
| privatestaticboolTryGetPropertyFromObject(stringkey,objectreplacementObject,outobjectvalue) |
| { |
| // need to split this into accessors so we can traverse nested objects |
| varmembers=key.Split(new[]{"."},StringSplitOptions.None); |
| if(members.Length==1) |
| { |
| PropertyInfopropertyInfo=replacementObject.GetType().GetProperty(key,propertyBindingFlags); |
| |
| if(propertyInfo==null) |
| { |
| value=null; |
| returnfalse; |
| } |
| else |
| { |
| value=propertyInfo.GetValue(replacementObject); |
| returntrue; |
| } |
| } |
| else |
| { |
| objectcurrentObject=replacementObject; |
| |
| foreach(varmemberinmembers) |
| { |
| PropertyInfopropertyInfo=currentObject.GetType().GetProperty(member,propertyBindingFlags); |
| |
| if(propertyInfo==null) |
| { |
| value=null; |
| returnfalse; |
| } |
| else |
| { |
| currentObject=propertyInfo.GetValue(currentObject); |
| } |
| } |
| |
| value=currentObject; |
| returntrue; |
| } |
| } |
At present,
FormatWithsupports 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:
However, we should be able to do this:
This is the code that will need to be changed:
FormatWith/FormatWith/Internal/FormatWithMethods.cs
Lines 320 to 361 in 3fbb5ee