Search code examples
c#.netgenerator

C# API generator


I have a few dll files and I want to export all public classes with methods separated by namespaces (export to html / text file or anything else I can ctrl+c/v in Windows :) ).

I don't want to create documentation or merge my dlls with xml file. I just need a list of all public methods and properties.

What's the best way to accomplish that?

TIA for any answers


Solution

  • Very rough around the edges, but try this for size:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    
    namespace GetMethodsFromPublicTypes
    {
        class Program
        {
            static void Main(string[] args)
            {
                var assemblyName = @"FullPathAndFilenameOfAssembly";
    
                var assembly = Assembly.ReflectionOnlyLoadFrom(assemblyName);
    
                AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);
    
                var methodsForType = from type in assembly.GetTypes()
                                     where type.IsPublic
                                     select new
                                         {
                                             Type = type,
                                             Methods = type.GetMethods().Where(m => m.IsPublic)
                                         };
    
                foreach (var type in methodsForType)
                {
                    Console.WriteLine(type.Type.FullName);
                    foreach (var method in type.Methods)
                    {
                        Console.WriteLine(" ==> {0}", method.Name);
                    }
                }
            }
    
            static Assembly CurrentDomain_ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args)
            {
                var a = Assembly.ReflectionOnlyLoad(args.Name);
                return a;
            }
        }
    }
    

    Note: This needs refinement to exclude property getters/setters and inherited methods, but it's a decent starting place