²»Ê¹ÓÃAOPÒ²¿ÉÒÔ»º½âÕâЩÎÊÌ⣬±ÈÈç¿ÉÒÔдµ¥Ôª²âÊÔ»òÕß·ÀÓùÐÔ±à³Ì£¬ËüÃÇ¿ÉÒÔÑéÖ¤ËùÓн«»á·¢Ë͵ÄÕýȷ֪ͨ¡£ËäÈ»¿ÉÒÔʹÓ÷¢ÉäʹµÃÕâ¼þʼòµ¥Ð©£¬µ«ÊÇ»áDZÔÚ²úÉúºÜ¶à´úÂë¡££¨±ÈÈç¿ÉÒÔʹÓ÷´Éä¿ÉÒÔÑ»·±éÀúÀàµÄËùÓÐÊôÐÔ£¬»ñµÃÊôÐÔÃû³Æ£¬½ø¶øÈ·±£µ±Ê¼þ´¥·¢Ê±ÊôÐÔÃû³ÆʱƥÅäµÄ£©¡£
Èç¹ûʹÓÃÁË.NET 4.5£¬ÄÇô¿ÉÒÔʹÓÃÒ»¸ö½Ð×öCallMemberNameµÄй¤¾ßÀ´´¦ÀíINotifyPropertyChanged,CallMemberNameÊÇÒ»¸öÌØÐÔ£¬¿ÉÒÔʹÓÃËü½«Ò»¸ö²ÎÊýÉèÖóɵ÷ÓõÄÊôÐÔµÄÃû³Æ¡£ÕâÀïÎÒÃÇ¿ÉÒÔʹÓÃËüÀ´¼õÉÙNameViewModelÀàÖÐ×Ö·û´®µÄÒÀÀµ£¬´úÂëÈçÏ£º
public class NameViewModel2:INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged([CallerMemberName]string propertyName="") { if (PropertyChanged!=null) { PropertyChanged(this,new PropertyChangedEventArgs(propertyName)); } } string _firstName; public string FirstName { get { return _firstName; } set { if (value!=_firstName) { _firstName = value; OnPropertyChanged();//ÕâÀï¾Í¿ÉÒÔÒƳý¡°FirstName¡±ÁË OnPropertyChanged("FullName"); } } } string _lastName; public string LastName { get { return _lastName; } set { if (value != _firstName) { _lastName = value; OnPropertyChanged();//ÕâÀï¾Í¿ÉÒÔÒƳý¡°LastName¡±ÁË OnPropertyChanged("FullName"); } } } public string FullName { get { return string.Format("{0} {1}", _firstName, _lastName); } } }ÕâÊÇÒ»ÖÖ¸ÄÉÆ£¬ÖØÃüÃûÊôÐÔFirstNameºÍLastName²»ÔÙÊÇÎÊÌâÁË£¬ÒòΪ.Net Framework»á°ïÎÒÃÇÌî³ä¿Õ°×¡£Æ´Ð´Ê§ÎóÒ²²»ÊÇÎÊÌâÁË£¬ÒòΪCallMemberNameÖ»»áʹÓÃÊôÐÔµÄÃû³Æ£¬µ«ÊÇ»¹±ØÐë֪ͨFullName¸ü¸ÄÁË£¬ÒòΪËüÊÇÒ»¸öµ¼³öÊôÐÔ¡£¶øÇÒÈÔÈ»ÓкܶàÑù°å´úÂ룬°üÀ¨ÏÔʽ֧³Ö×ֶκÍÐí¶àsetterÖеĴúÂë¡£
ÎÒÃÇ¿ÉÒÔʹÓÃAOPÀ´´¦ÀíÕâЩÎÊÌ⣬ÏÂÃæÎÒÃǾÍʹÓÃÒ»¸öеÄAOP¹¤¾ßÀ´ÐÖú´¦ÀíINotifyPropertyChanged¡£
֮ǰÒѾʹÓÃPostSharpºÍCastle DynamicÕâÁ½¸öAOP¿ò¼ÜÑÝʾÁ˺ܶàÀý×Ó£¬ÏÖÔÚÔÙÒýÈëÒ»¸öеĿò¼Ü£¬Õâ¸ö¿ò¼Ü֮ǰ½ÐINotifyPropertyWeaver,ÊÇרΪINotifyPropertyChanged¶øÉúµÄ£¬È»¶ø£¬ÏÖÔÚÕâ¸ö¿ò¼ÜÒѾÆúÓÃÁË£¬ÔÚÍøÉÏ»ù±¾ÕÒ²»µ½¹ØÓÚËüµÄÏûÏ¢ÁË£¬È»¶ø£¬ËûµÄ×÷Õß½«Ëü¼¯³Éµ½ÁËFodyÏîÄ¿ÖУ¬¶øÇÒËüÏÖÔÚµÄÃû×Ö½ÐPropertyChanged.Fody£¬°²×°Ê±£¬Ö±½ÓÔÚNuget¿ØÖÆ̨ÊäÈëInstall-Package PropertyChanged.Fody¼´¿É¡£
°²×°ºÃÖ®ºó£¬ÎÒÃÇÖ»ÐèÒª¶¨ÒåÒ»¸öÔʼµÄNameViewModelÀࣺ
È»ºó£¬ÉñÆæµÄµØ·½À´ÁË£¬Ö»ÐèÒªÔÚÕâ¸öÀàµÄÉÏ·½Ìí¼ÓÒ»¸öÌØÐÔImplementPropertyChanged¼´¿É£¬µ±È»Ç°ÃæµÄÊý¾Ý°ó¶¨µÄ²½Ö軹ÊÇÒª×öµÄ£º
[ImplementPropertyChanged] public class NameViewModel {ÔËÐгÌÐò£¬ÈçÏ£º
¼òֱ̫·½±ãÁË£¬Ö»ÐèÒª¼ÓÒ»¸öÌØÐԾͰÑ֮ǰҪдÄÇô¶à´úÂëµÄÊÂÇéÍê³ÉÁË¡£ÊÂʵÉÏ£¬Õâ¸öÌØÐÔ×öµÄ¾ÍÊÇÕâ¼þÊ£¬´úÂë±àÒëÖ®ºóµÄЧ¹ûºÍÎÒÃÇдÄÇô¶à´úÂëÊÇÒ»ÑùµÄ¡£
µ±È»£¬Õâ¸ö¹¤¾ßÀïÃ滹ҪºÜ¶àÆäËüÌØÐÔ£¬¸ÐÐËȤµÄͬѧ¿ÉÒÔÈ¥GithubÉÏȥѧϰ£¬Á´½ÓÔÚ´Ë£ºhttps://github.com/Fody/PropertyChanged/wiki¡£
PropertyChanged.Fody°ïÎÒÃÇÌî³äÁËËùÓеļä϶£¬ËüºÜ´ÏÃ÷£¬¿ÉÒÔ̽²â³öµ¼³öÊôÐÔ£¬´Ó¶øÌî³ä֪ͨ¼ä϶¡£PostSharpÒ²ÒÔPostSharp UltimateµÄÐÎʽÌṩÁËÀàËƵŦÄÜ¡£
PostSharp Ultimate
PostSharp UltimateÊÕ¼¯Á˺ܶàÏֳɵĿªÔ´ÇÐÃ棬°üÀ¨µÄÏÖ³ÉÇÐÃæÈçÏ£º
ÕâЩ¹¤¾ß¶¼ÊÇÃâ·ÑµÄ£¬µ«ÊÇËüÃÇÒªÇóPostSharpÊÇÈ«ÉÌÒµ°æ±¾¡£ÕâЩ¹¤¾ßµÄÓÅÊÆÊÇ¿ÉÒÔ»ñµÃAOPµÄËùÓÐÓÅÊÆÀ´½â¾öÌض¨µÄÎÊÌ⣬²»ÐèÒª´ÓÁ㿪ʼ±àдÇÐÃæ¡£µ±È»£¬ÎÒÃÇÒ²¿ÉÒÔʹÓÿªÔ´Ãâ·ÑµÄFody´úÌæ¡£
½ÓÏÂÀ´ÎÒÃÇʹÓÃPostSharpÀ´ÊµÏÖºÍÉÏÃæʹÓÃProperyChanged.FodyÏàͬµÄ¹¦ÄÜ¡£ÏÈ°²×°Postsharp,ÔÙ´´½¨Ò»¸ö½Ð×öNotifyPropertyChangedAspectÇÐÃæÀ࣬Ëü¼Ì³ÐÓÚLocationIntercetionAspect:
[Serializable] public class NotifyPropertyChangedAspect:LocationInterceptionAspect { private string[] _derivedProperties; public NotifyPropertyChangedAspect(params string[] derived)//¹¹Ô캯Êý²ÎÊýΪ¿É±ä³¤²ÎÊý£¬ÓÃÓÚ½ÓÊÕµ¼³öÊôÐÔ { _derivedProperties = derived; } public override void OnSetValue(LocationInterceptionArgs args) { //base.OnSetValue(args); } }»ØÒäһϣ¬Ö®Ç°ÎÒÃÇÒ»¿ªÊ¼ÊµÏÖINotifyPropertyChangedʱ£¬À§ÄѵŤ×÷¶¼·Åµ½setterÖÐÁË£¬Òò´Ë£¬ÕâÀïÐèÒªÖØдOnSetValue·½·¨£¬Õâ¸ö·½·¨»áÔÚʹÓÃÊôÐÔµÄsetterʱÔËÐУ¬¶øÇÒËü»á´úÌæsetterÔËÐС£
ÔÚOnSetValueÀïÃ棬ÐèÒª×ö2¼þÊ£º
¡¡