C
C#2mo ago
anthony63

✅ Get bytes and append to List c#

public class ByteWriter {
public MemoryStream Stream = new MemoryStream();
public BinaryWriter Writer;

public ByteWriter() {
Writer = new BinaryWriter(Stream);
}

public void Write<T>(T value) {
Writer.Write(value);
}
}
public class ByteWriter {
public MemoryStream Stream = new MemoryStream();
public BinaryWriter Writer;

public ByteWriter() {
Writer = new BinaryWriter(Stream);
}

public void Write<T>(T value) {
Writer.Write(value);
}
}
im trying to write a method that will append the bytes for value to the stream Stream, T will be a variable amount of bytes so im taking it in as a generic type, but Write doesnt have a generic overload any ideas what i can do?
No description
20 Replies
Joreyk ( IXLLEGACYIXL )
you have 2 stages 1. convert to byte and write 2. map T to a writer that knows how to convert T to bytes eg something like this pseudo code https://gist.github.com/IXLLEGACYIXL/9f65b0ebc2ff38a22b0d779e0c92fd59
Gist
t.cs
GitHub Gist: instantly share code, notes, and snippets.
Joreyk ( IXLLEGACYIXL )
@mynarco
anthony63
anthony63OP2mo ago
got it, thank you, ill try to get this working what exactly is Converter?
Joreyk ( IXLLEGACYIXL )
that thing who knows the plan to convert T to bytes for example you have a Vector3 then you will need some sort of converter who knows how to convert Vector3 to bytes and read Vector3 from bytes
anthony63
anthony63OP2mo ago
like this?
class Converter {
public byte[] ConvertToBytes(int value) {

}

public byte[] ConvertToBytes(ushort value) {

}
}
class Converter {
public byte[] ConvertToBytes(int value) {

}

public byte[] ConvertToBytes(ushort value) {

}
}
wait no i still have no idea how it works i cant reallyt understand how the psuedocode works? Any way you can provide a working example? Its fine if it only converts 1 type just need an idea of how to add a type conversion
Joreyk ( IXLLEGACYIXL )
@mynarco ive updated the gist at the bottom is a sample or top whereever it is for you
anthony63
anthony63OP2mo ago
ok looking now
Joreyk ( IXLLEGACYIXL )
so you jump from converter to converter until you can convert it to bytes and your primtive converters are used for determing the endless chain forget about the generic context for now
anthony63
anthony63OP2mo ago
ok got that down, but how do i define the dictionary then? because Converter takes in a generic..
Joreyk ( IXLLEGACYIXL )
make foreach converter a non generic class which is capable of instantiating the converter from a Type parameter with activate instance so your helper is a factory of converters you can source gen that and your converters
Joreyk ( IXLLEGACYIXL )
this is how it could look, or atleast thats my source gened code that implements what i tell you
anthony63
anthony63OP2mo ago
got it, thanks
Joreyk ( IXLLEGACYIXL )
this is maybe more complex than you need it as i wanted ful generic compatibility if you want it fixed like system.text.json then you dont need to worry about all of that too much, generating the extension method would be fine allready depends what you want
anthony63
anthony63OP2mo ago
i got it thanks :)
ero
ero2mo ago
Does T need to be any type? Or are blittable types enough? The suggestions are (no offense) ridiculous either way If the types can be blittable, just include CommunityToolkit.HighPerformance and use its StreamExtensions You can just use Stream.Write<T> on the MemoryStream then, no need to implement anything yourself If T should be any type that you defined yourself, I recommend implementing an interface over creating converters:
interface ISerializable<TSelf>
where TSelf : ISerializable<TSelf>
{
static abstract void Serialize(Stream stream, TSelf value);
}

interface IDeserializable<TSelf>
where TSelf : IDeserializable<TSelf>
{
static abstract TSelf Deserialize(Stream stream);
}
interface ISerializable<TSelf>
where TSelf : ISerializable<TSelf>
{
static abstract void Serialize(Stream stream, TSelf value);
}

interface IDeserializable<TSelf>
where TSelf : IDeserializable<TSelf>
{
static abstract TSelf Deserialize(Stream stream);
}
anthony63
anthony63OP2mo ago
thank you that works well. i appreciate it :), T is just a number, like byte, ushort, uint, and ulong
ero
ero2mo ago
Yeah then don't overcomplicate it and just use what's already there As a side note, in your original code (ByteWriter), you should consider if those fields really need to be public. .NET recommends fields to always be private or protected, properties should be public $close
MODiX
MODiX2mo ago
If you have no further questions, please use /close to mark the forum thread as answered
anthony63
anthony63OP2mo ago
Of course. I really appreciate it
Want results from more Discord servers?
Add your server