Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MemoryPackSerializationException in complex polymorphism #377

Open
Darkar25 opened this issue Feb 15, 2025 · 0 comments
Open

MemoryPackSerializationException in complex polymorphism #377

Darkar25 opened this issue Feb 15, 2025 · 0 comments

Comments

@Darkar25
Copy link

Looks like MemoryPack does not support chained polymorhism.
This example would produce MemoryPackSerializationException upon calling the Serialize method.

internal class Program
{
    static void Main(string[] args)
    {
        _ = MemoryPackSerializer.Serialize<IA>(new B());
    }
}
[MemoryPackable]
[MemoryPackUnion(0, typeof(A))]
public partial interface IA { }
[MemoryPackable]
[MemoryPackUnion(0, typeof(B))]
public abstract partial class A : IA { }
[MemoryPackable]
public partial class B : A { }

As if the unions weren't confusing enough already, they require the concrete(non-abstract) implementations rather than just the classes that inherit from the specified interface/abstract class.
However, this code would work:

[MemoryPackable]
[MemoryPackUnion(0, typeof(B))]
public partial interface IA { }
[MemoryPackable]
[MemoryPackUnion(0, typeof(B))]
public abstract partial class A : IA { }
[MemoryPackable]
public partial class B : A { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant