funkyxym
funkyxym

Reputation: 61

How to use reflection in C# to extract class and method information from executable file(.exe)

I have .exe file generated via visual studio. Now, in a reversed way, I need to get classes info (namespace and class name) and method info (access modifier, return type, and input parameters) from .exe file.

Actually I could get those info from dll files, but no idea from executable files.

Could anyone give me a simple code demo? Thanks in advance!!

Upvotes: 2

Views: 5029

Answers (2)

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34199

Actually I could get those info from dll files, but no idea from executable files.

There is no difference between dll and exe files. They are both .NET assemblies. Just use the same code / approach, which you use for DLLs.

You need to use Assembly.LoadFile to load an Assembly from EXE or DLL file:

var assembly = Assembly.LoadFile("C:\path_to_your_exe\YourExe.exe");

foreach (var type in assembly.GetTypes())
{
    Console.WriteLine($"Class {type.Name}:");
    Console.WriteLine($"  Namespace: {type.Namespace}");
    Console.WriteLine($"  Full name: {type.FullName}");

    Console.WriteLine($"  Methods:");
    foreach (var methodInfo in type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
    {
        Console.WriteLine($"    Method {methodInfo.Name}");

        if (methodInfo.IsPublic)
            Console.WriteLine($"      Public");

        if (methodInfo.IsFamily)
            Console.WriteLine($"      Protected");

        if (methodInfo.IsAssembly)
            Console.WriteLine($"      Internal");

        if (methodInfo.IsPrivate)
            Console.WriteLine($"      Private");

        Console.WriteLine($"      ReturnType {methodInfo.ReturnType}");
        Console.WriteLine($"      Arguments {string.Join(", ", methodInfo.GetParameters().Select(x => x.ParameterType))}");
    }
}

Upvotes: 1

KARAN
KARAN

Reputation: 1041

Download ILSPY software.

step 1 => Unzip the Folder.
step 2 => now you should be able to seen ilspy application open that file.
step 3 => after open ilspy application you should go into => FILE Menu => Open.
step 4 => Browse your .exe file and open you should be able to seen whole code.

Thank you.

Upvotes: 3

Related Questions