{"id":1697,"date":"2012-08-03T07:58:51","date_gmt":"2012-08-03T12:58:51","guid":{"rendered":"http:\/\/www.wiredprairie.us\/blog\/?p=1697"},"modified":"2012-08-04T10:30:30","modified_gmt":"2012-08-04T15:30:30","slug":"lovin-net-4-5s-callermembernameattribute","status":"publish","type":"post","link":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1697","title":{"rendered":"Lovin’ .NET 4.5’s CallerMemberNameAttribute"},"content":{"rendered":"

Check this out:<\/p>\n

http:\/\/msdn.microsoft.com\/en-us\/library\/system.runtime.compilerservices.callermembernameattribute(v=vs.110).aspx<\/a><\/p>\n

Hate the INotifyPropertyChanged pattern in .NET, and especially the syntax of needing to pass the name of the property being changed (via a string or an Expression or \u2026)?<\/p>\n

There\u2019s finally a built-in solution in .NET 4.5!<\/p>\n

private string <\/span>_testValue;\n<\/span>public string <\/span>TestValue\n{\n    <\/span>get <\/span>{ <\/span>return <\/span>_testValue; }\n    <\/span>set <\/span>{\n        <\/span>if <\/span>(_testValue != <\/span>value<\/span>)\n        {\n            _testValue = <\/span>value<\/span>;\n            RaisePropertyChanged();\n        }            \n    }\n}\n\n<\/span>protected void <\/span>RaisePropertyChanged([<\/span>CallerMemberName<\/span>] <\/span>String <\/span>propertyName = <\/span>null<\/span>)\n{\n    <\/span>var <\/span>eventHandler = <\/span>this<\/span>.PropertyChanged;\n    <\/span>if <\/span>(eventHandler != <\/span>null<\/span>)\n    {\n        eventHandler(<\/span>this<\/span>, <\/span>new <\/span>PropertyChangedEventArgs<\/span>(propertyName));\n    }\n}<\/span><\/pre>\n

A new attribute you can apply to optional parameters which emits the name of the Caller! So in the example above, it\u2019s \u201cTestValue.\u201d<\/p>\n

Rock on! <\/p>\n","protected":false},"excerpt":{"rendered":"

Check this out: http:\/\/msdn.microsoft.com\/en-us\/library\/system.runtime.compilerservices.callermembernameattribute(v=vs.110).aspx Hate the INotifyPropertyChanged pattern in .NET, and especially the syntax of needing to pass the name of the property being changed (via a string or an Expression or \u2026)? There\u2019s finally a built-in solution in .NET 4.5! private string _testValue; public string TestValue { get { return _testValue; } set { […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[4],"tags":[55,62],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pd5QIe-rn","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1524,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1524","url_meta":{"origin":1697,"position":0},"title":"Alternative to ApplicationSettings in .NET","date":"February 1, 2012","format":false,"excerpt":"After dealing with lost settings, an unclear upgrade path, and my own confusion surrounding the magic of Settings in a .NET client application, I decided to build my own. You\u2019re probably familiar with this UI in Visual Studio. It hasn\u2019t changed much since it was first created: A list of\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2012\/02\/image.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":448,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/448","url_meta":{"origin":1697,"position":1},"title":"Silverlight 2.0 — Adjusting UI when Animation completes","date":"August 1, 2008","format":false,"excerpt":"I've discussed how to locate the UIElement associated with an animation's Completed event a while back for WPF. Here's an update for how I do the same thing in Silverlight 2.0. The technique could be used in WPF with a few minor tweaks. I like this method rather than relying\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1113,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1113","url_meta":{"origin":1697,"position":2},"title":"How to bind ListBoxItem.IsSelected to a bound item in Silverlight 4.0","date":"July 26, 2010","format":false,"excerpt":"This technique requires the Blend 4.0 SDK (which is included in Blend 4.0 and is also available as a free download. Someone on StackOverflow asked how to bind a collection of items to a ListBox in Silverlight where the IsSelected property of the ListBoxItem is bound to an IsSelected property\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2010\/07\/image2.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1216,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1216","url_meta":{"origin":1697,"position":3},"title":"Sending an email using SMTP in .NET 4.0","date":"July 13, 2011","format":false,"excerpt":"I had need of sending embedded images within an e-mail. .NET has had a few handy classes for sending an email using SMTP for a few versions. While there were a few examples floating around the internet, none were as clean and easy to follow as I expected. So, I\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1705,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1705","url_meta":{"origin":1697,"position":4},"title":"WinRT\/Xaml\/AKA Metro DataTemplate selection based on Data Types","date":"August 20, 2012","format":false,"excerpt":"You may have noticed that WinRT does not have automatic resolution of a DataTemplate based on the data type of object added to an ItemsControl. While unfortunate as this behavior is quite handy, it\u2019s not too difficult to replicate the functionality using a DataTemplateSelector. WPF for example, could do something\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":568,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/568","url_meta":{"origin":1697,"position":5},"title":"Data Binding and Tooltips in Silverlight","date":"September 19, 2008","format":false,"excerpt":"Have you ever wanted to databind a tooltip in Silverlight (or WPF for that matter), and found that the DataContext isn't available for tooltips (the datacontext is null)? It's very annoying. Tooltips, unfortunately, aren't connected to their parents in anyway when they're created, so they loose the ability to connect\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1697"}],"collection":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/comments?post=1697"}],"version-history":[{"count":2,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1697\/revisions"}],"predecessor-version":[{"id":1699,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1697\/revisions\/1699"}],"wp:attachment":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/media?parent=1697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/categories?post=1697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/tags?post=1697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}