开发者

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 fil开发者_Go百科e. I just need a list of all public methods and properties.

What's the best way to accomplish that?

TIA for any answers


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


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


You can start here with Assembly.GetExportedTypes()

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