好懒,啥都不想写了。C#的属性伤透了我的心。只能相信记忆力和想象力能让我下次翻到这篇日志时能瞬间想到我们在谈瓦特。

http://stackoverflow.com/questions/1224270/when-is-my-c-sharp-property-initialized

http://blogs.msdn.com/b/jmstall/archive/2007/03/13/func-eval-abort-is-evil.aspx

http://blogs.msdn.com/b/jmstall/archive/2005/03/23/400794.aspx

http://blogs.msdn.com/b/jmstall/archive/2006/03/05/writing-funceval-in-a-debugger.aspx

另外马克一句嗷叔的指教

“断点的原理就是把第一个字节改成0xcc”

前因后果,以及想让它不那么邪恶的办法在此:http://flylib.com/books/en/4.442.1.38/1/

以下是引用。

Format Specifiers and Property Evaluation

If you're coming from a C++ background, you'll remember the, code values, called format specifiers , that you could use to influence exactly how you wanted the data to be displayed. If you're a C# developer, some of those format specifiers have reappeared to make your life easier. For example, if you have a variable i and you type i,h in the Watch window, the number will be displayed as hexadecimal instead of the decimal default. By the way, you can force the default Watch window display to hexadecimal by right-clicking in the Watch window and selecting Hexadecimal Display from the shortcut menu. If you have set the default display to Hexadecimal, you can specify the ,d format specifier to display the data as decimal. The ,raw formatting code is used when DebuggerTypeProxyAttributes have been applied to a class, which I will explain more in detail in the "Expanding Your Own Types" section later in the chapter.

The ,nq formatting code is especially handy because to make the string more readable, it will display a string without the escaped quotes. For example, if you have the XML element, the default view of the inner text would show the string like the following: " < book genre =\ " novel \" ISBN=\ "1-861001-57-5\" misc=\ "sale item\"/> ." After you add the ,nq on the end of the string property, the display changes to " < book genre="novel" ISBN="1-861001-57-5" misc="sale item"/> " that is much easier to read. Personally, I wish the Watch window would default to the non-quoted string display.

If you have Just My Code enabled, and you want to see the private members of an object, you'd specify the ,hidden code after the variable name. Note that the Visual Studio documentation is incorrect and calls this formatting code ,private.

The last format specifier I want to mention is ,ac , which exists to force expression evaluation. The documentation lists this format specifier with a capital A, which is incorrect. In order to talk about the ,ac specifier, I have to talk about implicit property evaluation. Whereas the format specifiers are annoyingly C# only, implicit property evaluation applies to all .NET languages, and it's important that you understand the ramifications .

For .NET developers, it's second nature to see the values of properties on objects in any of the Watch window family of windows. What the debugger's doing is actually calling the property's get method in the context of debuggee. In most cases, there's no trouble at all having the debugger help you out that way. Occasionally, a property evaluation inside the debugger can cause a side effect that causes a great deal of trouble. If there is a property getter that makes a database query that changes data, the property evaluation in the debugger will certainly be changing the state of your application. I know no one reading this would ever have a property getter changing data, but you can't always force your coworkers to follow best practices.

If there's any hint that the debugger property evaluation will cause you problems, you'll want to turn it off by going to the Options dialog box, clicking the Debugging node, General property page, and clearing the Enable Property Evaluation and Other Implicit Function Calls check box. You can toggle this option all you want during a debugging session to control the debugger behavior. Although I would prefer a menu command or a toolbar button to control the option, it's not too onerous to toggle it. You'll know that implicit property evaluation is turned off when you start debugging because you'll see "Property evaluation is disabled in debugger windows. Check your settings in Tools.Options.Debugging.General." for Visual Basic and "Implicit function evaluation is turned off by user ." for C#. Interestingly, turning off property evaluation does not disable calling properties and methods on conditional breakpoint modifiers.

In previous versions of the debugger, once you turned off implicit property evaluation, there was no way to see those property values without turning it fully back on. Fortunately, the Visual Studio 2005 debugger is much more forgiving , and you have various ways to evaluate specific properties. For all languages, the easiest thing to do is to click the green refresh button on the right side of the Value column. Figure 5-7 shows the Watch window with the refresh buttons called out. As you can see, you can click the refresh button for the entire object so all properties in the class are shown, or you can do just the one property you're interested in seeing.

Figure 5-7. Controlling debugger-implicit property evaluation

If you're debugging a C# application, you can add the ,ac format specifier to that property or the class as a whole. This will tell the debugger that you always want to evaluate the specific property or all properties on the object every time you stop the debugger.

One interesting item I noticed with implicit property evaluation turned off is that when you add an object to the Watch window, the Watch window will evaluate all the properties on that object so you can see their values. It's intuitively what you would expect, but if you drag and drop that one object with a nasty-side-effect property, you've just caused it to execute. Just keep that in mind when you are adding scary objects to the Watch window. What you'll want to do instead is stop in another scope where the variable is not defined and add the value you want to watch there. That way it's in the Watch window when you stop, so the debugger won't evaluate the properties when it stops in the real location.

Before I jump to my favorite new feature in the Watch window, I wanted to mention two pseudo variables you can display in the Watch window. If you've turned off the Exception Assistant, the window that shows the unhandled exception, the C#-only $exception pseudo variable will be automatically added to the Locals window so you can see the unhandled exception. The new pseudo value, $user , will display the account information for the current thread and indicate if it's impersonating.

