mbraceproject / FsPickler

A fast multi-format message serializer for .NET
http://mbraceproject.github.io/FsPickler/
MIT License
323 stars 54 forks source link

Support for pickling code from Reflection.Emit? #123

Open hsenag opened 3 years ago

hsenag commented 3 years ago

Would it be plausible to add support to pickle things produced by System.Reflection.Emit, or is that inherently difficult?

My use case is that for slightly complicated reasons I want to send some code from machine A to B via a quotation, execute part of that code on B and then send a continuation to machine C via FSPickler.

A very simplified example would be

let v = <@ fun x -> x+1 @>

let w : int -> int = FSharp.Quotations.Evaluator.QuotationEvaluator.Evaluate v

let fspickler = MBrace.FsPickler.FsPickler.CreateBinarySerializer()
fspickler.Pickle(w)

which currently fails as expected because System.Reflection.Emit.DynamicMethod+RTDynamicMethod is not serializable by FsPickler:

 Unhandled exception. MBrace.FsPickler.FsPicklerException: Error serializing object of type 'Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,System.Int32]'.
---> MBrace.FsPickler.NonSerializableTypeException: Type 'System.Reflection.Emit.DynamicMethod+RTDynamicMethod' is not serializable: global method 'Int32 lambda_method(System.Runtime.CompilerServices.Closure, Microsoft.FSharp.Core.Unit, Int32)' in assembly 'Anonymously Hosted DynamicMethods Assembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'

I'm aware of Vagabond but currently it only advertises support for .NET Core 3.1 and I also need to support .NET Framework 4. Would I be better off trying to get Vagabond to support .NET Standard?

0x53A commented 2 years ago

I'm aware of Vagabond but currently it only advertises support for .NET Core 3.1 and I also need to support .NET Framework 4.

I randomly saw this, and just wanted to mention that this commit removed .NET FX support from Vagabond.

You might just be able to use an older version from NuGet.