Strange question

  • Thread starter Thread starter Jim H
  • Start date Start date
J

Jim H

Is there a way to programmatically get a list of classes that are contained
in a namespace? I have a namespace that holds a bunch of different data
classes, all derived from the same base class, that the user will choose
from. Rather than manually creating one of each type and putting it in a
list I was wondering if there was a way to programmatically query a
namespace for a list of types.

Thanks,
jim
 
Jim,

Unfortunately, there is not. A namespace is a logical grouping, with no
physical boundaries. You can have classes in different namespaces in
different assemblies.

What you can do, however, is get a list of types from each assembly that
is loaded, and then query the namespace from the type, and then group them
appropriately.

Hope this helps.
 
That will work. All of these classes are in their own assembly. How would
I do that? Can you point me in the right direction?

Thanks,
Jim

Nicholas Paldino said:
Jim,

Unfortunately, there is not. A namespace is a logical grouping, with
no physical boundaries. You can have classes in different namespaces in
different assemblies.

What you can do, however, is get a list of types from each assembly
that is loaded, and then query the namespace from the type, and then group
them appropriately.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jim H said:
Is there a way to programmatically get a list of classes that are
contained in a namespace? I have a namespace that holds a bunch of
different data classes, all derived from the same base class, that the
user will choose from. Rather than manually creating one of each type
and putting it in a list I was wondering if there was a way to
programmatically query a namespace for a list of types.

Thanks,
jim
 
Jim,

If you get the programattic representation of the assembly they are in
(the Assembly instance), then you can call GetTypes. This will return an
array of Type instances which represent the types in the array.

Once you have that, you can cycle through the array, and check the
Namespace property of each type. If it matches the one you are looking for,
then store the instance (in an ArrayList, perhaps), and then process the
list left over when you are done.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jim H said:
That will work. All of these classes are in their own assembly. How
would I do that? Can you point me in the right direction?

Thanks,
Jim

Nicholas Paldino said:
Jim,

Unfortunately, there is not. A namespace is a logical grouping, with
no physical boundaries. You can have classes in different namespaces in
different assemblies.

What you can do, however, is get a list of types from each assembly
that is loaded, and then query the namespace from the type, and then
group them appropriately.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jim H said:
Is there a way to programmatically get a list of classes that are
contained in a namespace? I have a namespace that holds a bunch of
different data classes, all derived from the same base class, that the
user will choose from. Rather than manually creating one of each type
and putting it in a list I was wondering if there was a way to
programmatically query a namespace for a list of types.

Thanks,
jim
 
Jim,

Take a look at System.Reflection.Assembly - this has the GetTypes() method,
which gives you a list of types that you can drill down into. You can get the
currently running assembly using the static method
System.Reflection.Assembly.GetCallingAssembly()

Hope this helps,
Chris.

Jim H said:
That will work. All of these classes are in their own assembly. How would
I do that? Can you point me in the right direction?

Thanks,
Jim

Nicholas Paldino said:
Jim,

Unfortunately, there is not. A namespace is a logical grouping, with
no physical boundaries. You can have classes in different namespaces in
different assemblies.

What you can do, however, is get a list of types from each assembly
that is loaded, and then query the namespace from the type, and then group
them appropriately.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jim H said:
Is there a way to programmatically get a list of classes that are
contained in a namespace? I have a namespace that holds a bunch of
different data classes, all derived from the same base class, that the
user will choose from. Rather than manually creating one of each type
and putting it in a list I was wondering if there was a way to
programmatically query a namespace for a list of types.

Thanks,
jim
 
Thanks, that helps a lot. How can I create and instance of a Type I find?

Type T = MyAssembly.GetTypes[0];

//I know this does not work but this is what I want to do.
//BaseClass is the base class that T was derived from.
BaseClass lTemp = new T;

Thanks again,
jim
Nicholas Paldino said:
Jim,

If you get the programattic representation of the assembly they are in
(the Assembly instance), then you can call GetTypes. This will return an
array of Type instances which represent the types in the array.

Once you have that, you can cycle through the array, and check the
Namespace property of each type. If it matches the one you are looking
for, then store the instance (in an ArrayList, perhaps), and then process
the list left over when you are done.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jim H said:
That will work. All of these classes are in their own assembly. How
would I do that? Can you point me in the right direction?

Thanks,
Jim

Nicholas Paldino said:
Jim,

Unfortunately, there is not. A namespace is a logical grouping, with
no physical boundaries. You can have classes in different namespaces in
different assemblies.

What you can do, however, is get a list of types from each assembly
that is loaded, and then query the namespace from the type, and then
group them appropriately.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Is there a way to programmatically get a list of classes that are
contained in a namespace? I have a namespace that holds a bunch of
different data classes, all derived from the same base class, that the
user will choose from. Rather than manually creating one of each type
and putting it in a list I was wondering if there was a way to
programmatically query a namespace for a list of types.

Thanks,
jim
 
Found it.
Assembly.CreateInstance()

Thanks for your help.

jim

Jim H said:
Thanks, that helps a lot. How can I create and instance of a Type I find?

Type T = MyAssembly.GetTypes[0];

//I know this does not work but this is what I want to do.
//BaseClass is the base class that T was derived from.
BaseClass lTemp = new T;

Thanks again,
jim
Nicholas Paldino said:
Jim,

If you get the programattic representation of the assembly they are in
(the Assembly instance), then you can call GetTypes. This will return an
array of Type instances which represent the types in the array.

Once you have that, you can cycle through the array, and check the
Namespace property of each type. If it matches the one you are looking
for, then store the instance (in an ArrayList, perhaps), and then process
the list left over when you are done.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jim H said:
That will work. All of these classes are in their own assembly. How
would I do that? Can you point me in the right direction?

Thanks,
Jim

in message Jim,

Unfortunately, there is not. A namespace is a logical grouping,
with no physical boundaries. You can have classes in different
namespaces in different assemblies.

What you can do, however, is get a list of types from each assembly
that is loaded, and then query the namespace from the type, and then
group them appropriately.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Is there a way to programmatically get a list of classes that are
contained in a namespace? I have a namespace that holds a bunch of
different data classes, all derived from the same base class, that the
user will choose from. Rather than manually creating one of each type
and putting it in a list I was wondering if there was a way to
programmatically query a namespace for a list of types.

Thanks,
jim
 
Back
Top