C#中的属性太邪恶了的更多相关文章

  1. java中静态属性和和静态方法的继承问题 以及多态的实质

    首先结论是:java中静态属性和和静态方法可以被继承,但是没有被重写(overwrite)而是被隐藏. 静态方法和属性是属于类的,调用的时候直接通过类名.方法名完成的,不需继承机制就可以调用如果子类里 ...

  2. Web API 2中的属性路由

    Web API 2中的属性路由 前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.ht ...

  3. FusionCharts饼图中label值太长怎么解决

    FusionCharts饼图中label值太长怎么解决 1.使用hoverText属性 <?xml version="1.0" encoding="UTF-8&qu ...

  4. CSS中 Zoom属性

    CSS中 Zoom属性 其实Zoom属性是IE浏览器的专有属性,Firefox等浏览器不支撑.它可以设置或检索对象的缩放比例.除此之外,它还有其他一些小感化,比如触发ie的hasLayout属性,清除 ...

  5. OpenGLES 与 WebGL 中顶点属性的组织格式的误解 - 一个不好笑的笑话

    版权声明:本文为博主原创文章,未经博主同意不得转载.转载联系 QQ 30952589.加好友请注明来意. https://blog.csdn.net/sleks/article/details/289 ...

  6. 你所不知道的库存超限做法 服务器一般达到多少qps比较好[转] JAVA格物致知基础篇:你所不知道的返回码 深入了解EntityFramework Core 2.1延迟加载(Lazy Loading) EntityFramework 6.x和EntityFramework Core关系映射中导航属性必须是public? 藏在正则表达式里的陷阱 两道面试题,带你解析Java类加载机制

    你所不知道的库存超限做法 在互联网企业中,限购的做法,多种多样,有的别出心裁,有的因循守旧,但是种种做法皆想达到的目的,无外乎几种,商品卖的完,系统抗的住,库存不超限.虽然短短数语,却有着说不完,道不 ...

  7. css3中content属性的应用

    可以使用css3中content功能为html元素增减内容.content需要配合 E:before和E:after使用. 废话少说,看代码和效果说明: 第一种: css代码: #div1:befor ...

  8. 黑马vue---16、vue中通过属性绑定为元素设置class类样式

    黑马vue---16.vue中通过属性绑定为元素设置class类样式 一.总结 一句话总结: 这里就是为元素绑定class样式,和后面的style样式区别一下 vue中class样式绑定方式的相对于原 ...

  9. C#中的属性-Property

    C#的属性一直都有用,但具体了解的不是很深,而且一些注意事项也没有太在意过,糊里糊涂的用着.这两天看了C#的书专门学习了一下属性,这才知道,原来属性也有这么多东西~ ~今天记录一下,算是对学习的一个检 ...

随机推荐

  1. SQL联合查询(内联、左联、右联、全联)的语法

    联合查询效率较高,举例子来说明联合查询:内联inner join .左联left outer join .右联right outer join .全联full outer join 的好处及用法. 联 ...

  2. cocos2dx-3.9 集成admob

    Part 1: 安装GoogleMobileAds framework (即admob) 1. 安装Cocoapods,否则解决依赖关系和配置真的会把人不累死也得烦死 sudo gem install ...

  3. jquery 甘特图开发指南

    JQuery.Gantt是一个开源的基于JQuery库的用于实现甘特图效果的可扩展功能的JS组件库. <link rel="stylesheet" href="cs ...

  4. VS属性页的目录类型

    常用的三个: 1.可执行目录 :在其中搜索可执行文件的目录. 对应于 PATH 环境变量,即为.dll的目录. 2.包含目录    :在其中搜索源代码中所引用的包含文件的目录. 对应于 INCLUDE ...

  5. POJ 3041 -- 二分图匹配

    题意:有个N*N的网格,有一部分格子里有陨石,小明有很牛逼的武器,打一枪后一行或一列的陨石就没了,给出陨石分布,求最小打炮数. 分析:其实就是Konig定理.记最小打炮数为m,在网格里你最多可以找出M ...

  6. 深入理解javascript中实现面向对象编程方法

    介绍Javascript中面向对象编程思想之前,需要对以下几个概念有了解: 1. 浅拷贝和深拷贝:程序在运行过程中使用的变量有在栈上的变量和在堆上的变量,在对象或者变量的赋值操作过程中,大多数情况先是 ...

  7. linux磁盘存储命令 磁盘存储命令

    硬盘空间是一个有限的资源, 硬盘空间是一个有限的资源,用户用下面的命令 可 以随时了解当前硬盘空间的使用情况. 以随时了解当前硬盘空间的使用情况.   ? du,df命令 查看磁盘空间状况的操作 , ...

  8. linux学习笔记1-ubuntu的安装与基本设置

    ubuntu的安装 (1) 要装ubuntu首先当然得捣鼓一台机器,当然你用虚拟机也是可以的.本人的这台DELL品牌机是从某师兄脚底下挖出来的,大概很多年没人用了,内存1.25G|||- -,但是硬盘 ...

  9. Python 基礎 - 字符串常用操作

    字符串常用操作 今天就介紹一下常用的字符串操作,都是以 Python3撰寫的 首字母變大寫 #!/usr/bin/env python3 # -*- coding:utf-8 -*- name = & ...

  10. CentOS 7 安装 MySQL Database

    CentOS 7 安装 MySQL Database 1. 现在安装包,MySQL的安装包被分成了社区版和企业版,而本文将记录社区版本MySQL安装过程,下载MySQL版本如下: mysql-5.7. ...