Im messing arround with structlayouts and I found some thing i find quite odd:
The following code is working as i thought it would:
using System;
using System.Runtime.InteropServices;
public class Program
{
[STAThread]
static void Main()
{
Magic m = new Magic
{
InstanceA = new ClassA(),
InstanceB = new ClassB {Value="47"}
};
Console.WriteLine(m.InstanceA.Value);
Console.ReadKey();
}
class ClassA
{
public dynamic Value;
}
class ClassB
{
public string Value; // Change to int and it will get messy
}
[StructLayout(LayoutKind.Explicit)]
struct Magic
{
[FieldOffset(0)]
public ClassA InstanceA;
[FieldOffset(0)]
public ClassB InstanceB;
}
}
However, if you change classB.Value to int, this code will throw the mentioned FatalExecutionEngineError.
Can anyone explain why or maybe how to workarround? I know this is probably way too complicated and im just messing arround here but someone might want some challenge.