Ejemplo n.º 1
0
        private IAccessor CreateSetter(IConstructLanguage language, string propertyName, IFieldDeclaration field)
        {
            string propertyChangedName = language.Name == LanguageNames.CSharp ? "OnPropertyChanged" : "RaisePropertyChangedEvent";

            IMethodInvocation onPropertyChanged = language.MethodInvocation(language.None <IExpression>(),
                                                                            language.Identifier(propertyChangedName),
                                                                            language.None <ITypeArguments>(),
                                                                            language.Arguments(
                                                                                language.Argument(language.StringLiteral(propertyName))));

            IMemberAccess fieldUsage = language.MemberAccess(language.None <IExpression>(), field.Identifier);
            IExpression   valueUsage = language.Expression("value");

            IStatement assignment = language.AssignmentStatement(fieldUsage, valueUsage);

            IIfStatement ifStatement =
                language.IfStatement(
                    language.BinaryExpression(fieldUsage,
                                              Operator.NotEqual,
                                              valueUsage),
                    language.Block(assignment, language.ExpressionStatement(onPropertyChanged)));

            IAccessor setter = language.Setter(language.Block(ifStatement));

            return(setter);
        }
Ejemplo n.º 2
0
        private void InsertWrappedProperties(IClassDeclaration viewModel, List <IPropertyDeclaration> confirmedPropertiesForWrapping, IFieldDeclaration wrappedClassField, IIdentifier onPropertyChangedIdentifier)
        {
            IConstructLanguage language = viewModel.Language;

            foreach (IPropertyDeclaration property in confirmedPropertiesForWrapping)
            {
                IMemberAccess propertyMemberAccess = language.MemberAccess(
                    language.MemberAccess(language.None <IExpression>(), wrappedClassField.Identifier),
                    property.Identifier);

                IAccessor getterOfWrapper = language.None <IAccessor>();
                IAccessor propertyGetter  = property.Getter();

                if (propertyGetter.Exists && IsAccessorVisibleOutsideClass(propertyGetter.Modifiers))
                {
                    getterOfWrapper = language.Getter(
                        language.Modifiers(propertyGetter.Modifiers.Modifiers),
                        language.Block(
                            language.ReturnStatement(propertyMemberAccess)));
                }

                IAccessor setterOfWrapper = language.None <IAccessor>();
                IAccessor propertySetter  = property.Setter();

                if (propertySetter.Exists && IsAccessorVisibleOutsideClass(propertySetter.Modifiers))
                {
                    IStatement assignment = language.AssignmentStatement(propertyMemberAccess, language.Expression("value"));

                    IMethodInvocation onPropertyChangedInvocation =
                        language.MethodInvocation(
                            language.None <IExpression>(),
                            onPropertyChangedIdentifier,
                            language.None <ITypeArguments>(),
                            language.Arguments(
                                language.Argument(language.StringLiteral(property.Identifier.Name))));

                    IIfStatement ifStatement =
                        language.IfStatement(
                            language.BinaryExpression(propertyMemberAccess,
                                                      Operator.NotEqual,
                                                      language.Expression("value")),
                            language.Block(assignment, language.ExpressionStatement(onPropertyChangedInvocation)));

                    setterOfWrapper = language.Setter(
                        language.Modifiers(propertyGetter.Modifiers.Modifiers),
                        language.Block(ifStatement));
                }

                if (getterOfWrapper.Exists || setterOfWrapper.Exists)
                {
                    IPropertyDeclaration wrapperProperty = language.Property(
                        language.None <IDocComment>(),
                        language.None <IAttributes>(),
                        property.Modifiers,
                        property.TypeName,
                        property.Identifier,
                        getterOfWrapper,
                        setterOfWrapper);
                    viewModel.Insert(wrapperProperty);
                }
            }
        }
        private IAccessor CreateSetter(IConstructLanguage language, string propertyName, IFieldDeclaration field)
        {
            string propertyChangedName = language.Name == LanguageNames.CSharp ? "OnPropertyChanged" : "RaisePropertyChangedEvent";

            IMethodInvocation onPropertyChanged = language.MethodInvocation(language.None<IExpression>(),
                language.Identifier(propertyChangedName),
                language.None<ITypeArguments>(),
                language.Arguments(
                    language.Argument(language.StringLiteral(propertyName))));

            IMemberAccess fieldUsage = language.MemberAccess(language.None<IExpression>(), field.Identifier);
            IExpression valueUsage = language.Expression("value");

            IStatement assignment = language.AssignmentStatement(fieldUsage, valueUsage);

            IIfStatement ifStatement =
                language.IfStatement(
                    language.BinaryExpression(fieldUsage,
                        Operator.NotEqual,
                        valueUsage),
                    language.Block(assignment, language.ExpressionStatement(onPropertyChanged)));

            IAccessor setter = language.Setter(language.Block(ifStatement));
            return setter;
        }