I'm writing a system that has a set of protocol buffers (using protobuf-net), I want to define something like this in an abstract class they all inherit off:
public byte[] GetBytes()
however, the protocol buffer serealiser requires a type argument, is there some efficient way to get the type of the inheriting class?
Example:
public byte[] GetBytes()
{
using (MemoryStream stream = new MemoryStream())
{
Serializer.Serialize<T /* what goes here? */>(stream, this);
return stream.ToArray();
}
}
Just write "T" right?
and then in your class declaration:
public class M<T>
?
-- Edit
And then when you inherit it:
public class Foo : M<Apple>
You can do this via reflection, but protobuf-net did it for you.
Just change your call to:
Serializer.NonGeneric.Serialize(stream, this /* Takes an object here */);
This works by building the generic method at runtime via reflection. For details, check the code (second method here).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With