Commit 2b0a18b2 authored by Sydney Acksman's avatar Sydney Acksman Committed by Jie Luo

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

parent f50a1f84
...@@ -81,6 +81,8 @@ bool Generator::Generate( ...@@ -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 @@ ...@@ -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) { ...@@ -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) { ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment