Unity3D UWP app returns null on typeof() call.

I have an Unity 3D 2018.2 build which I am using to build an UWP game. I have included two assemblies (DLLs) built on .NET standard 2.0 written in C#. The first assembly instantiates a DataContractJsonSerializer object to deserialize JSON messages received from a server into a C# class defined in the second assembly. To the DataContractJsonSerializer() constructor I pass in a typeof(myclass). I run this code in the editor and it works fine, but when I build the UWP app and run that, in my logs I get an exception thrown:

System.NullReferenceException: Object reference not set to an instance of an object.
at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.TryWritePrimitive (System.Type type, System.Func1[TResult] value, System.Reflection.MemberInfo memberInfo, System.Nullable1[T] arrayItemIndex, System.Xml.XmlDictionaryString name, System.Int32 nameIndex) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.WriteMembers (System.Runtime.Serialization.ClassDataContract classContract, System.Runtime.Serialization.ExtensionDataObject extensionData, System.Runtime.Serialization.ClassDataContract derivedMostClassContract) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.WriteClass (System.Runtime.Serialization.ClassDataContract classContract) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.WriteToJson (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.Runtime.Serialization.ClassDataContract dataContract, System.Xml.XmlDictionaryString memberNames) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.JsonFormatWriterGenerator+CriticalHelper+<>c__DisplayClass0_0.b__0 (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.Runtime.Serialization.ClassDataContract dataContract, System.Xml.XmlDictionaryString memberNames) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.JsonFormatClassWriterDelegate.Invoke (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.Runtime.Serialization.ClassDataContract dataContract, System.Xml.XmlDictionaryString memberNames) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.JsonClassDataContract.WriteJsonValueCore (System.Runtime.Serialization.XmlWriterDelegator jsonWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.JsonDataContract.WriteJsonValue (System.Runtime.Serialization.XmlWriterDelegator jsonWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.DataContractJsonSerializer.WriteJsonValue (System.Runtime.Serialization.Json.JsonDataContract contract, System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson.WriteDataContractValue (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Boolean isDeclaredType, System.Boolean writeXsiType, System.Int32 declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.InternalSerialize (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Boolean isDeclaredType, System.Boolean writeXsiType, System.Int32 declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Boolean isDeclaredType, System.Boolean writeXsiType, System.Int32 declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in <00000000000000000000000000000000>:0
at System.Reflection.MonoMethod.InternalInvoke (System.Object obj, System.Object parameters, System.Exception& exc) [0x00000] in <00000000000000000000000000000000>:0
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object parameters, System.Globalization.CultureInfo culture) [0x00000] in <00000000000000000000000000000000>:0
at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object parameters) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.InternalSerialize (System.Reflection.MethodInfo methodInfo, System.Func`1[TResult] memberValue, System.Type memberType, System.Boolean writeXsiType) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.WriteValue (System.Type memberType, System.Object memberValue) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.WriteMembers (System.Runtime.Serialization.ClassDataContract classContract, System.Runtime.Serialization.ExtensionDataObject extensionData, System.Runtime.Serialization.ClassDataContract derivedMostClassContract) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.WriteClass (System.Runtime.Serialization.ClassDataContract classContract) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.WriteToJson (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.Runtime.Serialization.ClassDataContract dataContract, System.Xml.XmlDictionaryString memberNames) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.JsonFormatWriterGenerator+CriticalHelper+<>c__DisplayClass0_0.b__0 (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.Runtime.Serialization.ClassDataContract dataContract, System.Xml.XmlDictionaryString memberNames) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.JsonFormatClassWriterDelegate.Invoke (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.Runtime.Serialization.ClassDataContract dataContract, System.Xml.XmlDictionaryString memberNames) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.JsonClassDataContract.WriteJsonValueCore (System.Runtime.Serialization.XmlWriterDelegator jsonWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.JsonDataContract.WriteJsonValue (System.Runtime.Serialization.XmlWriterDelegator jsonWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.DataContractJsonSerializer.WriteJsonValue (System.Runtime.Serialization.Json.JsonDataContract contract, System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson.WriteDataContractValue (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Boolean verifyKnownType, System.RuntimeTypeHandle declaredTypeHandle, System.Type declaredType) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson.SerializeWithXsiTypeAtTopLevel (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.RuntimeTypeHandle originalDeclaredTypeHandle, System.Type graphType) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalWriteObjectContent (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalWriteObject (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.XmlObjectSerializer.InternalWriteObject (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.DataContractJsonSerializer.WriteObject (System.Xml.XmlDictionaryWriter writer, System.Object graph) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.DataContractJsonSerializer.WriteObject (System.IO.Stream stream, System.Object graph) [0x00000] in <00000000000000000000000000000000>:0

In Unity3D editor both assemblies are set to run on IL2CPP. I set the API compatibility level to .NET standard 2.0.

I tried copying the type into the same assembly as the call to instantiate the DataContractJsonSerializer but still get the same error.

Why is it that when I run it from the editor it works, but the UWP app fails?

Found the answer from a post a few months earlier: