Add C# compiler option to add System.SerializableAttribute to generated message classes (#5208)

pull/5222/merge
Sydney Acksman 7 years ago committed by Jie Luo
parent f50a1f843e
commit 2b0a18b270
  1. 2
      src/google/protobuf/compiler/csharp/csharp_generator.cc
  2. 8
      src/google/protobuf/compiler/csharp/csharp_message.cc
  3. 1
      src/google/protobuf/compiler/csharp/csharp_message.h
  4. 6
      src/google/protobuf/compiler/csharp/csharp_options.h

@ -81,6 +81,8 @@ bool Generator::Generate(
cli_options.base_namespace_specified = true; cli_options.base_namespace_specified = true;
} else if (options[i].first == "internal_access") { } else if (options[i].first == "internal_access") {
cli_options.internal_access = true; cli_options.internal_access = true;
} else if (options[i].first == "serializable") {
cli_options.serializable = true;
} else { } else {
*error = "Unknown generator option: " + options[i].first; *error = "Unknown generator option: " + options[i].first;
return false; return false;

@ -42,6 +42,7 @@
#include <google/protobuf/wire_format.h> #include <google/protobuf/wire_format.h>
#include <google/protobuf/wire_format_lite.h> #include <google/protobuf/wire_format_lite.h>
#include <google/protobuf/compiler/csharp/csharp_options.h>
#include <google/protobuf/compiler/csharp/csharp_doc_comment.h> #include <google/protobuf/compiler/csharp/csharp_doc_comment.h>
#include <google/protobuf/compiler/csharp/csharp_enum.h> #include <google/protobuf/compiler/csharp/csharp_enum.h>
#include <google/protobuf/compiler/csharp/csharp_field_base.h> #include <google/protobuf/compiler/csharp/csharp_field_base.h>
@ -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) { void MessageGenerator::Generate(io::Printer* printer) {
std::map<string, string> vars; std::map<string, string> vars;
vars["class_name"] = class_name(); vars["class_name"] = class_name();
@ -112,6 +119,7 @@ void MessageGenerator::Generate(io::Printer* printer) {
WriteMessageDocComment(printer, descriptor_); WriteMessageDocComment(printer, descriptor_);
AddDeprecatedFlag(printer); AddDeprecatedFlag(printer);
AddSerializableAttribute(printer);
printer->Print( printer->Print(
vars, vars,

@ -70,6 +70,7 @@ class MessageGenerator : public SourceGeneratorBase {
bool HasNestedGeneratedTypes(); bool HasNestedGeneratedTypes();
void AddDeprecatedFlag(io::Printer* printer); void AddDeprecatedFlag(io::Printer* printer);
void AddSerializableAttribute(io::Printer* printer);
std::string class_name(); std::string class_name();
std::string full_class_name(); std::string full_class_name();

@ -45,7 +45,8 @@ struct Options {
file_extension(".cs"), file_extension(".cs"),
base_namespace(""), base_namespace(""),
base_namespace_specified(false), base_namespace_specified(false),
internal_access(false) { internal_access(false),
serializable(false) {
} }
// Extension of the generated file. Defaults to ".cs" // Extension of the generated file. Defaults to ".cs"
string file_extension; string file_extension;
@ -68,6 +69,9 @@ struct Options {
// Whether the generated classes should have accessibility level of "internal". // Whether the generated classes should have accessibility level of "internal".
// Defaults to false that generates "public" classes. // Defaults to false that generates "public" classes.
bool internal_access; bool internal_access;
// Whether the generated classes should have a global::System.Serializable attribute added
// Defaults to false
bool serializable;
}; };
} // namespace csharp } // namespace csharp

Loading…
Cancel
Save