C#中的属性太邪恶了
好懒,啥都不想写了。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#中的属性太邪恶了的更多相关文章
- java中静态属性和和静态方法的继承问题 以及多态的实质
首先结论是:java中静态属性和和静态方法可以被继承,但是没有被重写(overwrite)而是被隐藏. 静态方法和属性是属于类的,调用的时候直接通过类名.方法名完成的,不需继承机制就可以调用如果子类里 ...
- Web API 2中的属性路由
Web API 2中的属性路由 前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.ht ...
- FusionCharts饼图中label值太长怎么解决
FusionCharts饼图中label值太长怎么解决 1.使用hoverText属性 <?xml version="1.0" encoding="UTF-8&qu ...
- CSS中 Zoom属性
CSS中 Zoom属性 其实Zoom属性是IE浏览器的专有属性,Firefox等浏览器不支撑.它可以设置或检索对象的缩放比例.除此之外,它还有其他一些小感化,比如触发ie的hasLayout属性,清除 ...
- OpenGLES 与 WebGL 中顶点属性的组织格式的误解 - 一个不好笑的笑话
版权声明:本文为博主原创文章,未经博主同意不得转载.转载联系 QQ 30952589.加好友请注明来意. https://blog.csdn.net/sleks/article/details/289 ...
- 你所不知道的库存超限做法 服务器一般达到多少qps比较好[转] JAVA格物致知基础篇:你所不知道的返回码 深入了解EntityFramework Core 2.1延迟加载(Lazy Loading) EntityFramework 6.x和EntityFramework Core关系映射中导航属性必须是public? 藏在正则表达式里的陷阱 两道面试题,带你解析Java类加载机制
你所不知道的库存超限做法 在互联网企业中,限购的做法,多种多样,有的别出心裁,有的因循守旧,但是种种做法皆想达到的目的,无外乎几种,商品卖的完,系统抗的住,库存不超限.虽然短短数语,却有着说不完,道不 ...
- css3中content属性的应用
可以使用css3中content功能为html元素增减内容.content需要配合 E:before和E:after使用. 废话少说,看代码和效果说明: 第一种: css代码: #div1:befor ...
- 黑马vue---16、vue中通过属性绑定为元素设置class类样式
黑马vue---16.vue中通过属性绑定为元素设置class类样式 一.总结 一句话总结: 这里就是为元素绑定class样式,和后面的style样式区别一下 vue中class样式绑定方式的相对于原 ...
- C#中的属性-Property
C#的属性一直都有用,但具体了解的不是很深,而且一些注意事项也没有太在意过,糊里糊涂的用着.这两天看了C#的书专门学习了一下属性,这才知道,原来属性也有这么多东西~ ~今天记录一下,算是对学习的一个检 ...
随机推荐
- Xcode清除缓存、清理多余证书
Xcode清除缓存.清理多余证书 1.删除Xcode中多余的证书provisioning profile 手动删除: Xcode6 provisioning profile path: ~/Libra ...
- oracle length and lengthb
LENGTH──返回以字符为单位的字符串长度. LENGTHB──返回以字节为单位的字符串长度,它和类型定义中的长度是一个概念,比如你定义的varchar2(10)中的10.在不同的数据库,因为字符集 ...
- 运行带cocoa pods 的项目,遇到的问题是找不到文件,解决办法
打开终端,进入项目所在的目录,也就是和Podfile在同一目录下,输入以下命令(由于已经有Podfile,所以不需要再创建Podfile): pod update 过几秒(也许需要十几秒,取决于你的 ...
- CGBitmapContextCreate函数参数详解
函数原型: CGContextRef CGBitmapContextCreate ( void *data, size_t width, size_t height, size_t bitsPerCo ...
- Google play(Android market)账户申请
从09年到现在,Google play账户在国内申请一直都是个问题,根本的原因是国内不支持Google wallet(原Google Checkout) 在国内要获取一个Google play账户,常 ...
- 吐槽THINKPHP5命令行
thinkphp,作为国内开源框架,一直在使用和学习. 但是实在忍不住想要吐槽一下他的开发文档,和 对初学者的不友好,建议刚接触MVC思想的人 还是尽量去使用其他框架当入门. 现在来吐槽一下think ...
- line-height的一点粗浅认识
line-height:有5种不同类型的单位. line-height: normal|inherit(继承)|%(百分比)|20px(像素值)|1.2(倍数); 将行高和字体大小结合: body { ...
- liunx中字符驱动编写的简单模板
下面是关于字符驱动两个程序,主要是说明驱动编写的思想,理解驱动是怎么一步一步被实现的. 驱动的第一个实现程序,是相对于裸机编程的,主要是体会一下驱动编程思想: cdev.h: 所包含的头文件 #ifn ...
- window.onload、DOMContentLoaded和$(document).ready()
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...
- 文本编辑器的使用(ckeditor/ueditor)
1.下载ckeditor或ueditor 2.编写示例代码 注意:使用ueditor时,需要特殊配置图片路径(图片的配置路径在ueditor/php/config.json文件中) 3.结果 (需要源 ...