Register all declared child classes of a class in C #

In the proof of concept project I'm working on, I have a generic abstract class that I expect to extend to several child classes:

public abstract class Pet { public abstract string PetTypeName {get;} } public class Dog : Pet { public override string PetTypeName { get { return "Dog"; } } } 

I would like to have a static set filled with all the subtypes of this class:

 public static List<Type> AllPetTypes = new List<Type> { typeof(Dog), typeof(Cat), //... }; 

But this just adds another step when we want to introduce a new subtype, and it is error prone, because this is another thing that a programmer can forget at runtime. How to create a collection like this automatically, including all types in my assembly that inherit this parent class?

Edit

In response to the question, what will I use for this:

Since we have several clients that use the same SaaS product, but with access to different subsets of the portal’s “channels”, it can be difficult to track how a user got to a particular page and where to send them when they click Finish .

So, I'm collecting brainstorming opportunities for architecture for the ASP.NET portal, where each page is a truly advanced user control that accepts a specific, strongly typed class for storing its parameters and all tasks related to tracking breadcrumbs, creating a URL and thus abstracted into the framework. Thus, the structure must know all the controls that can be processed as if they were pages. In the ideal case, creating a new user control within the framework and creating its code for the class can lead to the fact that a certain base class will be sufficient to register this control as a new page in the system.

This leads me to the following question: will the type search in this assembly, using the methods mentioned in the proposed solutions to this issue, work in the context of the ASP.NET web project, where the types are declared in the code behind? I'm not quite sure at what point the user controls are compiled or if they are considered part of the web project assembly. Does anyone have more info? (Should I ask a new question?)

+4
source share
3 answers
 var targetAssembly = Assembly.GetExecutingAssembly(); // or whichever var subtypes = targetAssembly.GetTypes().Where(t => t.IsSubclassOf(typeof(Pet))); 
+13
source

You can start with something like this. This will give you all types in the same assembly as the defining type derived from the defining type.

 class Aware { public static readonly IEnumerable<Type> AllTypes; static Aware() { Type awareType = typeof(Aware); allTypes = Assembly.GetAssembly(awareType) .GetTypes() .Where(t => awareType.IsAssignableFrom(t)); } } 

Test:

 class Foo : Aware { } class Bar : Aware { } foreach (var type in Aware.AllTypes) { Console.WriteLine(type); } 

Conclusion:

 Aware Foo Bar 

I'm curious to know what you used it for.

+3
source
 Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsSubclassOf(typeof(parentClass))) 
0
source

Source: https://habr.com/ru/post/1300342/


All Articles