- Home /
DataContract serialization with IL2CPP
I've been using DataContract serialization (using the System.Runtime.Serialization.DataContractSerializer) successfully for a while now to transfer data over a network in a UWP app (HoloLens), however when trying to port the project to IL2CPP I haven't been able to use this serializer anymore.
The exception it is throwing is:
System.NotSupportedException: IL2CPP does not support marshaling delegates that point to instance methods to native code. The method we're attempting to marshal is: System.Runtime.Diagnostics.DiagnosticsEventProvider::EtwEnableCallBack".
The mentioned EtwEnableCallback isn't something that is in any of my classes, so I'm guessing it is ingested by some of the Serialization attributes? Is there a way around this error?
Example code - Simple example class to be serialized:
[Serializable]
public class DataContractSerializationTest
{
[DataMember]
public string TestValue = "test";
}
Example code - Serialization:
DataContractSerializer serializerTest = new DataContractSerializer(typeof(DataContractSerializationTest));
MemoryStream streamTest = new MemoryStream();
using (var writerTest = XmlDictionaryWriter.CreateBinaryWriter(streamTest))
{
serializerTest.WriteObject(writerTest, new DataContractSerializationTest()); // It throws the exception here
}
Hi. I have exactly the same problem when using DataContractJsonSerializer:
T response;
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof (T));
using ($$anonymous$$emoryStream stream = new $$anonymous$$emoryStream(jsonByteArray))
{
response = (T) serializer.ReadObject(stream);
}
I'm getting this exception when calling "ReadObject" method: System.NotSupportedException: IL2CPP does not support marshaling delegates that point to instance methods to native code. The method we're attempting to marshal is: System.Runtime.Diagnostics.DiagnosticsEventProvider::EtwEnableCallBack occurred
I'm trying to use $$anonymous$$icrosoft Authentication Library ($$anonymous$$SAL) for .NET in Unity (2019.2) project, build using IL2CPP scripting backend and running on UWP (AR$$anonymous$$ platform).
Answer by DTDTDTDT · Dec 19, 2019 at 04:13 PM
Hi,
I finally managed to resolve this issue by adding a link.xml in my assets folder with the following information:
<linker>
<assembly fullname="System.Runtime.Serialization" preserve="all"/>
</linker>
This seems to prevent the Unity linker to trim some information that is needed by the .net reflection which is used by the DataContractSerializer.
Source: Forum post