From 2b0a18b270487bbacd8be2308f08d2efcf7cfe6e Mon Sep 17 00:00:00 2001 From: Sydney Acksman Date: Fri, 5 Oct 2018 13:06:02 -0500 Subject: [PATCH] Add C# compiler option to add System.SerializableAttribute to generated message classes (#5208) --- src/google/protobuf/compiler/csharp/csharp_generator.cc | 2 ++ src/google/protobuf/compiler/csharp/csharp_message.cc | 8 ++++++++ src/google/protobuf/compiler/csharp/csharp_message.h | 1 + src/google/protobuf/compiler/csharp/csharp_options.h | 6 +++++- 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/google/protobuf/compiler/csharp/csharp_generator.cc b/src/google/protobuf/compiler/csharp/csharp_generator.cc index c0597fe434..d23fdb0d02 100644 --- a/src/google/protobuf/compiler/csharp/csharp_generator.cc +++ b/src/google/protobuf/compiler/csharp/csharp_generator.cc @@ -81,6 +81,8 @@ bool Generator::Generate( cli_options.base_namespace_specified = true; } else if (options[i].first == "internal_access") { cli_options.internal_access = true; + } else if (options[i].first == "serializable") { + cli_options.serializable = true; } else { *error = "Unknown generator option: " + options[i].first; return false; diff --git a/src/google/protobuf/compiler/csharp/csharp_message.cc b/src/google/protobuf/compiler/csharp/csharp_message.cc index 1daae6f595..124e673b35 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message.cc +++ b/src/google/protobuf/compiler/csharp/csharp_message.cc @@ -42,6 +42,7 @@ #include #include +#include #include #include #include @@ -105,6 +106,12 @@ void MessageGenerator::AddDeprecatedFlag(io::Printer* printer) { } } +void MessageGenerator::AddSerializableAttribute(io::Printer* printer) { + if (this->options()->serializable) { + printer->Print("[global::System.SerializableAttribute]\n"); + } +} + void MessageGenerator::Generate(io::Printer* printer) { std::map vars; vars["class_name"] = class_name(); @@ -112,6 +119,7 @@ void MessageGenerator::Generate(io::Printer* printer) { WriteMessageDocComment(printer, descriptor_); AddDeprecatedFlag(printer); + AddSerializableAttribute(printer); printer->Print( vars, diff --git a/src/google/protobuf/compiler/csharp/csharp_message.h b/src/google/protobuf/compiler/csharp/csharp_message.h index b20bec3d8b..5e0ac960d8 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message.h +++ b/src/google/protobuf/compiler/csharp/csharp_message.h @@ -70,6 +70,7 @@ class MessageGenerator : public SourceGeneratorBase { bool HasNestedGeneratedTypes(); void AddDeprecatedFlag(io::Printer* printer); + void AddSerializableAttribute(io::Printer* printer); std::string class_name(); std::string full_class_name(); diff --git a/src/google/protobuf/compiler/csharp/csharp_options.h b/src/google/protobuf/compiler/csharp/csharp_options.h index 426fb3b50f..9629052340 100644 --- a/src/google/protobuf/compiler/csharp/csharp_options.h +++ b/src/google/protobuf/compiler/csharp/csharp_options.h @@ -45,7 +45,8 @@ struct Options { file_extension(".cs"), base_namespace(""), base_namespace_specified(false), - internal_access(false) { + internal_access(false), + serializable(false) { } // Extension of the generated file. Defaults to ".cs" string file_extension; @@ -68,6 +69,9 @@ struct Options { // Whether the generated classes should have accessibility level of "internal". // Defaults to false that generates "public" classes. bool internal_access; + // Whether the generated classes should have a global::System.Serializable attribute added + // Defaults to false + bool serializable; }; } // namespace csharp