Q: Anyway to enumerate the Classes in a Namespace?

  • Thread starter Thread starter Sky
  • Start date Start date
S

Sky

I have a namespace called Schema, in which I have defined 3 classes...

Is there a way of getting a list of these 3 class names?

Thank you in advance.
 
FYI: What I just wrote to list the classes within a namespace, and it works
(bare typos...),
is listed below...I just havn't figured out a way to pass a Namespace as an
argument...
(BTW: What is a namespace -- is there a System.Type for it? Couldn't see
one)...

Any and all help appreciated!

public string[] EnumerateClassesInNameSpace(System.Type
qAClassWithinNamespace){

System.Collections.ArrayList tResult = new System.Collections.ArrayList();

System.Type tTypeIN = qClassWithinNamespace;

System.Type tType;

string tNS = tTypeIN.Namespace;

System.Type[] tTypes = tTypeIN.Assembly.GetTypes();

for (int i=0;i<tTypes.Length;i++){

tType = tTypes;

if ( (tType.MemberType == System.Reflection.MemberTypes.TypeInfo) &&

(tType.Namespace == tNS) ){

tResult.Add(tType);

}

}

return (System.String)tResult.ToArray(typeof(System.String));

}
 
Sky said:
FYI: What I just wrote to list the classes within a namespace, and it
works
(bare typos...),
is listed below...I just havn't figured out a way to pass a Namespace as
an
argument...
(BTW: What is a namespace -- is there a System.Type for it? Couldn't see
one)...

A namespace is really a very vauge concept. As far as the typing system
goes, namespaces are just a part of the name(although they are stored
seperatly physically). There is no Type for a namespace, nor is a namespace
restricted to a single assembly. The best you can do is enumerate all types
in a given assembly and match hte namespace to the namespace in the type,
much as you do below. You'll have to pass the namespace as a string.

Any and all help appreciated!

public string[] EnumerateClassesInNameSpace(System.Type
qAClassWithinNamespace){

System.Collections.ArrayList tResult = new System.Collections.ArrayList();

System.Type tTypeIN = qClassWithinNamespace;

System.Type tType;

string tNS = tTypeIN.Namespace;

System.Type[] tTypes = tTypeIN.Assembly.GetTypes();

for (int i=0;i<tTypes.Length;i++){

tType = tTypes;

if ( (tType.MemberType == System.Reflection.MemberTypes.TypeInfo) &&

(tType.Namespace == tNS) ){

tResult.Add(tType);

}

}

return (System.String)tResult.ToArray(typeof(System.String));

}
 
Thanks Daniel for the info. ;-(



Daniel O'Connell said:
Sky said:
FYI: What I just wrote to list the classes within a namespace, and it
works
(bare typos...),
is listed below...I just havn't figured out a way to pass a Namespace as
an
argument...
(BTW: What is a namespace -- is there a System.Type for it? Couldn't see
one)...

A namespace is really a very vauge concept. As far as the typing system
goes, namespaces are just a part of the name(although they are stored
seperatly physically). There is no Type for a namespace, nor is a namespace
restricted to a single assembly. The best you can do is enumerate all types
in a given assembly and match hte namespace to the namespace in the type,
much as you do below. You'll have to pass the namespace as a string.

Any and all help appreciated!

public string[] EnumerateClassesInNameSpace(System.Type
qAClassWithinNamespace){

System.Collections.ArrayList tResult = new System.Collections.ArrayList();

System.Type tTypeIN = qClassWithinNamespace;

System.Type tType;

string tNS = tTypeIN.Namespace;

System.Type[] tTypes = tTypeIN.Assembly.GetTypes();

for (int i=0;i<tTypes.Length;i++){

tType = tTypes;

if ( (tType.MemberType == System.Reflection.MemberTypes.TypeInfo) &&

(tType.Namespace == tNS) ){

tResult.Add(tType);

}

}

return (System.String)tResult.ToArray(typeof(System.String));

}

 
Back
Top