Skip to content

Handling new keyword in v3 source generator #1995

Description

@pkindruk

This is not something I need for production code, I was experimenting with different options that came into my mind.

Simple test case for new keyword is not working with source generators. I tried both 3.0.134-beta and develop (d349b4135) and will refer to it as v3.

Code

public static class MessagePackTest2
{
    public static void Run()
    {
        var obj1 = new B { Prop = "qwe" };
        (obj1 as A).Prop = "qweqwe";
        var data = MessagePackSerializer.Serialize(obj1);
        var obj2 = MessagePackSerializer.Deserialize<B>(data);

        Console.WriteLine(MessagePackSerializer.ConvertToJson(data));
    }

    [MessagePackObject]
    public class A
    {
        [Key(0)]
        //[Key("A0")]
        public string Prop { get; set; }
    }

    [MessagePackObject]
    public class B : A
    {
        [Key(1)]
        //[Key("B1")]
        public new string Prop { get; set; }
    }
}

Output

// IntKey, 2.5.172
["qweqwe","qwe"]

// StringKey, 2.5.172
{"A0":"qweqwe","B1":"qwe"}

// IntKey, v3
["qwe","qwe"]

// StringKey, v3
// Compilation error in generated formatter
// B1
private static global::System.ReadOnlySpan<byte> GetSpan_Prop() => new byte[1 + 2] { 162, 66, 49 };
// A0
private static global::System.ReadOnlySpan<byte> GetSpan_Prop() => new byte[1 + 2] { 162, 65, 48 };

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions