Union accessors in C# should use generic type for the table

When accessing a union field, we should return the same object type
as was given to the method, i.e. the parameter should have a generic
type for any Table-derived type. This way, we do not need to make
superfluous casts (which also reduce type safety) like

var myUnionType = (MyUnionType)buff.GetUnionField(new MyUnionType());

when we can do just
var myUnionType = buff.GetUnionField(new MyUnionType());

Change-Id: Idac1b638e46cc50b1f2dc19f10741481202b1515
This commit is contained in:
Mormegil
2015-04-15 17:51:20 +02:00
committed by Wouter van Oortmerssen
parent f7d24f60a2
commit 221193eaa2
3 changed files with 33 additions and 15 deletions

View File

@@ -66,7 +66,7 @@ namespace FlatBuffers
}
// Initialize any Table-derived type to point to the union at the given offset.
protected Table __union(Table t, int offset)
protected TTable __union<TTable>(TTable t, int offset) where TTable : Table
{
offset += bb_pos;
t.bb_pos = offset + bb.GetInt(offset);