CR的代码文本

all for learning about the world
  订阅 订阅  :: 管理

C#中的属性太邪恶了

Posted on 2014-05-29 13:37  mumuliang  阅读(1066)  评论(0编辑  收藏  举报

好懒,啥都不想写了。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.