Cysharp / MemoryPack

Zero encoding extreme performance binary serializer for C# and Unity.
MIT License
3.29k stars 193 forks source link

Unable to compile when [MemoryPackable] class has multiple constructors #80

Closed littlehoneybunnytuttifruttipumpkinpie closed 1 year ago

littlehoneybunnytuttifruttipumpkinpie commented 1 year ago

I'm testing MemoryPack (latest .NET 7) with a class that has multiple constructors, and I marked one of the constructors as '[MemoryPackConstructor]'. However, the project doesn't compile and shows an error during compilation: "Error CS0234 The type or namespace name 'Formatters' does not exist in the namespace 'Tests.MemoryPack' (are you missing an assembly reference?)"

And it points to line 44 in generated code: image

Here is my full test code:

using MemoryPack;

namespace Tests.MemoryPack
{
    [MemoryPackable]
    public partial class TestObject
    {
        public string testString { get; set; } = "";

        [MemoryPackConstructor]
        public TestObject() { }

        public TestObject(string ignore) { }
    }

    class MemPackTest
    {
        public static void Main()
        {
            TestObject test_object = new();
            test_object.testString = "ABC";
            Console.WriteLine("Initial value before serializing: " + test_object.testString);

            byte[] serialized_object = MemoryPackSerializer.Serialize(test_object);
            TestObject? deserialized_object = MemoryPackSerializer.Deserialize<TestObject>(serialized_object);

            if (deserialized_object != null)
            {
                Console.WriteLine("Final value after deserializing: " + deserialized_object.testString);
            }

            Console.WriteLine("Test completed. Press any key to exit");
            Console.ReadKey();
        }

    }
}
littlehoneybunnytuttifruttipumpkinpie commented 1 year ago

Oops, this was a bug in my code, due to naming the Namespace "Tests.MemoryPack". Closing.