Using Protobuf-Net In Xamarin.iOS without full AOT

2019-02-20 18:11发布

问题:

Is there any alternative to achieving serialising and deserialising of objects in Xamarin.iOS (Monotouch) using protobuf-net other than this method:

http://www.frictionpointstudios.com/blog/2011/3/31/using-protobuf-net-serialization-in-unity-iphone.html

Reading around some people claim they have managed it (without giving evidence), but my understanding is that [iOS JIT==NO] so does not quite make sense.

If the only possible solution is to fully AOT all relevant classes what might a suitable pre/post-build event command line be to perform this AOT for any relevant assemblies automatically?

回答1:

I've heard a good number of people have success via that route, but I too can't give documented evidence.

That method is a bit out of date - I've simplified a few steps; there is a standalone pre-compile tool that should work:

  • create a project/assembly for the DTOs that you want to serialize that references the appropriate version of protobuf-net; presumably CoreOnly/ios, ideally with that dll set to copy into the output directory (it just makes life easier)
  • run

    precompile "SomePath/YourDto.dll" -t:MySerializer -o:MySerializer.dll
    

    (maybe with a mono before that to get mono to host the exe)

    this should resolve the framework and compile a MySerializer.dll that you can reference, which involves zero JIT (MySerializer.dll will reference your dto dll and the version of protobuf-net that your dto dll referenced)

  • now reference your dto dll, the serializer dll, and the protobuf-net dll, and it should all work just by using new MySerializer().Serialize(...)
  • when you compile your solution, the projects should all AOT nicely

I'll be happy to offer guidance, but currently I am mac-less, so I can't check right now. If you get any problems let me know. If it can't resolve the framework, you can add -f:{path to the framework assemblies} to give it a clue.