惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
C
Cisco Blogs
A
Arctic Wolf
月光博客
月光博客
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
量子位
小众软件
小众软件
Latest news
Latest news
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
N
Netflix TechBlog - Medium
K
Kaspersky official blog
人人都是产品经理
人人都是产品经理
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
Y
Y Combinator Blog
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Schneier on Security
D
Docker
Scott Helme
Scott Helme
MyScale Blog
MyScale Blog
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
GbyAI
GbyAI
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tenable Blog
B
Blog
Know Your Adversary
Know Your Adversary
IT之家
IT之家

博客园 - bartholomew

new作为修饰符时的使用,以及接口的显式实现 关键词:应用程序扩展,通配符应用程序映射 Silverlight 2 Beta 1在Firefox下显示时的一点小问题~ 关于SQL Server 2005的版本号 使用动态SQL的一点小技巧 通过本地IIS SMTP服务器发送邮件时提示“邮箱不可用”的解决办法 使用System.Net.Mail.SmtpClient发送邮件时出现的乱码问题 写代码的心情 在XP上安装SQL Server 2000、Visual studio .net 2003、Visual studio 2005、SQL Server 2005…… XPS M1210到了~~~~ 在dell的网上订购了XPS M1210,耐心等待中…… 对PropertyGrid控件中PropertyValueChanged事件的探讨 关于邮件群发 关于Dotnet中的线程池 Dotnet中强行关闭多线程应用程序的所有线程 工作之余,自省~ 创建某控件的线程之外的其他线程试图调用该控件引发的问题 古怪的ConfigurationManager类 多线程编程中Join与WaitOne的区别
Visual Studio也有调试禁区?!
bartholomew · 2008-05-15 · via 博客园 - bartholomew

今天在调试同事的一段代码时,发现有一段代码调试时怎么也进不去,难道遇到了Visual Studio的调试禁区?

简化后的代码如下:

    class Program
    
{
        
static void Main(string[] args)
        
{
            SimpleClass simpleClass 
= new SimpleClass();
            
string myString = simpleClass.OneField;
        }

    }


    
public class SimpleClass
    
{
        
private string _oneField = string.Empty;

        
public string OneField
        
{
            
get
            
{
                
if (_oneField.Length == 0)
                
{
                    _oneField 
= GetMyString();
                }


                
return _oneField;
            }

        }


        
private string GetMyString()
        
{
            
return "just for test~";
        }

    }

无法调试的代码即“_oneField = GetMyString();”这一行。

大家可以自行调试,会发现是因为_oneField这个变量总是被莫名其妙的赋上值了,但明明我们还没有给它赋值……

和同事讨论过后,还是没有得出明确的结论,只是推测可能是因为Visual Studio在调试的过程中,自动扫描对象中的属性,然后自行运行属性的get操作~

不知道大家有没有遇到这种情况,虽然最终通过把OneField属性改写成函数解决了这个问题,但还是有些不爽,难道在Visual Studio就真的没有办法调试如上代码?

更新结论:
经过大家的反馈,我重复试了多次,总结如下,欢迎大家反馈意见——
要重现我以上的问题,可按如下步骤操作:在“SimpleClass simpleClass = new SimpleClass();”这一行加上断点,然后逐行调试,并且(请注意,这一点很重要,相信很多兄弟和我的调试结果不一样,可能就是因为这个原因),满足以下任一条件:
1、在调试过程中打开了Autos窗口
2、将simpleClass.OneField添加到了Watch中
3、运行到“simpleClass.SecondField = "me";”这一行时,鼠标移到对象“simpleClass”上,在弹出的信息显示层中点击“+”,查看一下“simpleClass”对象的各属性值,嗯,这个操作的作用和打开Autos窗口一样)
那么,最终是无法到达“_oneField = GetMyString();”这一行的;

进一步更新:
本以为以上发现的问题不会影响程序运行的结果,但却意外发现有时也会影响到程序最终运行的结果——

代码如下:

    class Program
    
{
        
static void Main(string[] args)
        
{
            SimpleClass simpleClass 
= new SimpleClass();
            simpleClass.SecondField 
= "me";
            
string myString = simpleClass.OneField;
            Console.WriteLine(myString);

            Console.Read();
        }

    }


    
public class SimpleClass
    
{
        
private string _oneField = string.Empty;
        
private string _secondField = string.Empty;

        
public string OneField
        
{
            
get
            
{
                
if (_oneField.Length == 0)
                
{
                    _oneField 
= GetMyString();
                }


                
return _oneField;
            }

        }


        
public string SecondField
        
{
            
get return _secondField; }
            
set { _secondField = value; }
        }


        
private string GetMyString()
        
{
            
return "SecondField:" + SecondField;
        }

    }

以上这段代码正确的输出应该是:“SecondField:me”。

但如果我们在调试中这样做了的话。。。
嗯,还是从“

SimpleClass simpleClass = new SimpleClass();”这一行开始逐行调试,运行到“simpleClass.SecondField = "me";”这一行时(注意,一定要在运行“simpleClass.SecondField = "me";”这一语句之前进行以下操作),鼠标移到对象“simpleClass”上,在弹出的信息显示层中点击“+”,查看一下“simpleClass”对象的各属性值,然后,OK,F5吧,你会发现,最后的输出变成了:“SecondField:”!!!

到目前为止最好的解决办法:
Options -> Debugging -> General -> Enabled property evaluation and other implicit function calls
去掉这个选项的选中状态!
然后,以上所有的问题都得以解决——这个世界,终于清静了。。。。。。

感谢Q.yuhen兄弟!