Posts
74
Comments
93
Trackbacks
0
Enumerating all of the namespaces and their respective classes in an assembly (dll).

improve my => 'code'

I wrote some code in a test project, and then had to list all of the namespaces I used in my project for a manager so they could conform to a standard for production... As I used more than 50 namespaces for my test project, I wanted to do this automatically... Also I wanted to show all of the classes contained in each namespace...  Anyway, I used the following code... Thought this might be useful to others...  (If someone reading this sees a good way to refactor this, it would be appreciated!)

      Dictionary<string, List<string>> colNamespaces = new Dictionary<string, List<string>>();

      Assembly assembly = Assembly.GetAssembly( Type.GetType("Test2.Facade.ConcreteFacade,Test2"));

      Type[] theTypes = assembly.GetTypes();

      foreach ( Type t in theTypes)

            {

            string theNameSpace = t.FullName.Substring(0,t.FullName.LastIndexOf("."));

            string theClass = t.FullName.Substring(t.FullName.LastIndexOf(".")+1);

            if (colNamespaces.ContainsKey(theNameSpace))

            {

                colNamespaces[theNameSpace].Add(theClass);

            }

            else

            {

                List<string> classes = new List<string>();

                classes.Add( theClass );

                colNamespaces[theNameSpace] = classes;

            }

        }

I hope you find this helpful -

Jonathan Starr 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Monday, October 22, 2007 1:27 PM Print
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  
 
Jonathan Starr is a developer in Saint Louis, MO. He holds an MBA in Finance from Columbia Business School and earned his MCSD from Microsoft.


All statements in this blog are personal opinions and do not reflect the opinions of his employer.





Related Sites
Join My Community at MyBloglog!
Tag Cloud