Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions FormatWith/Internal/FormatWithMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,40 @@ public static IEnumerable<string> 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);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions FormatWithTests/FormatWithTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
10 changes: 10 additions & 0 deletions FormatWithTests/FormattableWithTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
4 changes: 4 additions & 0 deletions FormatWithTests/TestStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}
}