views:

223

answers:

3
+5  Q: 

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

A: 

You can start here with Assembly.GetExportedTypes()

http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getexportedtypes.aspx

Lou Franco
+1  A: 

Have you had a look at .NET Reflector from RedGate software. It has an export function.

jvanrhyn
It just exports all files and creates VS project. I don't really need that.
Jarek
+6  A: 

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

Rob
Need to group those classes per namespace in the LINQ query and output each namespace name also, if I interpret the OP correctly.
PHeiberg
@PHeiberg, I didn't say it was perfect! ;-) That said, I could've tweaked it to closer match what the OP was after. Fair point well made =)
Rob