Reflection:: Getting Namespaces, Classes & Interfaces names

I want to fill a combo box with the installed .Net Namespaces
and another combo box with The interfaces of a selected Namespace
and a combo box for classes in that Namespace too?!!!

in general how can I get these information from .Net
Please I need help in that.
Thanks

[287 byte] By [codefund.com] at [2007-12-16]
# 1
I don't know how to retrieve a list of "available" namespaces--I suppose you want to iterate through the assemblies in the GAC, and I didn't see a programmatic way to do this. Obviously, there IS such a method, 'cause the Add Reference dialog box does this.

In any case, for a given assembly, here's simple code to list out class and/or interfaces. There are many ways to optimize this code for your own needs, but this sample code just lists information to the immediate window:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.LoadWithPartialName("System.Drawing")
ListClasses(asm)
ListInterfaces(asm)
End Sub

Private Sub ListClasses(ByVal asm As System.Reflection.Assembly)
Dim typ As System.Type
For Each typ In asm.GetExportedTypes()
If typ.IsClass Then
Debug.WriteLine(typ.Name)
End If
Next
End Sub

Private Sub ListInterfaces(ByVal asm As System.Reflection.Assembly)
Dim typ As System.Type
For Each typ In asm.GetExportedTypes()
If typ.IsInterface Then
Debug.WriteLine(typ.Name)
End If
Next
End Sub
Hope that helps get you started. Look in the framework documentation for the System.Reflection namespace for more info.

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
Thanks a lot for your help.
you gave me a good start which solve my problem.
thanks again
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
Great. If you come up with code to iterate through all the assemblies in the GAC, please do post it. It would be interesting to see how that's done.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
well I build this very simple class to do the job, to be honest, the class browser application in the quick start samples was a great reference in that.
I converted the code to suits my needs...hope that what I had done is a good idea.
please tell me your opinion about the code, as always I'd like to know others opinion specially professionals to improve my code and work
please feed back this code.
thanks

using System;
using System.Reflection;
using System.Collections.Specialized;
using System.Collections;
public class AssemblyLoader
{
//Constant strings that hold name of main .Net Assimplies (.DLL)
private const string m_WinForms = "System.Windows.Forms, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
private const string m_DrawingLib = "System.Drawing, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
private const string m_DataLib = "System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
private const string m_XmlLib = "System.Xml, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
private const string m_MessagingLib = "System.Messaging, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
private const string m_DirectoryServicesLib = "System.DirectoryServices, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
private const string m_WebLib = "System.Web, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
private const string m_NetFramworkClassLib = "mscorlib, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
private const string m_NetFramworkClassLibExtensions = "System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";

private readonly StringCollection m_ModuleNames;
public AssemblyLoader()
{
m_ModuleNames=new StringCollection();
m_ModuleNames.Add(m_WinForms);
m_ModuleNames.Add(m_DrawingLib);
m_ModuleNames.Add(m_DataLib);
m_ModuleNames.Add(m_XmlLib);
m_ModuleNames.Add(m_MessagingLib);
m_ModuleNames.Add(m_DirectoryServicesLib);
m_ModuleNames.Add(m_WebLib);
m_ModuleNames.Add(m_NetFramworkClassLib);
m_ModuleNames.Add(m_NetFramworkClassLibExtensions);
}
//get the Namespaces in an ArrayList
public ArrayList ListNamespaces()
{
ArrayList NamespacesList = new ArrayList();
Hashtable NamespacesHash = new Hashtable();

for (int y = 0; y < m_ModuleNames.Count; y++)
{

Module[] CorRuntime = Assembly.Load(m_ModuleNames[y]).GetModules();
Type[] CorClasses = CorRuntime[0].GetTypes();

for( int x=0; x < CorClasses.Length; x++ )
{
if ( CorClasses[x].Namespace != null )
{
if (!NamespacesHash.ContainsKey(CorClasses[x].Namespace) && CorClasses[x].IsPublic )
{
NamespacesHash.Add(CorClasses[x].Namespace,"");
NamespacesList.Add(CorClasses[x].Namespace);
}
}
}
}

NamespacesList.Sort();
return NamespacesList;
}

public void ListClassesAndInterfaces(string CurrentNamespace,out ArrayList ClassList,out ArrayList InterfaceList)
{
ClassList = new ArrayList(20);
InterfaceList = new ArrayList(20);

for( int y=0; y < m_ModuleNames.Count; y++ )
{

Module[] CorRuntime = Assembly.Load(m_ModuleNames[y]).GetModules();
Type[] CorClasses = CorRuntime[0].GetTypes();

for (int x=0; x < CorClasses.Length; x++ )
{

if ( CorClasses[x].Namespace == CurrentNamespace && CorClasses[x].IsPublic)
{
if ( CorClasses[x].IsInterface )
InterfaceList.Add(CorClasses[x].Name);
else
ClassList.Add(CorClasses[x].Name);
}
}
}
InterfaceList.Sort();
ClassList.Sort();
}
}

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
Well, it's more complex than the code I had posted, and doesn't really seem to get you much more functionality. But it seems fine. It doesn't, of course, loop through all the assemblies in the GAC, but limits you to a very small, fixed subset.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6
Well, I'd like to know how could I loop through all the assemblies in the GAC?!!!

and I the functionality I want it from what I made is just to fill a combo box with the all namespaces in the .Net ..
then when I chose a namespace from the namespaces combo, the classes is loaded into another Combo and alos the interfaces into another one..
that is all what I need from my code..

please if you know more effecint way tell me
also I want to know how to loop through all the assemblies in the GAC..thanks a lot

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...