jeudi 19 mai 2016

Inject method/command to all constructors in C#

assume following scenario, where you have hundreds of different classes that do not share a common base class, but you need every instance of every class registered to a ReferenceRegistry (which is a List where you put all the instances).

Of course I'd like to have a single class where all the magic happens, rather than editing all classes (and put a ReferenceRegistry.Add(this) in every constructor or attach an attribute).

I was thinking to search the assembly at the very start of the program and inject a method to every class. This method is automatically called whenever the constructor of that class is called. Or inject the command directly to the constructor.

Is this even possible? If so, how? Is there another and better approach to get all instances in my project?





Aucun commentaire:

Enregistrer un commentaire