diff --git a/src/ProtocolBuffers.Test/MessageTest.cs b/src/ProtocolBuffers.Test/MessageTest.cs
index f27865b07b..8bb0fac7dd 100644
--- a/src/ProtocolBuffers.Test/MessageTest.cs
+++ b/src/ProtocolBuffers.Test/MessageTest.cs
@@ -141,6 +141,13 @@ namespace Google.ProtocolBuffers
Assert.IsTrue(builder.IsInitialized);
}
+ [TestMethod]
+ public void UninitializedBuilderToString()
+ {
+ TestRequired.Builder builder = TestRequired.CreateBuilder().SetA(1);
+ Assert.AreEqual("a: 1\n", builder.ToString());
+ }
+
[TestMethod]
public void RequiredForeign()
{
diff --git a/src/ProtocolBuffers.Test/TextFormatTest.cs b/src/ProtocolBuffers.Test/TextFormatTest.cs
index 2cdd9ce288..37a4192a92 100644
--- a/src/ProtocolBuffers.Test/TextFormatTest.cs
+++ b/src/ProtocolBuffers.Test/TextFormatTest.cs
@@ -99,6 +99,20 @@ namespace Google.ProtocolBuffers
});
}
+ ///
+ /// Tests that a builder prints the same way as a message.
+ ///
+ [TestMethod]
+ public void PrintBuilder()
+ {
+ TestUtil.TestInMultipleCultures(() =>
+ {
+ string messageText = TextFormat.PrintToString(TestUtil.GetAllSet());
+ string builderText = TextFormat.PrintToString(TestUtil.GetAllSet().ToBuilder());
+ Assert.AreEqual(messageText, builderText);
+ });
+ }
+
///
/// Print TestAllExtensions and compare with golden file.
///
diff --git a/src/ProtocolBuffers/AbstractBuilder.cs b/src/ProtocolBuffers/AbstractBuilder.cs
index 47d84a3563..e7a41fb3ed 100644
--- a/src/ProtocolBuffers/AbstractBuilder.cs
+++ b/src/ProtocolBuffers/AbstractBuilder.cs
@@ -249,5 +249,18 @@ namespace Google.ProtocolBuffers
}
#endregion
+
+ ///
+ /// Converts this builder to a string using .
+ ///
+ ///
+ /// This method is not sealed (in the way that it is in
+ /// as it was added after earlier releases; some other implementations may already be overriding the
+ /// method.
+ ///
+ public override string ToString()
+ {
+ return TextFormat.PrintToString(this);
+ }
}
}
\ No newline at end of file
diff --git a/src/ProtocolBuffers/TextFormat.cs b/src/ProtocolBuffers/TextFormat.cs
index 7ea25053b2..747dce4ead 100644
--- a/src/ProtocolBuffers/TextFormat.cs
+++ b/src/ProtocolBuffers/TextFormat.cs
@@ -61,6 +61,16 @@ namespace Google.ProtocolBuffers
Print(message, generator);
}
+ ///
+ /// Outputs a textual representation of the Protocol Message builder supplied into
+ /// the parameter output.
+ ///
+ public static void Print(IBuilder builder, TextWriter output)
+ {
+ TextGenerator generator = new TextGenerator(output, "\n");
+ Print(builder, generator);
+ }
+
///
/// Outputs a textual representation of to .
///
@@ -77,6 +87,13 @@ namespace Google.ProtocolBuffers
return text.ToString();
}
+ public static string PrintToString(IBuilder builder)
+ {
+ StringWriter text = new StringWriter();
+ Print(builder, text);
+ return text.ToString();
+ }
+
public static string PrintToString(UnknownFieldSet fields)
{
StringWriter text = new StringWriter();
@@ -93,6 +110,15 @@ namespace Google.ProtocolBuffers
PrintUnknownFields(message.UnknownFields, generator);
}
+ private static void Print(IBuilder message, TextGenerator generator)
+ {
+ foreach (KeyValuePair entry in message.AllFields)
+ {
+ PrintField(entry.Key, entry.Value, generator);
+ }
+ PrintUnknownFields(message.UnknownFields, generator);
+ }
+
internal static void PrintField(FieldDescriptor field, object value, TextGenerator generator)
{
if (field.IsRepeated)