lundi 12 octobre 2020

C#: Get a child class's nested type by name, when that child class inherits from a base class that holds those nested types

First off, I fully realize this is a bit strange and probably not optimal, but I'm still looking for a way to do it :)

I have a class similar to this:

public class Fruit {
  public class Seed { } 
  public class Pit { } 
}

Then I have a subclass that inherits:

public class Avocado : Fruit {}

Elsewhere in the code, I have a string that says "Avocado.Pit", and I need to get that exact Type.

typeof(Fruit).GetNestedTypes() does in fact contain Seed and Pit, but typeof(Avocado).GetNestedTypes() returns empty though, as Seed and Pit are on Fruit, not on Avocado (this is expected: even though Avocado inherits from Fruit, the documentation for GetNestedTypes() mentions that nested classes that come from the base type are omitted). If I manually try new Avocado.Pit(); or SomeGenericMethod<Avocado.Pit>();, it does work as expected, so it seems like this should be possible.

So how can I do something like:

System.Type desiredType = System.Type.GetNestedTypesIncludingInherited("Avocado.Pit");
// desiredType.FullName would be Avocado.Pit, not Fruit.Pit.

I expect the real way will require a few more hoops to jump through, but that's fine; is this possible? Thanks very much!





Aucun commentaire:

Enregistrer un commentaire