ManoRoshan
ManoRoshan

Reputation: 139

How to get namespace, class, methods and its arguments with Reflection

I need to load the dll using reflection and have to get the namespace, class, methods and its arguments for that dll. Also, I need to write those information in a log file.

I have used the following code. But I got only up to class that has been written to log file.

Assembly dll = Assembly.LoadFile(@"D:\Assemblies\Myapplication.dll");
foreach (Type type in dll.GetTypes())
{
    var name = "Myapplication~" + type.FullName;
    File.AppendAllText("Assembly Details.log", "Myapplication~" + type.FullName + "\r\n\r\n");

    Console.WriteLine(type.FullName);
}

In the log file, the information needs to written as below.

Myapplication~namespace.class~method(arguments)

Eg:

Myapplication~copyassembly.class~Mymethod(String, Type)

Any suggestions would be greatly helpful.

Upvotes: 4

Views: 1313

Answers (3)

Alberto Monteiro
Alberto Monteiro

Reputation: 6219

Using a sort of LINQ its possible to do this in this way

var dll = Assembly.LoadFile(@"D:\Trabalho\Temp\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe");

var logItems = dll.GetTypes()
    .SelectMany(t => t.GetMethods(), Tuple.Create)
    .OrderBy(t => t.Item1.Namespace)
    .ThenBy(t => t.Item1.Name)
    .ThenBy(t => t.Item2.Name)
    .Select(t => $"{"ConsoleApplication1"}~{t.Item1.FullName}~{t.Item2.Name}({string.Join(", ", t.Item2.GetParameters().Select(p => p.ParameterType.Name))})");

Console.WriteLine(string.Join(Environment.NewLine, logItems));

And then this output

ConsoleApplication1~MyNamespace.Program~Equals(Object)

ConsoleApplication1~MyNamespace.Program~GetHashCode()

ConsoleApplication1~MyNamespace.Program~GetType()

ConsoleApplication1~MyNamespace.Program~Main(String[])

ConsoleApplication1~MyNamespace.Program~ToString()

Then you can save it in file

File.AppendAllLines("Assembly Details.log", logItems);

Upvotes: 1

Arghya C
Arghya C

Reputation: 10068

You can write a code similar to this

Assembly dll = Assembly.LoadFile(@"C:\YourDirectory\YourAssembly.dll");

foreach (Type type in dll.GetTypes())
{
    var details = string.Format("Namespace : {0}, Type : {1}", type.Namespace, type.Name);
    //write details to your log file here...
    Console.WriteLine(details);

    foreach (var method in type.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
    {
        var methodDetails = string.Format("{0} {1}({2})", method.ReturnParameter, method.Name, 
                                            string.Join(",", method.GetParameters().Select(p => p.ParameterType.Name)));
        //write methodDetails to your log file here...
        Console.WriteLine("\t" + methodDetails);
    }
}

Which will write these details

Namespace : MyNamespace.Helpers, Type : Constants
    System.String  ToString()
    Boolean  Equals(Object)
    Int32  GetHashCode()
    System.Type  GetType()
Namespace : MyNamespace.Helpers, Type : FileHelper
    Void  WriteToFile(String,String,String,String)
    Void  UpdateFile(String,String,Boolean)

Here, you might [1] change the BindingFlags as per your need [2] change format of the log string (details/methodDetails) [3] format collection/generic types as you need

Edit
If you want to display without the return type, simply format it like

var methodDetails = string.Format("{1}({2})", 
                                  method.Name, 
                                  string.Join(",", method.GetParameters().Select(p => p.ParameterType.Name)));

Upvotes: 4

Nitin
Nitin

Reputation: 107

For public method

 MethodInfo[] myArrayMethodInfo = type.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly);

For Non-Public Methods

 MethodInfo[] myArrayMethodInfo1 = type.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly);

and then extract information using

 MethodInfo[] myArrayMethodInfo

// Display information for all methods.
for(int i=0;i<myArrayMethodInfo.Length;i++)
{
    MethodInfo myMethodInfo = (MethodInfo)myArrayMethodInfo[i];
    Console.WriteLine("\nThe name of the method is {0}.",myMethodInfo.Name);

    ParameterInfo[] pars = myMethodInfo.GetParameters();
    foreach (ParameterInfo p in pars) 
    {
        Console.WriteLine(p.ParameterType);
    }
}

Upvotes: 3

Related Questions