471,348 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Classes constructor

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

Dec 5 '05 #1
1 975
Giojo wrote:
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

Dec 5 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Randy | last post: by
7 posts views Thread by sureshsundar007 | last post: by
45 posts views Thread by Ben Blank | last post: by
8 posts views Thread by Robert W. | last post: by
74 posts views Thread by Zytan | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.