Re: Generics Questions

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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
.



Relevant Pages

  • Re: InputStream
    ... Especially if reading from a network connection or ... > The displayed message always says that the read amount is lower than the ... read(bytebuffer, int offset, int maxWanted) is for doing this. ...
    (comp.lang.java.programmer)
  • Can NetworkStream.Write() block?
    ... myNetworkStream.Write(byte, int offset, int size); ... From the MSDN documentation: ... If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code, and refer to the Windows Sockets version 2 API error code documentation in MSDN for a detailed description of the error. ... It says the call blocks until the bytes are sent but aren't the bytes just queued to the write buffer until the OS feels like sending them over the wire? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Can NetworkStream.Write() block?
    ... myNetworkStream.BeginWrite(bytebuffer, int offset, int size) since all that does is create a backlog of BeginWrite threads all waiting to complete. ... In general, you can assume asynchronous calls are *more* efficient than their synchronous companions, since those *always* take up a thread (namely the thread waiting for completion) while the asynchronous ones generally only take up a thread when executing the completion callback. ... The overlapped requests are simply queued until there's an opportunity to process them. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem with VS and memcpy...
    ... directly in buffer? ... memcpy(buf, temp, sizeof temp); ... It looks to me like temp has a terminating zero ...
    (microsoft.public.vc.language)