HTML5¼¼Êõ

.NetÖеÄAOPϵÁÐÖ®¡¶À¹½ØλÖá· - tkbÖÁ¼ò(5)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£º²©¿ÍÔ° 2016-08-30 18:00 ÎÒÒªÆÀÂÛ( )

²»Ê¹ÓÃAOPÒ²¿ÉÒÔ»º½âÕâЩÎÊÌ⣬±ÈÈç¿ÉÒÔдµ¥Ôª²âÊÔ»òÕß·ÀÓùÐÔ±à³Ì£¬ËüÃÇ¿ÉÒÔÑéÖ¤ËùÓн«»á·¢Ë͵ÄÕýȷ֪ͨ¡£ËäÈ»¿ÉÒÔʹÓ÷¢ÉäʹµÃÕâ¼þʼòµ¥Ð©£¬µ«ÊÇ»áDZÔÚ²úÉúºÜ¶à´úÂë¡££¨±ÈÈç¿ÉÒÔʹÓ÷´Éä¿ÉÒÔÑ­»·±éÀúÀàµÄËùÓÐÊôÐÔ

²»Ê¹ÓÃ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¡£

ʹÓÃAOP¼õÉÙÑù°å´úÂë

֮ǰÒѾ­Ê¹ÓÃPostSharpºÍCastle DynamicÕâÁ½¸öAOP¿ò¼ÜÑÝʾÁ˺ܶàÀý×Ó£¬ÏÖÔÚÔÙÒýÈëÒ»¸öеĿò¼Ü£¬Õâ¸ö¿ò¼Ü֮ǰ½ÐINotifyPropertyWeaver,ÊÇרΪINotifyPropertyChanged¶øÉúµÄ£¬È»¶ø£¬ÏÖÔÚÕâ¸ö¿ò¼ÜÒѾ­ÆúÓÃÁË£¬ÔÚÍøÉÏ»ù±¾ÕÒ²»µ½¹ØÓÚËüµÄÏûÏ¢ÁË£¬È»¶ø£¬ËûµÄ×÷Õß½«Ëü¼¯³Éµ½ÁËFodyÏîÄ¿ÖУ¬¶øÇÒËüÏÖÔÚµÄÃû×Ö½ÐPropertyChanged.Fody£¬°²×°Ê±£¬Ö±½ÓÔÚNuget¿ØÖÆ̨ÊäÈëInstall-Package PropertyChanged.Fody¼´¿É¡£
°²×°ºÃÖ®ºó£¬ÎÒÃÇÖ»ÐèÒª¶¨ÒåÒ»¸öԭʼµÄNameViewModelÀࣺ

public class NameViewModel { public string FirstName { get; set; } public string LastName { get; set; } public string FullName { get { return String.Format("{0}{1}", FirstName, LastName); } } }

È»ºó£¬ÉñÆæµÄµØ·½À´ÁË£¬Ö»ÐèÒªÔÚÕâ¸öÀàµÄÉÏ·½Ìí¼ÓÒ»¸öÌØÐÔImplementPropertyChanged¼´¿É£¬µ±È»Ç°ÃæµÄÊý¾Ý°ó¶¨µÄ²½Ö軹ÊÇÒª×öµÄ£º

