Enumerar las inner classes de un proyecto (clases anidadas)

Ej, si se quieren obtener todas las inner classes de LibX.dll:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using LibX;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static StreamWriter sw;

        static void Main(string[] args)
        {            
            if (File.Exists(@"c:\temp\out.txt"))
                File.Delete(@"c:\temp\out.txt");

            sw = File.AppendText(@"c:\temp\out.txt");


            var theList = Assembly.GetExecutingAssembly().GetTypes()
                      .Where(t => t.Namespace == "X")
                      .ToList();

            var asm = Assembly.LoadFile(@"C:\crusso\X\fuentes\Desarrollo\_DLL\LibX\bin\debug\LibX.dll");

            foreach (var t in asm.ExportedTypes)
            {
                Type[] typelist = GetTypesInNamespace(asm, t.Namespace);
                for (int i = 0; i < typelist.Length; i++)
                {
                    var jj = ListarInner(typelist[i]);
                    if (jj != "")
                    {
                        sw.WriteLine(jj);
                    }
                }
            }

            var namespaces = asm.GetTypes()
                                     .Select(t => t.Namespace)
                                     .Distinct();

            sw.Close();

            Console.ReadLine();
        }



        private static Type[] GetTypesInNamespace(Assembly assembly, string nameSpace)
        {
            return assembly.GetTypes().Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal)).ToArray();
        }

        public static string ListarInner(Type t)
        {
            Type myType = (t);

            Type[] myArrayType = myType.GetNestedTypes(BindingFlags.Public);
            foreach (var tt in myArrayType)
            {
                bool isStruct = tt.IsValueType && !tt.IsEnum;

                if (!tt.IsEnum && !isStruct)
                {
                    return tt + Environment.NewLine + ListarInner(tt);
                }
            }

            return "";
        }
    }
}



No hay comentarios:

Publicar un comentario