Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

to get all public methods in the project

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?

like image 931
Kuttan Sujith Avatar asked Mar 10 '11 06:03

Kuttan Sujith


2 Answers

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?

like image 144
Kirk Woll Avatar answered Sep 28 '22 03:09

Kirk Woll


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

like image 34
OrahSoft Avatar answered Sep 28 '22 02:09

OrahSoft