point-platform / dasher

Fast, lightweight, cross platform serialisation tool
Apache License 2.0
7 stars 6 forks source link

Consistent story around immutability/mutability #11

Open andysturrock opened 8 years ago

andysturrock commented 8 years ago

The following code throws a System.Security.VerificationException : Operation could destabilize the runtime.:

public class WithList
{
    public WithList(List<int> list)
    {
        ListOfInt = list;
    }
    public List<int> ListOfInt { get; }
}

void BlowUp()
{
    List<int> list = new List<int>();
    var msg = new WithList(list);
    new Serialiser<WithList>().Serialise(msg);
}

I'll take a look but you might get there quicker :-)

drewnoakes commented 8 years ago

List should not be supported as it's mutable. Use IReadOnlyList instead. However there is a bug here: it should throw on creating the serialiser.

andysturrock commented 8 years ago

I just forked the repo to look at this. It is now more graceful as it throws System.Exception : Unable to serialise type System.Collections.Generic.List1[System.Int32]`. Your NuGet release is behind the latest code in GibHub I guess.

I agree with the point about mutability but that's not checked everywhere. Eg this still works:

        public sealed class UserScore
        {
            public UserScore(string name, int score)
            {
                Name = name;
                Score = score;
            }

            public string Name { get; set; }
            public int Score { get; set; }
        }

So it's a bit inconsistent isn't it?

drewnoakes commented 8 years ago

Yes that is inconsistent. Will have a think. I like immutability and it's important to make it possible, but maybe Dasher shouldn't force it on users. Will have a think.

Just pushed 0.3.0 to NuGet.