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
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.
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();
}
}