7

i have a project. c# .net I would like to get names of all public function in all public classes in the project.

is there any tool or can i write a program that take the project dll or even the project directory ,as input ,and find all public function?

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Kuttan Sujith
  • 7,889
  • 18
  • 64
  • 95

3 Answers3

10

This probably does what you want:

MethodInfo[] methods = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).SelectMany(x => x.GetMethods().Where(y => y.IsPublic)).ToArray();

Out of curiosity, what are you plans with this info?

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
  • actually am going to run efprofiler. So i would like to get all method names to be printed in a papper.i will mark each method pass the efprofiler test :( but none of your answer will help(except DuckMaestro' second one) – Kuttan Sujith Mar 10 '11 at 06:56
1

You can find it by using System.Reflection.MethodInfo

Lets say you have a class with following methods in interfaces:

public interface IFaceOne {
  void MethodA();
}

public interface IFaceTwo {
  void MethodB();
}

public class MyClass: IFaceOne, IFaceTwo {
  public int myIntField;
  public string myStringField;
    private double myDoubleField = 0;


    public double getMyDouble(){
      return myDoubleField;
    }

  public void myMethod(int p1, string p2)
  {
  }

  public int MyProp
  {
    get { return myIntField; }
    set { myIntField = value; }
  }

  public void MethodA() {}
  public void MethodB() {}
}

Then use following code to read all methods/properties:

public static void Main(string[] args)
{
TheType.MyClass aClass = new TheType.MyClass();

Type t = aClass.GetType();
MethodInfo[] mi = t.GetMethods();
foreach(MethodInfo m in mi)
  Console.WriteLine("Method: {0}", m.Name);
}

You would get following result:
Method: getMyDouble
Method: myMethod
Method: get_MyProp
Method: set_MyProp
Method: MethodA
Method: MethodB
Method: ToString
Method: Equals
Method: GetHashCode
Method: GetType

OrahSoft
  • 783
  • 1
  • 5
  • 7
0

If you're talking about at design-time, then you're looking at one of the following:

  1. Writing your own source code parser.
  2. Writing your own or finding a 3rd party visual studio plugin.
  3. Compiling then loading the DLL in a tool such as .NET Reflector.

If you're talking about at run-time, then you're looking at using .NET reflection, through one or more of the following methods/classes:

  1. AppDomain.CurrentDomain.GetAssemblies() // returns loaded Assemblies (i.e. DLLs).
  2. myAssembly.GetTypes() // returns an array of Type's.
  3. myType.GetMethods() // returns an array of MethodInfo's.
DuckMaestro
  • 15,232
  • 11
  • 67
  • 85