diff --git a/src/idl_gen_csharp.cpp b/src/idl_gen_csharp.cpp index f98df4cc7..d2f14f094 100644 --- a/src/idl_gen_csharp.cpp +++ b/src/idl_gen_csharp.cpp @@ -618,8 +618,9 @@ class CSharpGenerator : public BaseGenerator { std::string dest_mask = ""; std::string dest_cast = DestinationCast(field.value.type); std::string src_cast = SourceCast(field.value.type); - std::string method_start = " public " + type_name_dest + optional + " " + - MakeCamel(field.name, true); + std::string field_name_camel = MakeCamel(field.name, true); + std::string method_start = + " public " + type_name_dest + optional + " " + field_name_camel; std::string obj = "(new " + type_name + "())"; // Most field accessors need to retrieve and test the field offset first, @@ -763,6 +764,30 @@ class CSharpGenerator : public BaseGenerator { code += offset_prefix + GenGetter(Type(BASE_TYPE_STRING)); code += "(o + __p.bb_pos) : null"; } + // As<> accesors for Unions + // Loop through all the possible union types and generate an As + // accessor that casts to the correct type. + for (auto uit = field.value.type.enum_def->Vals().begin(); + uit != field.value.type.enum_def->Vals().end(); ++uit) { + auto val = *uit; + if (val->union_type.base_type == BASE_TYPE_NONE) { continue; } + auto union_field_type_name = GenTypeGet(val->union_type); + code += member_suffix + "}\n"; + if (val->union_type.base_type == BASE_TYPE_STRUCT && + val->union_type.struct_def->attributes.Lookup("private")) { + code += " internal "; + } else { + code += " public "; + } + code += union_field_type_name + " "; + code += field_name_camel + "As" + val->name + "() { return "; + code += field_name_camel; + if (IsString(val->union_type)) { + code += "AsString()"; + } else { + code += "<" + union_field_type_name + ">().Value"; + } + } break; default: FLATBUFFERS_ASSERT(0); } diff --git a/tests/MyGame/Example/Monster.cs b/tests/MyGame/Example/Monster.cs index ddbc45e76..acfb3943e 100644 --- a/tests/MyGame/Example/Monster.cs +++ b/tests/MyGame/Example/Monster.cs @@ -46,6 +46,9 @@ public struct Monster : IFlatbufferObject public bool MutateColor(MyGame.Example.Color color) { int o = __p.__offset(16); if (o != 0) { __p.bb.Put(o + __p.bb_pos, (byte)color); return true; } else { return false; } } public MyGame.Example.Any TestType { get { int o = __p.__offset(18); return o != 0 ? (MyGame.Example.Any)__p.bb.Get(o + __p.bb_pos) : MyGame.Example.Any.NONE; } } public TTable? Test() where TTable : struct, IFlatbufferObject { int o = __p.__offset(20); return o != 0 ? (TTable?)__p.__union(o + __p.bb_pos) : null; } + public MyGame.Example.Monster TestAsMonster() { return Test().Value; } + internal MyGame.Example.TestSimpleTableWithEnum TestAsTestSimpleTableWithEnum() { return Test().Value; } + public MyGame.Example2.Monster TestAsMyGame_Example2_Monster() { return Test().Value; } public MyGame.Example.Test? Test4(int j) { int o = __p.__offset(22); return o != 0 ? (MyGame.Example.Test?)(new MyGame.Example.Test()).__assign(__p.__vector(o) + j * 4, __p.bb) : null; } public int Test4Length { get { int o = __p.__offset(22); return o != 0 ? __p.__vector_len(o) : 0; } } public string Testarrayofstring(int j) { int o = __p.__offset(24); return o != 0 ? __p.__string(__p.__vector(o) + j * 4) : null; } @@ -175,8 +178,14 @@ public struct Monster : IFlatbufferObject public bool MutateVectorOfNonOwningReferences(int j, ulong vector_of_non_owning_references) { int o = __p.__offset(88); if (o != 0) { __p.bb.PutUlong(__p.__vector(o) + j * 8, vector_of_non_owning_references); return true; } else { return false; } } public MyGame.Example.AnyUniqueAliases AnyUniqueType { get { int o = __p.__offset(90); return o != 0 ? (MyGame.Example.AnyUniqueAliases)__p.bb.Get(o + __p.bb_pos) : MyGame.Example.AnyUniqueAliases.NONE; } } public TTable? AnyUnique() where TTable : struct, IFlatbufferObject { int o = __p.__offset(92); return o != 0 ? (TTable?)__p.__union(o + __p.bb_pos) : null; } + public MyGame.Example.Monster AnyUniqueAsM() { return AnyUnique().Value; } + internal MyGame.Example.TestSimpleTableWithEnum AnyUniqueAsTS() { return AnyUnique().Value; } + public MyGame.Example2.Monster AnyUniqueAsM2() { return AnyUnique().Value; } public MyGame.Example.AnyAmbiguousAliases AnyAmbiguousType { get { int o = __p.__offset(94); return o != 0 ? (MyGame.Example.AnyAmbiguousAliases)__p.bb.Get(o + __p.bb_pos) : MyGame.Example.AnyAmbiguousAliases.NONE; } } public TTable? AnyAmbiguous() where TTable : struct, IFlatbufferObject { int o = __p.__offset(96); return o != 0 ? (TTable?)__p.__union(o + __p.bb_pos) : null; } + public MyGame.Example.Monster AnyAmbiguousAsM1() { return AnyAmbiguous().Value; } + public MyGame.Example.Monster AnyAmbiguousAsM2() { return AnyAmbiguous().Value; } + public MyGame.Example.Monster AnyAmbiguousAsM3() { return AnyAmbiguous().Value; } public MyGame.Example.Color VectorOfEnums(int j) { int o = __p.__offset(98); return o != 0 ? (MyGame.Example.Color)__p.bb.Get(__p.__vector(o) + j * 1) : (MyGame.Example.Color)0; } public int VectorOfEnumsLength { get { int o = __p.__offset(98); return o != 0 ? __p.__vector_len(o) : 0; } } #if ENABLE_SPAN_T diff --git a/tests/union_vector/Movie.cs b/tests/union_vector/Movie.cs index 2358600fd..dfbdd8d93 100644 --- a/tests/union_vector/Movie.cs +++ b/tests/union_vector/Movie.cs @@ -20,6 +20,12 @@ public struct Movie : IFlatbufferObject public Character MainCharacterType { get { int o = __p.__offset(4); return o != 0 ? (Character)__p.bb.Get(o + __p.bb_pos) : Character.NONE; } } public TTable? MainCharacter() where TTable : struct, IFlatbufferObject { int o = __p.__offset(6); return o != 0 ? (TTable?)__p.__union(o + __p.bb_pos) : null; } public string MainCharacterAsString() { int o = __p.__offset(6); return o != 0 ? __p.__string(o + __p.bb_pos) : null; } + public Attacker MainCharacterAsMuLan() { return MainCharacter().Value; } + public Rapunzel MainCharacterAsRapunzel() { return MainCharacter().Value; } + public BookReader MainCharacterAsBelle() { return MainCharacter().Value; } + public BookReader MainCharacterAsBookFan() { return MainCharacter().Value; } + public string MainCharacterAsOther() { return MainCharacterAsString(); } + public string MainCharacterAsUnused() { return MainCharacterAsString(); } public Character CharactersType(int j) { int o = __p.__offset(8); return o != 0 ? (Character)__p.bb.Get(__p.__vector(o) + j * 1) : (Character)0; } public int CharactersTypeLength { get { int o = __p.__offset(8); return o != 0 ? __p.__vector_len(o) : 0; } } #if ENABLE_SPAN_T