/// <summary> /// Generates code in /// </summary> /// <param name="genAttr"></param> /// <param name="parentWriter"></param> /// <remarks></remarks> private void GenInMember(NotifyPropertyChanged_GenAttribute genAttr, Writer parentWriter) { var prop = genAttr.ParentProperty; //!Parent can be either CodeFunction(only for ExtraNotifications) or CodeProperty string code = null; switch (genAttr.GenerationType) { case NotifyPropertyChanged_GenAttribute.GenerationTypes.NotifyOnly: //Only notification code = string.Format("this.NotifyChanged({0})", prop.Name); break; default: code = (genAttr.ParentProperty != null) ? string.Format("this.SetPropertyAndNotify(ref _{0}, value, \"{0}\");", prop.Name) : ""; break; } //Extra notifications var extraNotifyCode = GenInMember_ExtraNotifications(genAttr, parentWriter); code = code.Conjoin(Environment.NewLine, extraNotifyCode); //Code Element, could be property setter or a method var codeElement = (CodeFunction2)((prop != null) ? prop.Setter : genAttr.ParentFunction); var memberWriter = new Writer(parentWriter) { GenAttribute = genAttr, SearchStart = codeElement.StartPoint, SearchEnd = codeElement.EndPoint, Content = code, SegmentType = Types.Statements }; //Find insertion point EditPoint insertPoint = null; var insertTag = Manager.FindInsertionPoint(memberWriter); if (insertTag == null) { //!No insertion point tag specified, by default insert as last line of setter insertPoint = codeElement.GetPositionBeforeClosingBrace(); //always insert new line in case the everything in one line } else { //!InsertPoint Tag found, insert right after it insertPoint = insertTag.EndPoint.CreateEditPoint(); insertPoint.LineDown(1); insertPoint.StartOfLine(); } memberWriter.InsertStart = insertPoint; Manager.InsertOrReplace(memberWriter); }
/// <summary> /// Generate code that will notify other propertyName different from the member with the attribute. /// </summary> /// <param name="genAttr"></param> /// <param name="parentWriter"></param> /// <remarks> /// Example Add NotifyPropertyChanged_GenAttribute with ExtraNotifications="OtherProperty1,OtherProperty2" to SomeProperty. /// This method will generate code for Notify("OtherProperty1") and Notify("OtherProperty2") within that member /// This is useful for Property that affects other Property, or a method that affects another property. /// This has the advantage of generation/compile time verification of the properties /// </remarks> private string GenInMember_ExtraNotifications(NotifyPropertyChanged_GenAttribute genAttr, Writer parentWriter) { //Render extra notifications (notifications for other related properties) if (string.IsNullOrEmpty(genAttr.ExtraNotifications)) { return(null); } //also split by space to trim it var extras = genAttr.ExtraNotifications.Split(new[] { ",", " " }, StringSplitOptions.RemoveEmptyEntries); //Verify that all properties listed in ExtraNotification actually exists var invalids = genAttr.ValidateExtraNotifications(parentWriter.Class, extras); if (invalids.Any()) { parentWriter.HasError = true; parentWriter.Status.AppendFormat("Properties:{0} to be notified are not found in the class", string.Join(", ", invalids)); return(""); } return(string.Format("this.NotifyChanged({0});", string.Join(",", extras.Select((x) => x.Quote())))); }
/// <summary> /// Generates code in /// </summary> /// <param name="genAttr"></param> /// <param name="parentWriter"></param> /// <remarks></remarks> private void GenInMember(NotifyPropertyChanged_GenAttribute genAttr, Writer parentWriter) { var prop = genAttr.ParentProperty; //!Parent can be either CodeFunction(only for ExtraNotifications) or CodeProperty string code = null; switch (genAttr.GenerationType) { case NotifyPropertyChanged_GenAttribute.GenerationTypes.NotifyOnly: //Only notification code = string.Format("this.NotifyChanged({0})", prop.Name); break; default: code = (genAttr.ParentProperty != null) ? string.Format("this.SetPropertyAndNotify(ref _{0}, value, \"{0}\");", prop.Name) : ""; break; } //Extra notifications var extraNotifyCode = GenInMember_ExtraNotifications(genAttr, parentWriter); code = code.Conjoin(Environment.NewLine, extraNotifyCode); //Code Element, could be property setter or a method var codeElement = (CodeFunction2)((prop != null) ? prop.Setter : genAttr.ParentFunction); var memberWriter = new Writer(parentWriter) {GenAttribute = genAttr, SearchStart = codeElement.StartPoint, SearchEnd = codeElement.EndPoint, Content = code, SegmentType = Types.Statements}; //Find insertion point EditPoint insertPoint = null; var insertTag = Manager.FindInsertionPoint(memberWriter); if (insertTag == null) { //!No insertion point tag specified, by default insert as last line of setter insertPoint = codeElement.GetPositionBeforeClosingBrace(); //always insert new line in case the everything in one line } else { //!InsertPoint Tag found, insert right after it insertPoint = insertTag.EndPoint.CreateEditPoint(); insertPoint.LineDown(1); insertPoint.StartOfLine(); } memberWriter.InsertStart = insertPoint; Manager.InsertOrReplace(memberWriter); }
/// <summary> /// Generate code that will notify other propertyName different from the member with the attribute. /// </summary> /// <param name="genAttr"></param> /// <param name="parentWriter"></param> /// <remarks> /// Example Add NotifyPropertyChanged_GenAttribute with ExtraNotifications="OtherProperty1,OtherProperty2" to SomeProperty. /// This method will generate code for Notify("OtherProperty1") and Notify("OtherProperty2") within that member /// This is useful for Property that affects other Property, or a method that affects another property. /// This has the advantage of generation/compile time verification of the properties /// </remarks> private string GenInMember_ExtraNotifications(NotifyPropertyChanged_GenAttribute genAttr, Writer parentWriter) { //Render extra notifications (notifications for other related properties) if (string.IsNullOrEmpty(genAttr.ExtraNotifications)) { return null; } //also split by space to trim it var extras = genAttr.ExtraNotifications.Split(new[] { ",", " " }, StringSplitOptions.RemoveEmptyEntries); //Verify that all properties listed in ExtraNotification actually exists var invalids = genAttr.ValidateExtraNotifications(parentWriter.Class, extras); if (invalids.Any()) { parentWriter.HasError = true; parentWriter.Status.AppendFormat("Properties:{0} to be notified are not found in the class", string.Join(", ", invalids)); return ""; } return string.Format("this.NotifyChanged({0});", string.Join(",", extras.Select((x) => x.Quote()))); }