jeudi 12 mars 2020

C# Reflection - Get Generic Argument type of Super class from within the Base class

Consider the classes:

public class Super : Base<Test, Super>
{
    public Super Next { get; set; }
}

public class Base<TArg, TSuper>
{
    public Type GetTestArgument()
    {
        Type[] arguments = typeof(TSuper).GetProperty("Next").PropertyType.BaseType.GetGenericTypeDefinition().GetGenericArguments();
        // arguments is set to [ TArg, TSuper ] instead of [ Test, Super ]
        return arguments[0]; // should be Test
    }
}

Does anyone know how to get the generic type from within the base class?





Aucun commentaire:

Enregistrer un commentaire