I have 200 classes in asp.net application that all must be executed(some method called) together.These classes are of same interface. How should I write the code to make them all executed together, I don't want to do in declaring all objects of that classes in one file and then calling them one by one, because this approach is error-prone because I can forget to add some new classes. I would like to set execution from these classes from within, using some binding to some event or something, how should I create such class model?
Asked
Active
Viewed 71 times
0
-
cl1 o1 = new cl1(); cln on = new cln(); o1.Execute(); on.Execute(); and so on... also Method Chain can be used – Andron Apr 12 '13 at 12:55
-
How about a List
populated with instances of implementors of IMyInterface? – MikeSW Apr 12 '13 at 13:01 -
for this approach I need to populate List
from some place where I have to initiate N times my N objects in N lines of code. And I want to avoid it. Is it possible? – Andron Apr 12 '13 at 13:08 -
Of course, make that list a singleton. What are you trying to do is actually trivial assuming you know how how asp.net works and a bit of reflection. Probably you can even use a static field. – MikeSW Apr 12 '13 at 13:24
-
1@Andron you could load classes inheriting from your common Interface by reflection. this answer could help you http://stackoverflow.com/q/5411694/1061499 – davioooh Apr 12 '13 at 13:24
-
Incredible, this solves all my problems!!! Now I can relax at weekend. Thank you @davioooh very much!!! – Andron Apr 12 '13 at 14:00
1 Answers
0
Depending on how sophisticated you wanted to be, you could use the Managed Extensiblity Framework or MEF. If you marked all of your classes with MEF attributes, you could easily load them into a collection and call them all.
MEF uses reflection under the covers, but it encapsulates it for you so you don't have to worry about it.
There is a very simple example of how to use MEF here.

epotter
- 7,631
- 7
- 63
- 88