I spent hours trying to resolve compilation issues related to f__AnonymousType
. Seems to gets a lot of errors regarding expressions needing directives but not sure exactly what to do.
public static void ChangeSerialNumber(char volume, uint newSerial)
{
var source = new <>f__AnonymousType0<string, int, int>[]
{
new
{
Name = "FAT32",
NameOffs = 82,
SerialOffs = 67
},
new
{
Name = "FAT",
NameOffs = 54,
SerialOffs = 39
},
new
{
Name = "NTFS",
NameOffs = 3,
SerialOffs = 72
}
};
using (Helpers.Disk disk = new Helpers.Disk(volume))
{
byte[] sector = new byte[512];
disk.ReadSector(0U, sector);
var <>f__AnonymousType = source.FirstOrDefault(f => Helpers.Strncmp(f.Name, sector, f.NameOffs));
if (<>f__AnonymousType == null)
{
throw new NotSupportedException("This file system is not supported");
}
uint num = newSerial;
int i = 0;
while (i < 4)
{
sector[<>f__AnonymousType.SerialOffs + i] = (byte)(num & 255U);
i++;
num >>= 8;
}
disk.WriteSector(0U, sector);
}
}
This is used for USB Stick Refurbishments in order as part of the software to secure wipe, we would like to change the serial numbers of the drive (in effect spoof them) in case of a chargeback we can match the drive they return to make sure its the one we sent out.
The point about anonymous type, is that you don't have to give them a name, the compiler will do it for you.
<>f__AnonymousType0
is not a valid name in user code, but looks like the name generated by the compiler. You can't use it.
Just use anonymous syntax :
public static void ChangeSerialNumber(char volume, uint newSerial)
{
var sources = new[]
{
new
{
Name = "FAT32",
NameOffs = 82,
SerialOffs = 67
},
new
{
Name = "FAT",
NameOffs = 54,
SerialOffs = 39
},
new
{
Name = "NTFS",
NameOffs = 3,
SerialOffs = 72
}
};
using (Helpers.Disk disk = new Helpers.Disk(volume))
{
byte[] sector = new byte[512];
disk.ReadSector(0U, sector);
var source = sources.FirstOrDefault(f => Helpers.Strncmp(f.Name, sector, f.NameOffs));
if (source == null)
{
throw new NotSupportedException("This file system is not supported");
}
var num = newSerial;
var i = 0;
while (i < 4)
{
sector[source.SerialOffs + i] = (byte) (num & 255U);
i++;
num >>= 8;
}
disk.WriteSector(0U, sector);
}
}