Hello everyone, I am a wolf at the end of the desert.
本方首发于Dotnet9,介绍使用dnSpy调试第三方.NET库源码,行文目录:
- Install dnSpy
- Write sample programs
- Debugging sample program
- Debugging native methods for. NET libraries
- summary
1. Install dnSpy
dnSpy is a powerful decompilation tool for. NET programs. It can decompile. NET programs instead of library document functions. Code loss or damage can be directly recovered, so it can debug programs instantly without any source code, and even modify programs!
GitHub has a binary executable program download address and source code that can be compiled by itself. This article uses the former. The GitHub address is: github.com/dnSpy/dnSpy

2. Write sample programs
The example is a desktop program that enters numbers and echoes whether the entered numbers are odd or even on the right side:

The example code is relatively simple. Read all screenshots of interface binding and ViewModel relationship:

奇偶判断由类TestTool的TellMeOddEven方法返回,再回看回显,咦,0是奇数?1是偶数?
TestTool类是其他库定义,我假装你没有源码哈,虽然你有:

Class is specifically defined as follows:
namespace TestDll;
public class TestTool
{
public string TellMeOddEven(int number)
{
if (number % 2 == 1)
{
return $"{number}是偶数";
}
return $"{number}是奇数";
}
}
3. Debugging sample program
打开dnSpy,将主程序引用的TestDll拖入:

You can see the decompiled code:

The method definitions obtained from decompiled may be different from third-party source code. The following are some factors that may lead to different decompiled results:
Compiler optimization: Different versions of the compiler may optimize the code differently, such as using different algorithms, data structures, or code rearrangements. These optimizations may result in different decompiled code structures and sequences. The examples in this article are developed using. NET 8, and libraries compiled by the. NET Framework may be decompiled almost identical to the source code.
Decompilation tool updates: dnSpy itself is constantly updated to accommodate new. NET versions and compiler features. These updates may change decompile algorithms and strategies, resulting in inconsistent decompile results for different versions of dnSpy.
The code is simple. Compare the source code with the decompiled code. Divide the integer input parameter by 2 and take the remainder. If it is equal to 1, it is judged to be even, otherwise it is odd. Of course, this is wrong. If the code logic is complex, you can use dnSpy to debug it.
Running the test program, giving method interruptions in dnSpy, and attaching the test program to the debugging menu is similar to the operation in VS:

4. Methods for debugging. NET libraries
The above methods for debugging the sample program can be used in other third-party. NET libraries, but what about. NET's own library methods?
The method is similar. Find the corresponding class and method of the. NET library, run the target program, and then interrupt the point. NET library methods are found here: Click [File]][Open from GAC]=] to search for the target library, double-click the library, and then find the target method. The debugging steps are the same:

5. summary
- For technical exchanges and groups, please add webmaster WeChat: codewf
- 文中示例代码:MultiVersionLibrary
dnSpy很强大的,还能直接监视第三方代码的变量、修改值等,就和你使用VS开发自己的程序一样,了解更多用法还请查看文章开头给的链接https://github.com/dnSpy/dnSpy, 这篇大佬的文章也不错,建议看看:《神器如 dnSpy,无需源码也能修改 .NET 程序》。
** By the way, the parity number judgment in the example program is incorrect, and I don't have the code. What should I do if I want to correct it? **
解决这个问题,上面大佬的文章您可以拜读了,下一篇站长继续讲解第三方库拦截,能实现不修改第三方库达到修改方法逻辑和返回结果的效果,可以提前预习快学会这个技能-.NET API拦截技法,当然下一篇会有新知识点:非公有类非公有方法拦截技法。
Concludes this article with two animations of dnSpy debugging third-party libraries in the original warehouse:

