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

Ouverte
#2,187 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
45/100
Type d'issue
Bug
Clarté
Plutôt claire
Activité
À l'abandon
Stack technique
csharp
Domaine
backend

Piste de recherche

Commencez par la résolution des chaînes de UnionAttribute et comparez le comportement de Type.GetType("SecondLibrary.SubClass1") depuis MessagePack.Annotations avec celui du projet consommateur. Reproduisez la configuration de CoreLibrary et SecondLibrary présentée dans l’issue, puis vérifiez que la sérialisation polymorphe résout les sous-classes externes sans TypeLoadException.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.

Langage dominant
C#
Étoiles
6.8k
Forks
778
Merge moyen
22 h 8 min
PR mergées (30 j)
2

Préparer son environnement

Ouvrir dans Codespaces

Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de MessagePack-CSharp/MessagePack-CSharp

Toutes les issues de MessagePack-CSharp/MessagePack-CSharp

Issues similaires

Plus d'issues C#

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.