Obtener el NameSpace y los tipos de un ensamblado

by Misael Monterroca 21. May 2010 14:13

Para un documento de arquitectura que estoy elaborando, me vi en la necesidad de obtener los namespaces y los tipos de un determinado ensamblado, el siguiente código les permitirá obtenerlos utilizando reflection y escribirlos en un archivo de texto.

La aplicación leerá los ensamblados que se encuentren dentro de un directorio dejando un archivo llamado datos.txt que tendrá un contenido similar al siguiente:

Para el ensamblado Contoso.SN.Framework
NAMESPACES

Contoso.SN.Framework
Contoso.SN.Framework.Exceptions
Contoso.Sacip.Framework.Infopath
……..

CLASES

ConfigurationException
ConfigurationSettings
……

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
            {
                DirectoryInfo directory = new DirectoryInfo(@"C:\dlls");
                var dls = directory.GetFiles("*.dll");

                if (File.Exists("datos.txt"))
                    File.Delete("datos.txt");
            
            using (StreamWriter writer = File.CreateText("datos.txt"))
                {
                    foreach (var item in dls)
                    {

                        var assembly = Assembly.LoadFrom(item.FullName);

                        Console.WriteLine("Para el ensamblado " + assembly.GetName().Name);
                        writer.WriteLine("Para el ensamblado " + assembly.GetName().Name);
                        writer.WriteLine("NAMESPACES ");
                        writer.WriteLine();

                        var namespaces = assembly.GetTypes().Select(T => T.Namespace).Distinct();
                        foreach (var type in namespaces)
                        {
                            Console.WriteLine(type);
                            writer.WriteLine(type);
                        }

                        writer.WriteLine();
                        writer.WriteLine();

                        writer.WriteLine("Tipos ");
                        var types = assembly.GetTypes().OrderBy(T => T.Name);
                        foreach (var type in types)
                        {
                            if(!type.IsNested)
                                writer.WriteLine(type.Name);
                        }

                        writer.WriteLine();
                        writer.WriteLine();
                    }
                }

                Console.ReadKey();
            }
    }
}

Tags:

Comments

3/31/2011 9:43:17 PM #

global mobility

The post stranded me, in the third sentence of Obtener el NameSpace y los tipos de un ensamblado. I suppose I get what you are illustrating. I realise what you are articulating, but you should notice that you will encounter some other types on the planet who may perhaps not fall into line with you.  See that you do not encumber yourself. Lots of writers restrict themselves to items they think they are able to perform. Be confident you can travel as far as your head will permit you.

global mobility United Kingdom

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



MVP