Reflection question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!

I want to instantiate one object of every class under a particular
namespace. I then want to check their type, and if they are inheriting from a
particular interface I want to do stuff with them.

Can anyone suply som examplecode?

Many thanx!
 
Paul said:
I want to instantiate one object of every class under a particular
namespace. I then want to check their type, and if they are inheriting from a
particular interface I want to do stuff with them.

Can anyone suply som examplecode?

Well, there are several steps in there. Which one are you having
problems with?

I'd suggest you look particularly at:

Assembly.GetTypes
Type.Namespace
Activator.CreateInstance
Type.IsAssignableFrom (or just use "is" after creating the instance, if
you're creating an instance of all types anyway)
 
Thank you!

That´s exactly what I needed to know!
For your information, below follows the code that solved the problem.

public static ArrayList GetTemplateControls()
{
ArrayList alTemplateControls = new ArrayList();

Type[] allTypes = System.Reflection.Assembly.GetCallingAssembly().GetTypes();

foreach(Type type in allTypes)
{
if(type.Namespace.EndsWith(".TemplateControls"))
{
object templateControl = Activator.CreateInstance(type);
if(templateControl is TemplateControl)
alTemplateControls.Add(templateControl);
}
}

return alTemplateControls;
}
 
Back
Top