Polymorphism issue - TypeLoadException when using string class names in UnionAttribute across projects
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 45/100
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
Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.
- Aucun Dockerfile ni fichier Docker Compose
- Aucun modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de MessagePack-CSharp/MessagePack-CSharp
-
SerializableMemberTypeAnalyzer throws AD0001 when any referenced assembly has an assembly-level attribute with a single array argumentPeut-être pris Une pull request liée à cette issue est ouverte ou déjà fusionnée. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 76/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
MessagePack-CSharp/MessagePack-CSharp#2276 · 2 commentaires ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 66/100
MessagePack-CSharp/MessagePack-CSharp#2252 · 3 commentaires ·
-
Difficulté 4/5 3-5 jours Accessibilité débutants 52/100
Toutes les issues de MessagePack-CSharp/MessagePack-CSharp
Issues similaires
-
kind/bug
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
unoplatform/uno#26555 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
Listenarrs/Listenarr#1173 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 77/100
CactuseSecurity/firewall-orchestrator#5739 ·
Les mainteneurs répondent en général sous 1 jour
-
Add File sorts a picked file by VB6's extensions, not the project format's (#4243 follow-up)Ouvertechore project-system small
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
vikramlearning/blazorbootstrap#1388 ·
Les mainteneurs répondent en général sous 1 jour