Get all public methods in the project

I have a project. With # .net I would like to get the names of all public functions in all public classes of a project.

is there any tool or can I write a program that accepts a project dll or even a project directory as input and find all public functions?

+4
source share
3 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 planning with this information?

+8
source

You can find it using System.Reflection.MethodInfo

Suppose you have a class with the 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 the 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 will get the following result:
Method: getMyDouble
Method: myMethod
Method: get_MyProp
Method: set_MyProp
Method: MethodA
Method: MethodB
Method: ToString
Method: equal
Method: GetHashCode
Method: GetType

+1
source

If you speak during development, you are looking at one of the following:

  • Writing your own source code analyzer.
  • Writing your own or searching for a third-party visual studio plugin.
  • Compile, then load the DLL into a tool such as .NET Reflector .

If you speak at runtime, you are looking at using .NET reflection through one or more of the following methods / classes:

  • AppDomain.CurrentDomain.GetAssemblies() // returns loaded Assemblies (ie DLLs).
  • myAssembly.GetTypes() // returns an array of Type's.
  • myType.GetMethods() // returns an array of MethodInfo's.
0
source

Source: https://habr.com/ru/post/1343120/


All Articles