Get All Namespace used in a Assembly

  • Thread starter Thread starter Kiran
  • Start date Start date
K

Kiran

Hi,

I want to load a Assembly and get all the namespace ussed in that Assembly
via my C# program, I know it can be done, but do not know how exactly its
possible. Could some one give me a pointer how this can be done?

Regards
Kiran
 
I want to load a Assembly and get all the namespace ussed in that Assembly
via my C# program, I know it can be done, but do not know how exactly its
possible. Could some one give me a pointer how this can be done?

Something like

ArrayList namespaces = new ArrayList();
foreach ( Type t in asm.GetTypes() )
if ( !namespaces.Contains( t.Namespace ) )
namespaces.Add( t.Namespace );


Mattias
 
Hi Mattias,

Thanks for the reply.
ArrayList namespaces = new ArrayList();
foreach ( Type t in asm.GetTypes() )
if ( !namespaces.Contains( t.Namespace ) )
namespaces.Add( t.Namespace );

I did have a look in to that, but this do not look like the ideal way of
doing things, if I have only one namespace in my assembly & 10000 types, it
will loop so many times where as its not really required.

I am already on a performance problem, is there some other ways to get
around this problem without having to go via all the types in the Assembly?

Regards

Kiran
 
Kiran,
I did have a look in to that, but this do not look like the ideal way of
doing things, if I have only one namespace in my assembly & 10000 types, it
will loop so many times where as its not really required.

Well there's certainly room for improvements, I didn't bother to
optimize it. You could make the lookup faster than Contains() if you
use a set class, a Hashtable or keep the ArrayList sorted and use
BinarySearch() instead.

I am already on a performance problem

Really? How often is this going to run?

is there some other ways to get
around this problem without having to go via all the types in the Assembly?

No, not at the level Reflection works anyway.



Mattias
 
Type [] ta = System.Reflection.Assembly.GetExecutingAssembly().GetTypes();
foreach(Type t in ta)
{
Console.WriteLine(t.Namespace);
}

Adam
 
Mattias,
Well there's certainly room for improvements, I didn't bother to
optimize it. You could make the lookup faster than Contains() if you
use a set class, a Hashtable or keep the ArrayList sorted and use
BinarySearch() instead.

More than Optmization, I was looking at other ways of doing this.
You are right, i have moved to HashTable... that should help a bit in my
case.
Really? How often is this going to run?

This is the issue, it will be run very often, My initial impression is that
this will be called once but I am not in control of that process, and they
are calling this operation very often :(
Assembly?

No, not at the level Reflection works anyway.

OK.

Thanks for your help.

regards
Kiran.
 
This is the issue, it will be run very often, My initial impression is that
this will be called once but I am not in control of that process, and they
are calling this operation very often :(

Can't you just cache the result then, so you only have to do the
actual iteration once?



Mattias
 
Can't you just cache the result then, so you only have to do the
actual iteration once?

yes, probably thats something I can do, I have to look for the modified time
of those Assembly and decide if I need to refresh them.

Thanks.
Kiran
 
Back
Top