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#的书专门学习了一下属性,这才知道,原来属性也有这么多东西~ ~今天记录一下,算是对学习的一个检 ...
随机推荐
- Python学习路程day20
本节内容: 项目:开发一个简单的BBS论坛 需求: 整体参考“抽屉新热榜” + “虎嗅网” 实现不同论坛版块 帖子列表展示 帖子评论数.点赞数展示 在线用户展示 允许登录用户发贴.评论.点赞 允许上传 ...
- Apache Server Status主机状态查看
10月30日,国外安全研究人员发现由于对apache设置不严,导致服务器状态暴露于公网.本来apache有一个叫server-status 的功能,为方便管理员检查服务器运行状态的.它是一个HTML页 ...
- 直接请求json文件爬取天眼查企业信息(未解决验证码问题)——python3实现
几个月前...省略一堆剧情...直接请求json文件爬取企业信息未成功,在知乎提问后,得到解决,有大佬说带上全部headers和cookie是可以的,我就又去试了下,果然可以(之前自己试的时候不行,没 ...
- SqlServer性能优化 即席查询(十三)
执行计划,查询类别: 1.即席查询 2.预定义查询 select c.EnglishProductCategoryName,p.EnglishProductName,p.Color,p.Siz ...
- c# DllImport 找不到指定模块
两年前的一个项目,基于身份证阅读器的开发,之前都是在公司电脑上开发维护等,今天有需要用到自己的笔记本,只有vs2008和mysql5.5,以为足够,兴致勃勃的拿到客户那里现场解决问题,F5运行程序,程 ...
- mysql 信息查询
show tables information 除了查询数据,管理数据之外,还有其他方面的信息方便和约束着 -- show information about characterset,查询mys ...
- Cocos2d-x win7 + vs2010 配置图文详解
Cocos2d-x win7 + vs2010 配置图文详解 下载最新版的cocos2d-x.打开浏览器,输入cocos2d-x.org,然后选择Download,本教程写作时最新版本为cocos2d ...
- xpath轴的正确使用姿势
网上看了许多关于轴的介绍,只介绍了语法,而没有明说具体实际中该怎么使用,百思不得其解. 背景--python中使用xpath: ----------------------------------- ...
- sql server 表中的编号自增
http://jingyan.baidu.com/article/fec4bce244f902f2608d8b7a.html
- Ibator的配置和使用
1. Ibator介绍 Ibator是iBATIS的代码发生器,其原名叫abator,后来更名为Ibator,同时代码结构也做了相应的一些修改,所以两者的配置也有所不同.Ibator可以生成一 ...