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.
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.
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.
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).
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());