生成具有方法类型的类的方法列表 [英] Generate List of methods of a class with method types

查看:98
本文介绍了生成具有方法类型的类的方法列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个类或类目录中所有方法的列表.我还需要他们的返回类型.将其输出到文本文件将可以...是否有人知道工具,用于VS的插入式工具或将执行此任务的工具?即时通讯使用C#代码,Visual Studio 2008作为IDE

I want to generate a list of all methods in a class or in a directory of classes. I also need their return types. Outputting it to a textfile will do...Does anyone know of a tool, lug-in for VS or something which will do the task? Im using C# codes by the way and Visual Studio 2008 as IDE

推荐答案

确定-使用Type.GetMethods().您将需要指定不同的绑定标志以获取非公共方法等.这是一个很粗糙但可行的起点:

Sure - use Type.GetMethods(). You'll want to specify different binding flags to get non-public methods etc. This is a pretty crude but workable starting point:

using System;
using System.Linq;

class Test
{
    static void Main()
    {
        ShowMethods(typeof(DateTime));
    }

    static void ShowMethods(Type type)
    {
        foreach (var method in type.GetMethods())
        {
            var parameters = method.GetParameters();
            var parameterDescriptions = string.Join
                (", ", method.GetParameters()
                             .Select(x => x.ParameterType + " " + x.Name)
                             .ToArray());

            Console.WriteLine("{0} {1} ({2})",
                              method.ReturnType,
                              method.Name,
                              parameterDescriptions);
        }
    }
}

输出:

System.DateTime Add (System.TimeSpan value)
System.DateTime AddDays (System.Double value)
System.DateTime AddHours (System.Double value)
System.DateTime AddMilliseconds (System.Double value)
System.DateTime AddMinutes (System.Double value)
System.DateTime AddMonths (System.Int32 months)
System.DateTime AddSeconds (System.Double value)
System.DateTime AddTicks (System.Int64 value)
System.DateTime AddYears (System.Int32 value)
System.Int32 Compare (System.DateTime t1, System.DateTime t2)
System.Int32 CompareTo (System.Object value)
System.Int32 CompareTo (System.DateTime value)
System.Int32 DaysInMonth (System.Int32 year, System.Int32 month)

(等)

这篇关于生成具有方法类型的类的方法列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