mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-11 15:37:27 +00:00
[Issue 252] Add type cast for default enum values in C#
When creating a CreateXxx(...) method for a simple table type, enum-type fields with a non-zero default must have an explicit cast for the respective argument default value, because in C#, there is an implicit cast from int to an enum only for 0. Also, added an example of such type into the example monster_test type, so that we test this feature.
This commit is contained in:
@@ -789,7 +789,15 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
|
||||
// Java doesn't have defaults, which means this method must always
|
||||
// supply all arguments, and thus won't compile when fields are added.
|
||||
if (lang.language != GeneratorOptions::kJava) {
|
||||
code += " = " + GenDefaultValue(lang, field.value, false);
|
||||
code += " = ";
|
||||
// in C#, enum values have their own type, so we need to cast the
|
||||
// numeric value to the proper type
|
||||
if (lang.language == GeneratorOptions::kCSharp &&
|
||||
field.value.type.enum_def != nullptr &&
|
||||
field.value.type.base_type != BASE_TYPE_UNION) {
|
||||
code += "(" + field.value.type.enum_def->name + ")";
|
||||
}
|
||||
code += GenDefaultValue(lang, field.value, false);
|
||||
}
|
||||
}
|
||||
code += ") {\n builder.";
|
||||
|
||||
Reference in New Issue
Block a user