Click here to Skip to main content
Click here to Skip to main content

Naming Properties in MVVM

By , 20 Jul 2011
 
PropertyChanged(this, new PropertyChangedEventArgs(MethodBase.GetCurrentMethod().Name.Substring(4)))

It is slower than your method, but it might not be a big issue and it is much easier to manage from a code perspective.

Not sure it is advisable, but definitely simpler.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Daniel Gidman
Architect Stackify
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: Unfortunatly not - I thought Substring(...) would cut the en...memberDoc Lobster21 Jul '11 - 19:52 
Unfortunatly not - I thought Substring(...) would cut the end of the string, not its start. See Daniels reply below
GeneralAbout "it doesn't really remove maintenance concerns", you'r...memberSiavash Mortazavi22 Jul '11 - 17:26 
About "it doesn't really remove maintenance concerns", you're completely right Daniel Wink | ;)
It's not an ultimate solution, but maybe better than nothing Smile | :)
Thanks for posting linq expression tip.
GeneralMagic number (4) is account for the prefix "set " || "get "...memberDaniel Gidman21 Jul '11 - 3:44 
Magic number (4) is account for the prefix "set " || "get " due to it being inside a property asscessor.
 
So, not really magic at all, just due to location of call.
 
As to simplifying for outside calling of a the PropertyChanged... Use expressions.
 
linq-expressions-and-extension-methods-to-get-property-name @ stackoverflow.com
 
I think the big thing that I don't like very much about your solution here is that it doesn't really remove maintenance concerns, it minimizes them some and centralizes them, but doesn't remove them completely.
GeneralYou're right Doc ;) Thanks for your tip :)memberSiavash Mortazavi21 Jul '11 - 3:03 
You're right Doc Wink | ;)
Thanks for your tip Smile | :)
GeneralAlso you would have to use a magic number (4) so you still h...memberDoc Lobster21 Jul '11 - 0:44 
Also you would have to use a magic number (4) so you still have the risk of setting the string range to an incorrect value (especially when using copy/paste). This isn't much better then the risk of a typo during entering the string.
GeneralRe: You're right Doc ;) Thanks for your tip :)memberSiavash Mortazavi21 Jul '11 - 3:04 
You're right Doc Wink | ;)
Thanks for your tip Smile | :)
GeneralHi, As you said, yourself, Reflection in to slow; maybe it's...memberSiavash Mortazavi20 Jul '11 - 18:08 
Hi,
As you said, yourself, Reflection in to slow; maybe it's not a wise idea to use it in big projects.
And there is another problem with this method: in your method we can't refactor the process of raising the event in a separate method (e.g. RaisePropertyChanged), because if so, the reflection will return the name of the method, itself.
 
I haven't ever taught of using reflection this way, thanks a lot for sharing your knowledge Wink | ;)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 20 Jul 2011
Article Copyright 2011 by Daniel Gidman
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid