I tried to experiment with the ability of .NET to support unions using the code below, but it causes a System.ExecutionengineException in .NET 2.0 and FatalExecutionEngineError in .NET 4.0 with the message:
The runtime has encountered a fatal error. The address of the error was at 0x738b3138, on thread 0x1080. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
I agree that this code should not work, but I didn't expect this kind of exception. Is this a .NET bug?
Class POLine
Public price As Decimal
Public VendorItem As String
End Class
Class SOLine
Public price As Decimal
Public Required As DateTime
End Class
<System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)> _
Structure LineRef
<System.Runtime.InteropServices.FieldOffset(0)> _
Public poLine As POLine
<System.Runtime.InteropServices.FieldOffset(0)> _
Public soLine As SOLine
End Structure
Sub Main()
Dim lr As New LineRef
lr.poLine = New POLine With {.price = 1.23D, .VendorItem = "X22"}
lr.soLine = New SOLine With {.price = 3.14D, .Required = DateTime.Now}
Console.WriteLine("{0} {1}", lr.soLine.price, lr.soLine.Required)
Console.WriteLine("{0} {1}", lr.poLine.price, lr.poLine.VendorItem)
End Sub