Hi all, I have a lot of long boring work ahead of me, unless this long shot comes up roses.
I have a lot of classes I need to add a ToByteArray() method to, and I'm wondering if Visual studio 2015 has
any tricks up its sleeve where I can create some macro or template to generate the methods, instead of manually writing them for many scores of classes.
Here is an example of the method based on the properties of the class.
class Class
{
public int var1 { get; set; }
public uint var2 { get; set; }
public byte var3 { get; set; }
public byte[] ToByteArray()
{
byte[][] baArray = {
BitConverter.GetBytes(var1),
BitConverter.GetBytes(var2),
new byte[1] { var3 }
};
int isize = 0;
foreach (byte[] bdata in baArray)
{
isize += bdata.Length;
}
byte[] bRtn = new byte[isize];
int ioffset = 0;
foreach (byte[] bdata in baArray)
{
Buffer.BlockCopy(bdata, 0, bRtn, ioffset, bdata.Length);
ioffset += bdata.Length;
}
return bRtn;
}
}
Of course each class will have a varying number of properties with different names.
I really do know I'm clutching at straws, but I've been surprised before, and my search skills and unwordyness
let me down a lot in my reasearch.
As always, even just the correct things I ought to be searching for will be a great help.
Thanks for reading.