Polymorphism issue - TypeLoadException when using string class names in UnionAttribute across projects

Offen
#2,187 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
45/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
csharp
Bereich
backend

Rechercherichtung

Beginnen Sie mit der Auflösung von Zeichenfolgen durch UnionAttribute und vergleichen Sie, wie sich Type.GetType("SecondLibrary.SubClass1") aus MessagePack.Annotations gegenüber dem konsumierenden Projekt verhält. Reproduzieren Sie das in der Issue gezeigte Setup für CoreLibrary und SecondLibrary und überprüfen Sie anschließend, dass die polymorphe Serialisierung die externen Unterklassen ohne TypeLoadException auflöst.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

I'm sure this must have been dealt with before, but I can't find any existing issues on it. Apologies if it's a duplicate.

I have a classic case where I have a project that acts as a core library. This library defines some base classes. I have another project which has a dependency to the core library and has sub classes of the base classes in the core library. I want to be able to serialize objects which use polymorphism and have MessagePack be aware of the sub types during serialization. This in theory should work via setting the Union attribute (public UnionAttribute(int key, string subType)) on the base class with the name of the sub class. However, when I do this, I get an exception: TypeLoadException: Could not load type 'SecondLibrary.SubClass1' from assembly 'MessagePack.Annotations, Version=3.1.3.0, Culture=neutral, PublicKeyToken=b4a0369545f0a1be'.

I will try to provide minimal code for my situation:

CoreLibrary project:

namespace CoreLibrary
{
    [MessagePackObject]
    [Union(0, "SecondLibrary.SubClass1")]
    [Union(1, "SecondLibrary.SubClass2")]
    public abstract class BaseClass
    {
        [Key(0)]
        public int Prop1 { get; set; }
    }
}

SecondLibrary project (has dependency to CoreLibrary project)

namespace SecondLibrary
{
    [MessagePackObject]
    public class SubClass1 : CoreLibrary.BaseClass
    {
        [Key(1)]
        public int Prop2 { get; set; }
    }

    [MessagePackObject]
    public class SubClass2 : CoreLibrary.BaseClass
    {
        [Key(1)]
        public int Prop2 { get; set; }
    }

    // I have a class that I want to serialize that uses CoreLibrary.BaseClass
    [MessagePackObject]
    public class AnotherClass
    {
        [Key(0)]
        public CoreLibrary.BaseClass PolyProperty { get; set; } // This can be SubClass1 or SubClass2
    }
}

SecondLibrary project has a direct dependency on CoreLibrary project. CoreLibrary project is not aware of SecondLibrary project.

Obviously I cannot use [Union(0, typeof(SecondLibrary.SubClass1))] because I cannot have a circular dependency between CoreLibrary and SecondLibrary, so I have to use the string class name [Union(0, "SecondLibrary.SubClass1")]. Why would this not work? Both classes are loaded at runtime, but for some reason it's looking to resolve the dependency of "SecondLibrary.SubClass1" from the context of "MessagePack.Annotations". If I was to do Type.GetType("SecondLibrary.SubClass1", throwOnError: true) from anywhere else in my code, it would work...

If I'm doing something wrong, please let me know.

Vorherrschende Sprache
C#
Sterne
6.8k
Forks
778
Ø Merge
22 Std. 8 Min.
Gemergte PRs (30 T.)
2

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus MessagePack-CSharp/MessagePack-CSharp

Alle Issues in MessagePack-CSharp/MessagePack-CSharp

Ähnliche Issues

Weitere Issues zu C#

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.