1

Is there a good way to, say, print a list of all the methods in my project or file?

And from there, list of classes or variables, etc.

5
  • 1
    Check out Red Gate Reflector. Commented Feb 10, 2012 at 15:50
  • You can use various things in the reflection namespace to query assemblies for what classes they contain, what methods they have, etc. Commented Feb 10, 2012 at 15:51
  • ... Or ILSpy, JustDecompile, DotPeek (these ones are actually free). Commented Feb 10, 2012 at 15:51
  • Like some sort of documentation generator? Do you comment your code with /// ? Commented Feb 10, 2012 at 15:53
  • 1
    You should add some context to your question. Are you trying to analyze your program while it is running ? Or analyzing the contents of a project in Visual Studio ? Commented Feb 10, 2012 at 15:55

6 Answers 6

2

You're looking for the System.Reflection namespace. You'll have to play around with the BindingFlags enumeration to get exactly what you want, but here's a quick example program:

using System;
using System.Reflection;

namespace ReflectionExample
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Type[] types = typeof (MyClass).Assembly.GetTypes();

            foreach (Type type in types)
            {
                Console.WriteLine(type.Name);

                MemberInfo[] members = type.GetMembers(
                    BindingFlags.Instance | BindingFlags.Public
                    | BindingFlags.FlattenHierarchy | BindingFlags.DeclaredOnly);

                foreach (MemberInfo memberInfo in members)
                {
                    Console.WriteLine("\t" + memberInfo.Name);
                }
            }

            Console.ReadLine();
        }

        public class MyClass
        {
            public int Bar { get; set; }

            public void AMethod()
            {
                Console.WriteLine("foo");
            }

            public void BMethod()
            {
                Console.WriteLine("foo");
            }

            public void CMethod()
            {
                Console.WriteLine("foo");
            }
        }
    }
}

Alternatively, you could /// document everything in your project and then turn on the XML documentation output in your project settings. I guess it depends on what you need this list for.

Sign up to request clarification or add additional context in comments.

Comments

2

Depends on what you're trying to do. If you're looking to analyze your .NET executable, you can use one of the free decompilation tools available (dotPeek, ILSpy or even .NET Reflector).

However, if you're looking for this information at runtime, that is, you want for example to print all the types and methods executing in your application, you can either use Reflection (here's a good tutorial on CodeProject), or use an AOP solution, like PostSharp, to print the names of all executing methods (see the Tracing example).

Comments

1

You can use Reflection to list classes, methods, properties.

Here Reflection

Hope this helps.

Comments

0

I'm not sure about print capabilities, but have you looked at .Net Reflector?

Comments

0

With reflection you can find any meta data about functions, classes etc. C# has very good support for it.

Comments

0
        string dllPathName = @"dllPath\dllFileName";
        string outputFilePathName = @"outputPath\outputFileName";
        System.Reflection.Assembly fileNetAssembly = System.Reflection.Assembly.LoadFrom(dllPathName);
        StringBuilder sb = new StringBuilder();

        foreach (Type cls in fileNetAssembly.GetTypes())
        {
            //only printing non interfaces that are abstract, public or sealed
            sb.Append(!cls.IsInterface ? (cls.IsAbstract ? string.Format("Class: {0} is Abstract.{1}", cls.Name, System.Environment.NewLine) :
                                            cls.IsPublic ? string.Format("Class: {0} is Public.{1}", cls.Name, System.Environment.NewLine) :
                                            cls.IsSealed ? string.Format("Class: {0} is Sealed.{1}", cls.Name, System.Environment.NewLine) :
                                            string.Empty) : 
                                        string.Empty);

            if (!cls.IsInterface && (cls.IsAbstract || cls.IsPublic || cls.IsSealed))
            {
                //printing all methods within the class
                foreach (System.Reflection.MemberInfo method in cls.GetMethods())
                {
                    sb.Append("\t- ");
                    sb.Append(method.Name);
                    sb.Append(System.Environment.NewLine);
                }
            }
        }

        //output the file
        File.WriteAllText(outputFilePathName, sb.ToString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.