I found an odd case in the codebase at my work after upgrading to 5.9.0.1 today. I can't say for certain if this was present in previous versions of NCrunch, as it has been a while since I was able to run NCrunch with our solution.
What I do know is that this test passes in the VS test runner, but not in NCrunch:
Quote:[StructLayout(LayoutKind.Explicit)]
private struct BytesToUints
{
[FieldOffset(0)] public byte[] Bytes;
[FieldOffset(0)] public uint[] Uints;
}
[Fact]
public void InvalidCastException_Repro()
{
byte[] bytes = new byte[] { 1, 1, 0, 0 };
uint[] uints = new BytesToUints { Bytes = bytes }.Uints;
uint result = uints[0];
Assert.Equal(257u, result);
}
This is an unconventional way of achieving the desired reinterpretation in C#, I imagine it was written long ago by someone more familiar with C++. I'm not entirely sure it's fully reliable, given the nature of managed arrays, but it does seem to work in practice.
That being said, I wonder why this fails in NCrunch specifically. The exception is the following:
Quote:System.InvalidCastException: Unable to cast object of type 'System.Byte[]' to type 'System.UInt32[]'.
Any ideas?
Edit: It may be based on this blog post, referenced from the wiki for MurmurHash:
http://landman-code.blog...h-and-murmurhash2.html.
As mentioned there, this is a bit of a hack, but "works". As long as you don't rely on Uints.Length (or perhaps other properties of either array), because it will actually equal the length of the byte array. I wonder what NCrunch is doing that causes a cast to be attempted of the uint[] to the "actual" type of byte[]. I thought it might be RDI related, but disabling that does not remove the exception.