[ImplementPropertyChanged] public class NameViewModel {

ÔËÐгÌÐò£¬ÈçÏ£º

¼òֱ̫·½±ãÁË£¬Ö»ÐèÒª¼ÓÒ»¸öÌØÐԾͰÑ֮ǰҪдÄÇô¶à´úÂëµÄÊÂÇéÍê³ÉÁË¡£ÊÂʵÉÏ£¬Õâ¸öÌØÐÔ×öµÄ¾ÍÊÇÕâ¼þÊ£¬´úÂë±àÒëÖ®ºóµÄЧ¹ûºÍÎÒÃÇдÄÇô¶à´úÂëÊÇÒ»ÑùµÄ¡£

µ±È»£¬Õâ¸ö¹¤¾ßÀïÃ滹ҪºÜ¶àÆäËüÌØÐÔ£¬¸ÐÐËȤµÄͬѧ¿ÉÒÔÈ¥GithubÉÏȥѧϰ£¬Á´½ÓÔÚ´Ë£ºhttps://github.com/Fody/PropertyChanged/wiki¡£

PropertyChanged.Fody°ïÎÒÃÇÌî³äÁËËùÓеļä϶£¬ËüºÜ´ÏÃ÷£¬¿ÉÒÔ̽²â³öµ¼³öÊôÐÔ£¬´Ó¶øÌî³ä֪ͨ¼ä϶¡£PostSharpÒ²ÒÔPostSharp UltimateµÄÐÎʽÌṩÁËÀàËƵŦÄÜ¡£

PostSharp Ultimate
PostSharp UltimateÊÕ¼¯Á˺ܶàÏֳɵĿªÔ´ÇÐÃ棬°üÀ¨µÄÏÖ³ÉÇÐÃæÈçÏ£º

  • ¶àÏß³Ì
  • Õï¶Ï£¨logging£©
  • ÓÐÏÞµÄINotifyPropertyChangedÇÐÃæ
    ÕâЩ¹¤¾ß¶¼ÊÇÃâ·ÑµÄ£¬µ«ÊÇËüÃÇÒªÇó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¼þÊ£º

  • ±È½ÏÐÂÖµºÍ¾ÉÖµ£¬Èç¹û²»µÈ£¬ÄÇôӦ¸ÃÔÊÐíset²Ù×÷ͨ¹ý£¨Ê¹ÓÃPostSharpµÄargs.ProceedSetValue·½·¨£©£»
  • ÐèҪ֪ͨÒѾ­¸Ä±äµÄÊôÐÔ£¨°üÀ¨¹¹Ô캯ÊýÖÐÖ¸¶¨µÄÈκε¼³öÊôÐÔ£©¡£´´½¨¿ÉÒÔÖظ´Ê¹ÓõÄRaisePropertyChanged·½·¨£¬¶ÔÖ¸¶¨µÄËùÓе¼³öÊôÐÔ½øÐÐÑ­»·±éÀú£¬²¢µ÷ÓÃRaisePropertyChanged·½·¨¡£´úÂëÈçÏ£º
  • [Serializable] public class NotifyPropertyChangedAspect:LocationInterceptionAspect { private string[] _derivedProperties; public NotifyPropertyChangedAspect(params string[] derived)//¹¹Ô캯Êý²ÎÊýΪ¿É±ä³¤²ÎÊý£¬ÓÃÓÚ½ÓÊÕµ¼³öÊôÐÔ { _derivedProperties = derived; } public override void OnSetValue(LocationInterceptionArgs args) { var oldValue = args.GetCurrentValue(); var newValue = args.Value; if (oldValue!=newValue) { args.ProceedSetValue(); RaisePropertyChanged(args.Instance, args.LocationName);//Ö»ÒªÊôÐÔÖ´ÐÐÁËsetter£¬¾Í´¥·¢RaisePropertyChangedʼþ if (_derivedProperties!=null) { //¶Ôÿ¸öµ¼³öÊôÐÔ´¥·¢Ê¼þ foreach (string derivedProperty in _derivedProperties) { RaisePropertyChanged(args.Instance,derivedProperty); } } } } private void RaisePropertyChanged(object p1, string p2) { throw new NotImplementedException(); } }

    ¡¡

    1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

    Ïà¹ØÎÄÕÂ
    • ÉýѶÍþ΢ÐÅÓªÏúϵͳ¿ª·¢Êµ¼ù£º£¨3£©ÖпطþÎñÆ÷µÄÉè¼Æ .Net »¹ÊÇ Java£¿ - sheng.chao

      ÉýѶÍþ΢ÐÅÓªÏúϵͳ¿ª·¢Êµ¼ù£º£¨3£©ÖпطþÎñÆ÷µÄÉè¼Æ .Net »¹ÊÇ Java

      2016-08-26 16:00

    • ¿ªÔ´£ºASP.NET MVC+EF6+Bootstrap¿ª·¢¿ò¼Ü - NFine

      ¿ªÔ´£ºASP.NET MVC+EF6+Bootstrap¿ª·¢¿ò¼Ü - NFine

      2016-08-24 11:00

    • ASP.NET ¹ØÓÚGridView ±í¸ñÖظ´Áкϲ¢ - С·É·Éoo

      ASP.NET ¹ØÓÚGridView ±í¸ñÖظ´Áкϲ¢ - С·É·Éoo

      2016-08-23 10:02

    • Óµ±§.NET Core£¬ÈçºÎ¿ª·¢Ò»¸ö¿çƽ̨Àà¿â (1) - KAnts

      Óµ±§.NET Core£¬ÈçºÎ¿ª·¢Ò»¸ö¿çƽ̨Àà¿â (1) - KAnts

      2016-08-08 14:00

    ÍøÓѵãÆÀ
    ¦