4

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.

1
  • Type's FullName contains the namespace. You only need to split the FullName apart to the namespace. To get methods, try Type.GetMethods(BindingFlags) Commented Dec 30, 2015 at 6:03

3 Answers 3

4

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)));
Sign up to request clarification or add additional context in comments.

Comments

3

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);
    }
}

2 Comments

MethodInfo will contain all methods for particular type. its up to you what information you want to extract.
@ManoRoshan see my edited answer, that should help you. Additionally, you can tag people like @<username> to grab their attention.
1

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

2 Comments

Thanks Arghya and Alberto. @Arghya how to get the overloaded methods also in the above codes
@ManoRoshan this code already get overloaded methods, se my output test

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.