Take the 2-minute tour ×  Under a given namespace, I have a set
of classes which implement an interface. Let's call it
ISomething. I have another class (let's call it CClass) which
knows about ISomething but doesn't know about the classes which
implement that interface.  I would like that CClass to look for
all the implementation of ISomething, instantiate an instance of
it and execute the method.  Does anybody have an idea on how to
do that with C# 3.5?   share|improve this question possible
duplicate of Getting all types that implement an interface with
C# 3.0 –  Kazark Mar 10 at 23:59  5 Answers 5  up vote 74 down
vote accepted  A working code-sample:  var instances = from t in
Assembly.GetExecutingAssembly().GetTypes()                 where
t.GetInterfaces().Contains(typeof(ISomething))
&& t.GetConstructor(Type.EmptyTypes) != null
select Activator.CreateInstance(t) as ISomething;  foreach (var
instance in instances)     instance.Foo(); // where Foo is a
method of I