Open
Description
Description
When defining a struct
that implements IXmlSerializable
AND has a parameterless constructor defined; an InvalidProgramException will occur when using XmlSerializer.Deserialize(object)
.
Reproduction Steps
Example test cases: https://github.com/Dash/XmlSerializableStructTest/blob/master/Program.cs
When using a struct like this in a class and then deserializing it with XmlSerializer:
public struct SerializableInitStruct : IXmlSerializable
{
public SerializableInitStruct()
{
this.Data = "Nope";
}
public string Data { get; set; }
public XmlSchema? GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
this.Data = reader.GetAttribute("Data");
}
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("Data", this.Data);
}
}
Expected behavior
The Xml content to deserialise correctly into a POCO class, without exception, in the same way as would happen if there wasn't a parameterless constructor defined.
Actual behavior
"System.InvalidOperationException: There is an error in XML document (2, 2).
---> System.InvalidProgramException: Common Language Runtime detected an invalid program.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderSerializableInitWrapper.Read2_SerializableInitWrapper(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderSerializableInitWrapper.Read3_SerializableInitWrapper()
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)\r\n --- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
at XmlSerializableStructTest.Program.FromXmlStream[T](Stream strea
m) in ..."
Regression?
No response
Known Workarounds
No known workaround as of yet.
Configuration
- .NET 8
- Windows 11 Enterprise
- x64
Other information
If run against .NET 4.8 using the Compilers NuGet package to get newer C# versions, this error is raised at the same time:
Managed Debugging Assistant 'FatalExecutionEngineError'
Message=Managed Debugging Assistant 'FatalExecutionEngineError' : 'The runtime has encountered a fatal error. The address of the error was at 0x729e0019, on thread 0x2904. 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.'