Re: Generics Questions
- From: Marc Gravell <marc.gravell@xxxxxxxxx>
- Date: Sun, 27 Jul 2008 12:25:36 -0700 (PDT)
In your example however, don't I end up with the same problem inside
ReadFromBuffer()?
I won't be able to cast back to my there as well will I?
What am I missing?
Each implementation is specific to the type it is designed for, so
there /is/ no cast, or if there is it is a well-defined one; for
example:
public class SingleByteConverter : IByteConverter<float> {
public float ReadFromBuffer(byte[] buffer, int offset) {
return BitConverter.ToSingle(buffer, offset);
}
public int WriteToBuffer(float value, byte[] buffer, int offset) {
byte[] tmp = BitConverter.GetBytes(value);
buffer[offset++] = tmp[0];
buffer[offset++] = tmp[1];
buffer[offset++] = tmp[2];
buffer[offset] = tmp[3];
return 4; // how many bytes we wrote
}
}
So there might be 4 different classes, each just implementing
IByteConverter< something > (for a different something).
Note that it is possible for a single class to support multiple
primates using explicit interface implementation; that is what the
various Foo_Bar files are (in the pervious link).
Of course, if you simply want to provide a binary serialization for
your classes, you could consider protobuf-net ;-p
Marc
.
- References:
- Generics Questions
- From: Udi
- Re: Generics Questions
- From: Marc Gravell
- Re: Generics Questions
- From: Marc Gravell
- Re: Generics Questions
- From: Marc Gravell
- Re: Generics Questions
- From: Udi
- Generics Questions
- Prev by Date: Re: FTP related classes
- Next by Date: Re: FTP related classes
- Previous by thread: Re: Generics Questions
- Next by thread: Re: Generics Questions
- Index(es):
Relevant Pages
|