diff --git a/FormatWith/Internal/FormatWithMethods.cs b/FormatWith/Internal/FormatWithMethods.cs index b908b22..35dfa10 100644 --- a/FormatWith/Internal/FormatWithMethods.cs +++ b/FormatWith/Internal/FormatWithMethods.cs @@ -160,14 +160,40 @@ public static IEnumerable GetFormatParameters( private static ReplacementResult FromReplacementObject(string key, object replacementObject) { - PropertyInfo propertyInfo = replacementObject.GetType().GetProperty(key, propertyBindingFlags); - if (propertyInfo == null) + // need to split this into accessors so we can traverse nested objects + var members = key.Split(new[] { "." }, StringSplitOptions.None); + if (members.Length == 1) { - return new ReplacementResult(false, null); + PropertyInfo propertyInfo = replacementObject.GetType().GetProperty(key, propertyBindingFlags); + + if (propertyInfo == null) + { + return new ReplacementResult(false, null); + } + else + { + return new ReplacementResult(true, propertyInfo.GetValue(replacementObject)); + } } else { - return new ReplacementResult(true, propertyInfo.GetValue(replacementObject)); + object currentObject = replacementObject; + + foreach (var member in members) + { + PropertyInfo propertyInfo = currentObject.GetType().GetProperty(member, propertyBindingFlags); + + if (propertyInfo == null) + { + return new ReplacementResult(false, null); + } + else + { + currentObject = propertyInfo.GetValue(currentObject); + } + } + + return new ReplacementResult(true, currentObject); } } } diff --git a/FormatWithTests/FormatWithTests.cs b/FormatWithTests/FormatWithTests.cs index 817db41..1046025 100644 --- a/FormatWithTests/FormatWithTests.cs +++ b/FormatWithTests/FormatWithTests.cs @@ -30,6 +30,13 @@ public void TestReplacement3() Assert.Equal(TestFormat4Solution, replacement); } + [Fact] + public void TestNestedPropertiesReplacements() + { + string replacement = TestFormat5.FormatWith(new { Foo = new { Replacement1 = Replacement1 } }); + Assert.Equal(TestFormat5Solution, replacement); + } + [Fact] public void TestUnexpectedOpenBracketError() { diff --git a/FormatWithTests/FormattableWithTests.cs b/FormatWithTests/FormattableWithTests.cs index 483f65f..34f6a30 100644 --- a/FormatWithTests/FormattableWithTests.cs +++ b/FormatWithTests/FormattableWithTests.cs @@ -48,6 +48,16 @@ public void TestReplacement4() Assert.Equal(TestFormat4Solution, formattableString.ToString()); } + [Fact] + public void TestNestedProperties() + { + FormattableString formattableString = TestFormat5.FormattableWith(new { Foo = new { Replacement1 = Replacement1 } }); + Assert.Equal(TestFormat5Composite, formattableString.Format); + Assert.Equal(1, formattableString.ArgumentCount); + Assert.Equal(Replacement1, formattableString.GetArgument(0)); + Assert.Equal(TestFormat5Solution, formattableString.ToString()); + } + [Fact] public void TestCustomBraces() { diff --git a/FormatWithTests/TestStrings.cs b/FormatWithTests/TestStrings.cs index a2657cf..c62e0dd 100644 --- a/FormatWithTests/TestStrings.cs +++ b/FormatWithTests/TestStrings.cs @@ -20,5 +20,9 @@ public static class TestStrings public static readonly string TestFormat4 = "abc{Replacement1}def{{escaped1}}ghi{{{Replacement2}}}jkl{{{{escaped2}}}}mno"; public static readonly string TestFormat4Composite = "abc{0}def{{escaped1}}ghi{{{1}}}jkl{{{{escaped2}}}}mno"; public static readonly string TestFormat4Solution = $"abc{Replacement1}def{{escaped1}}ghi{{{Replacement2}}}jkl{{{{escaped2}}}}mno"; + + public static readonly string TestFormat5 = "abc{Foo.Replacement1}"; + public static readonly string TestFormat5Composite = "abc{0}"; + public static readonly string TestFormat5Solution = $"abc{Replacement1}"; } }