2022-08-22

DataContractSerializer fails for List

I have changed my serialization to DataContracts but now I am having problem with a specific class. It works fine on my Mac, but not on my android devices when built using IL2CPP. The thread stops at the writeObject function. My three classes related to the error:

[DataContract]
[KnownType(typeof(TaskIdentifier))]
[KnownType(typeof(TraceableTaskItem))]
[KnownType(typeof(List<TraceableTaskItem>))]
public class TraceableTaskContainer
{
    [DataMember]
    protected TaskIdentifier _taskIdent;

    [DataMember]
    protected List<TraceableTaskItem> _lNotAccomplishedTaskItems = new List<TraceableTaskItem>();

//.....
}
[DataContract]
[KnownType(typeof(DateTime))]
[KnownType(typeof(ItemReviewStage))]
public class TraceableTaskItem : GenericTaskItem, IEquatable<TraceableTaskItem>, IComparable<TraceableTaskItem>
{
    [DataMember]
    public string sDisplayTextInTraceableTaskReport;

    [DataMember]
    protected DateTime NextReviewDate;

    [DataMember] //ItemReviewStage is a enum
    protected ItemReviewStage reviewStage = ItemReviewStage.NewTask;

   
    public TraceableTaskItem() //important to deserialize old classes, do not remove it
    {

    }
//....
}
[DataContract]
//[KnownType(typeof(List<bool>))]
abstract public class GenericTaskItem
{
    [DataMember]
    public string sItemID = "";

    //[DataMember]
    protected List<bool> lTimesAnsweredCorrectly = new List<bool>();

    protected List<List<string>> llWrongAnswers = new List<List<string>>();

//...
}

The code works with the commented lines above. But as soon as I uncomment DataMember on the lTimesAnsweredCorrely and with or without uncommenting the equivalent KnownType line (I have tested both), the code stops working on my mobile devices. Any idea how can I fix this?

Exception:

"System.Reflection.TargetInvocationException: 
Exception has been thrown by the target of an invocation. 
---> System.ExecutionEngineException: Attempting to call method \'System.Runtime.Serialization.XmlObjectSerializerWriteContext::
IncrementCollectionCountGeneric<System.Boolean>\' 
for which no ahead of time (AOT) code was generated.\n  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 \n  at 
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] 
in <00000000000000000000000000000000>:0 \n  at System.Runtime.Serialization.XmlFormatWriterInterpreter.WriteCollection (System.Runtime.Serialization.CollectionDataContract collectionContract) [0x00000] 
in <00000000000000000000000000000000>:0 \n  at 
System.Runtime.Serialization.XmlFormatWriterInt… string



 StackTrace: "  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 \n  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <00000000000000000000000000000000>:0 \n  at System.Runtime.Serialization.XmlFormatWriterInterpreter.WriteCollection (System.Runtime.Serialization.CollectionDataContract collectionContract) [0x00000] in <00000000000000000000000000000000>:0 \n  at System.Runtime.Serialization.XmlFormatWriterInterpreter.WriteCollectionToXml (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.XmlObjectSerializerWriteContext context, System.Runtime.Serialization.CollectionDataContract collectionContract) [0x00000] in <00000000000000000000000000000000>:0 \n  at System.Runtime.Serialization.XmlForma… string

Source: "mscorlib" string

inner exception: 
 InnerException "System.ExecutionEngineException: Attempting to call method \'System.Runtime.Serialization.XmlObjectSerializerWriteContext::
IncrementCollectionCountGeneric<System.Boolean>\' for which no ahead of time (AOT) code was generated.\n  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 \n  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <00000000000000000000000000000000>:0 \n  at System.Runtime.Serialization.XmlFormatWriterInterpreter.WriteCollection (System.Runtime.Serialization.CollectionDataContract collectionContract) [0x00000] in <00000000000000000000000000000000>:0 \n  at System.Runtime.Serialization.XmlFormatWriterInterpreter.WriteCollectionToXml (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Ru… System.Exception

Update

The problem seems to be with bool and int only, a List of string works just as expected.



No comments:

Post a Comment