Classes constructor

  • Thread starter Thread starter Giojo
  • Start date Start date
G

Giojo

Hello guys!
I've a strange situation..
I have to fill a list with all classes defined in a particular
namespace..
I have to add all class defined in that namespace (there are many
classes..)
So instead to make
list.Add(new class1());
list.Add(new class2());
....
Is it possible to write
foreach (class in "MyProject.ClassesToAdd")
list.Add(classCostructor(class));
where "MyProject.ClassesToAdd" is a namespace?

thank you very much!
Giojo
 
Giojo said:
I've a strange situation..
I have to fill a list with all classes defined in a particular
namespace..
I have to add all class defined in that namespace (there are many
classes..)
So instead to make
list.Add(new class1());
list.Add(new class2());
...
Is it possible to write
foreach (class in "MyProject.ClassesToAdd")
list.Add(classCostructor(class));
where "MyProject.ClassesToAdd" is a namespace?

You can't do it quite like that. For any one assembly, you can list the
types in the assembly (Assembly.GetTypes()), and pick out each one in
the right namespace (Type.Namespace). Also, for any type, you can call
a constructor reflectively, eg using Activator.CreateInstance. Between
the two of those, I think you can do what you want. If you have any
problems with either of the steps, give details (and how far you've
got) and we should be able to put you back on the right track.

Jon
 
Back
Top