jeudi 4 mars 2021

Why is AssemblyQualifiedName of anonymous type not always the same?

I was trying to find an anonymous type from a collection of assemblies by AssemblyQualifiedName, but though the anonymous type is present in the scanned assembly it's not found. GetTypes() seems to return types with other AssemblyQualifiedNames.

Why is that, and what can I do to find the type in the given assemblies?

    [Fact]
    public void AnonTypes()
    {
        var entity = new { SomeString = "Asger" };
        var type = entity.GetType();
        var assemblyQualifiedName = type.AssemblyQualifiedName;

        var types = type.Assembly.GetTypes()
            .Where(x => x.AssemblyQualifiedName == assemblyQualifiedName)
            .ToList();

        types.Count.ShouldBe(1);
    }

Please note that Type.GetType(assemblyQualifiedName) finds the type, but I can not use this method as I not always have the AssemblyQualifiedName, but some other qualifier to search for.

Also note that same thing happens if entity is a ValueTuple.





Aucun commentaire:

Enregistrer un commentaire