mbraceproject / FsPickler

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

FsPickler cannot serialize C# auto properties from base class #8

Closed vasily-kirichenko closed 10 years ago

vasily-kirichenko commented 10 years ago

First, try to serialize-deserialize F# types:

[<AbstractClass>]
type My() = 
    member val BoolProp = false with get, set

type My1() = 
    inherit My()
    member val Str = "" with get, set

let pickler = FsPickler()
let ms = new MemoryStream()
pickler.Serialize (ms, My1(BoolProp = true, Str = "str") :> My)
ms.Position <- 0L
let my: My = pickler.Deserialize ms

result:

val it : My = FSI_0030+My1 {BoolProp = true; Str = "str";}

which is OK.

Now serialize an object from a C# assembly:

namespace ClassLibrary1
{
    [Serializable]
    public abstract class Class1
    {
        public bool BoolProp { get; set; }
    }

    [Serializable]
    public class Class2 : Class1
    {
        public string StrProp { get; set; }
    }
}
let pickler = FsPickler()
let ms = new MemoryStream()
pickler.Serialize (ms, ClassLibrary1.Class2 (BoolProp = true, StrProp = "str") :> ClassLibrary1.Class1)
ms.Position <- 0L
let my: ClassLibrary1.Class1 = pickler.Deserialize ms

result:

val it : ClassLibrary1.Class1 = ClassLibrary1.Class2 {BoolProp = false; StrProp = "str";}

The BoolProp contains false however true is expected.

eiriktsarpalis commented 10 years ago

Interesting.

I tracked down the issue to the fact that reflection will not identify the inherited FieldInfo in C# code. Not sure why this does not happen in F#.

eiriktsarpalis commented 10 years ago

Fixed in aaddc46e037c9fd9666311b29f2e04fafb2b1921. Expect an update in nuget soon.

vasily-kirichenko commented 10 years ago

Thanks, everything is OK now.