vendredi 5 avril 2019

Is it possible to find the .NET class that uses my class as a generic type?

I have a DTO class:

public class MyDto {...}

and a validator that extends a generic validator of my class:

public class MyDtoValidator: AbstractValidator<MyDto>

In my code I know the typeof MyDto (and can create an instance if need be). Based on this type, is it possible to get to the type of MyDtoValidator using reflection? I know the assembly and the exact namespace where the validator is located, if that helps. I realize there can be any number of types that inherit from AbstractValidator<MyDto>, but in my case there will always be one, so even if I get a list I get a list of one.

I can find it by class name if I stick "Validator" at the end of my class (and I may end up having to do that) but I'm trying to limit dependence on naming conventions and string concatenation.





Aucun commentaire:

Enregistrer un commentaire