jamescourtney / FlatSharp

Fast, idiomatic C# implementation of Flatbuffers
Apache License 2.0
511 stars 51 forks source link

Support for GetMaxSize without an instance #147

Closed NVentimiglia closed 3 years ago

NVentimiglia commented 3 years ago

This seems unnecessary to require an instance of a type to determine the max size of a object.

FlatBufferSerializer.Default.GetMaxSize(MyInstance);

would be better to just pass in a type.

FlatBufferSerializer.Default.GetMaxSize();

jamescourtney commented 3 years ago

You don't know the max size until you look at the instance. Strings and Vectors can be arbitrary in length, so I don't see how this would work.

NVentimiglia commented 3 years ago

What about a generic deserialization method ? Pass in a Type and buffer as arguments, returns an instance.

jamescourtney commented 3 years ago

Did you mean non-generic? Most of the default functionality is exposed via generics with FlatBufferSerializer. If you want non-generic, the following methods can help you:

ISerializer FlatBufferSerializer.Instance.Compile(object item) ISerializer FlatBufferSerializer.Instance.Compile(Type itemType)

The results of compile are cached, so repeated calls for the same type return the same instance. Of course, the ISerializer you get back is valid only for that type of type of object.

jamescourtney commented 3 years ago

@NVentimiglia , let me know if you have more questions.