Skip to content

NullReferenceException in System.Xml.Serialization.TypeScope.ShouldBeReplaced #38025

Closed

Description

Description

I don't know if this is the correct place to report this bug. I am converting a large solution from .net Framework to .net Core 3.1. I am not including code, save for one line, but I feel any NullReferenceExeption (without further details) is a bug in the code and should be addressed by, at a minimum, providing more details in the error message. This is especially true now that we have the #Nullable parameter available to us.

Configuration

.net Core 3.1, running on desktop Visual Studio debug x64,

Regression?

This same code works when compiled as .net Framework 4.8 .

Other information

The most important part of this is the stack trace:
System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=System.Private.Xml StackTrace: at System.Xml.Serialization.TypeScope.ShouldBeReplaced(MemberInfo memberInfoToBeReplaced, Type derivedType, MemberInfo& replacedInfo) at System.Xml.Serialization.TypeScope.PopulateMemberInfos(StructMapping structMapping, MemberMapping[] mappings, Dictionary``2 memberInfos) at System.Xml.Serialization.XmlSerializationWriterILGen.WriteStructMethod(StructMapping mapping) at System.Xml.Serialization.XmlSerializationWriterILGen.GenerateMethod(TypeMapping mapping) at System.Xml.Serialization.XmlSerializationILGen.GenerateReferencedMethods() at System.Xml.Serialization.XmlSerializationWriterILGen.GenerateEnd() at System.Xml.Serialization.TempAssembly.GenerateRefEmitAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace) at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location) at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping, Type type, String defaultNamespace, String location) at System.Xml.Serialization.XmlSerializer..ctor(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, String defaultNamespace, String location) at System.Xml.Serialization.XmlSerializer..ctor(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, String defaultNamespace) at gov.DoD.USXports.LPS.Entity.EntityBase.XmlSerializerCollection.<>c.<GetOrCreate>b__1_0(TypeAndNamespace k) in D:\Workspaces\SPAN_USXPORTS3\app_Core\IMPLEMENTATION\Application\gov.DoD.USXports.LPS.Entity\EntityBase.cs:line 212

The call that triggered this (I replaced some explicitly passed nulls and it still fails) is:
return new XmlSerializer(k.Type, xo, Array.Empty<Type>(), new XmlRootAttribute(), k.XmlNamespace);


I debugged my project turning off JustMyCode and turning on source code repository. The problem line is 1184:
if (!info.GetMethod.IsPublic && memberInfoToBeReplaced is PropertyInfo && ((PropertyInfo)memberInfoToBeReplaced).GetMethod.IsPublic )
In it, memberInfoToBeReplaced and GetMethod.IsPublic is not null. It will not tell me the value of info because it has been optimized, so info.GetMethod is probably the likeliest culprit.


The property that is triggering the NullReferenceException in the code above it looks like:
public override ChangedCode Changed { set => SetChanged(value); }
You will note that this is a override that only overrides the set and not the get.

The workaround to:
public override ChangedCode Changed { set => SetChanged(value); get => base.Changed; // This is needed for .net Core XmlSerializer because of a NullReferenceException in it. }
Seems to have solved my problem. But it did not solve the bug in System.Xml.Serialization.TypeScope.ShouldBeReplaced .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions