VS2010调试Framework源代码的大门

本文介绍如何在Visual Studio 2010中启用.NET Framework源代码级调试功能,通过设置可以轻松地追踪并理解.NET Framework内部实现细节,如IEnumerable<int>的Sum扩展方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有一种提高自己编程能力的好办法,就是看看.net framework的源码是如何写的?我们在追踪bug的时候,也往往需要追踪到.net framework的源码中去。按照如下方法设置vs2010,即可追踪到.net framwork的源代码中去。

点击浏览下一页

点击浏览下一页

可以看出,我将pdb文件放在了d:\msSource\MicrosoftPublicSymbols目录下面,这样在调试的时候,vs会自动去该目录下面找pdb文件。

我们可以看看这个目录下的文件

点击浏览下一页

调试源码后,我们会找到一些平时不怎么写的东西。比如我追踪 IEnumerable<int>的扩展方法Sum,可以看到

如下的代码:

public static int Sum(this IEnumerable<int> source) {
if (source == null) throw Error.ArgumentNull("source");
int sum = 0;
checked {
foreach (int v in source) sum += v;
}
return sum;
}

这样,我们就学会了一个扩展方法的写法,何乐而不为呢?

编程的快乐,往往在乎一念之间。

PS:代码是从微软服务器上下载的,所以设置完毕后,第一次下载过程可能有点长。

更详细的文档如下:

Using the Microsoft Symbol Server to obtain symbol debugging information is now much easier in VS 2010. Microsoft gives you access to their internet symbol server that contains symbol files for most of the .NET framework including the recently announced availability of MVC 2 Symbols.

SETUP

In VS 2010 RTM, go to Tools –> Options –> Debugging –> General. Check “Enable .NET Framework source stepping”
点击浏览下一页
We get the following dialog box

点击浏览下一页
This automatically disables “Enable My Code”
点击浏览下一页
Go to Debugging –> Symbols and Check “Microsoft Symbol Servers”. You can selectively exclude modules if you want to.
点击浏览下一页
You will get a warning dialog like so:
点击浏览下一页
Hitting OK will start the download process
点击浏览下一页
The setup is complete. You are now ready to start debugging!


DEBUGGING

Add a break point to your application and run the application in debug mode (F5 shortcut for me). Go to your call stack when you hit the break point. Right click on a frame that is grayed out.
点击浏览下一页
Select “Load Symbols from” “Microsoft Symbol Servers”. VS will begin a one time download of that assembly. This assembly will be cached locally so you don’t have to wait for the download the next time you debug the app.
点击浏览下一页
We get a one time license agreement dialog box
点击浏览下一页

You might see an error like the one below regarding different encoding (hopefully will be fixed).
点击浏览下一页
Assemblies for which the symbols have been loaded are no longer grayed out. Double clicking on any entry in the call stack should now directly take you to the source code for that assembly.

点击浏览下一页

点击浏览下一页

AFAIK, not all symbols are available on the MS symbol server. In cases like that you will see a tab like the one below and be given the option to “Show Disassembly”.

点击浏览下一页


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值