protected override string GenerateCode(LanguageProperties language, IClass currentClass) { StringBuilder builder = new StringBuilder(); IDocumentLine line = editor.Document.GetLineForOffset(anchor.Offset); string indent = DocumentUtilitites.GetWhitespaceAfter(editor.Document, line.Offset); bool implementInterface = this.implementInterface.IsChecked == true; bool hasOnPropertyChanged = HasOnPropertyChanged(currentClass); bool useEventArgs = false; if (implementInterface && !currentClass.IsStatic) { if (!hasOnPropertyChanged) { var nodes = new List <AbstractNode>(); var rt = new GetClassReturnType(currentClass.ProjectContent, "System.ComponentModel.INotifyPropertyChanged", 0); if (!currentClass.ClassInheritanceTree.Any(bt => bt.FullyQualifiedName == "System.ComponentModel.INotifyPropertyChanged")) { int insertion = editor.Document.PositionToOffset(currentClass.BodyRegion.BeginLine, currentClass.BodyRegion.BeginColumn); if (currentClass.BaseTypes.Count > 0) { editor.Document.Insert(insertion, ", INotifyPropertyChanged"); } else { editor.Document.Insert(insertion, " : INotifyPropertyChanged"); } } language.CodeGenerator.ImplementInterface(nodes, rt, false, currentClass); var ev = rt.GetEvents().First(e => e.Name == "PropertyChanged"); MethodDeclaration onEvent = language.CodeGenerator.CreateOnEventMethod(new DefaultEvent(ev.Name, ev.ReturnType, ev.Modifiers, ev.Region, ev.BodyRegion, currentClass)); nodes.Add(onEvent); onEvent.Parameters[0].TypeReference = new TypeReference("string", true); onEvent.Parameters[0].ParameterName = "propertyName"; ((RaiseEventStatement)onEvent.Body.Children[0]).Arguments[1] = new ObjectCreateExpression(new TypeReference("PropertyChangedEventArgs"), new List <Expression> { new IdentifierExpression("propertyName") }); foreach (var node in nodes) { builder.AppendLine(language.CodeGenerator.GenerateCode(node, indent)); } useEventArgs = false; } else { useEventArgs = currentClass.DefaultReturnType.GetMethods().First(m => m.Name == "OnPropertyChanged").Parameters[0].ReturnType.FullyQualifiedName != "System.String"; } } foreach (FieldWrapper field in listBox.SelectedItems) { var prop = language.CodeGenerator.CreateProperty(field.Field, true, field.AddSetter); if (!field.Field.IsStatic && !currentClass.IsStatic && field.AddSetter && implementInterface) { var invocation = new ExpressionStatement(CreateInvocation(field.PropertyName, useEventArgs)); var assignment = prop.SetRegion.Block.Children[0]; prop.SetRegion.Block.Children.Clear(); prop.SetRegion.Block.AddChild( new IfElseStatement( new BinaryOperatorExpression(new IdentifierExpression(field.MemberName), BinaryOperatorType.InEquality, new IdentifierExpression("value")), new BlockStatement { Children = { assignment, invocation } } ) ); } builder.AppendLine(language.CodeGenerator.GenerateCode(prop, indent)); } return(builder.ToString().Trim()); }
CodeExpression CreateMemberExpression(CodeExpression target, string parentName, string name, bool isStatic) { _fieldReferenceType = null; string combinedName = parentName + "." + name; if (pc.GetClass(combinedName, 0) != null) { return(new CodeTypeReferenceExpression(combinedName)); } else if (pc.NamespaceExists(combinedName)) { return(new CodeTypeReferenceExpression(combinedName)); } GetClassReturnType rt = new GetClassReturnType(pc, parentName, 0); foreach (IProperty prop in rt.GetProperties()) { if (prop.IsStatic == isStatic && prop.Name == name) { _fieldReferenceType = prop.ReturnType; return(new CodePropertyReferenceExpression(target, name)); } } foreach (IEvent ev in rt.GetEvents()) { if (ev.IsStatic == isStatic && ev.Name == name) { _fieldReferenceType = ev.ReturnType; return(new CodeEventReferenceExpression(target, name)); } } foreach (IMethod me in rt.GetMethods()) { if (me.IsStatic == isStatic && me.Name == name) { _fieldReferenceType = me.ReturnType; CodeMethodReferenceExpression cmre = new CodeMethodReferenceExpression(target, name); cmre.UserData["methodUserData"] = me; return(cmre); } } foreach (IField field in rt.GetFields()) { if (field.IsStatic == isStatic && field.Name == name) { _fieldReferenceType = field.ReturnType; return(new CodeFieldReferenceExpression(target, name)); } } // unknown member, guess: if (char.IsUpper(name, 0)) { return(new CodePropertyReferenceExpression(target, name)); } else { return(new CodeFieldReferenceExpression(target, name)); } }