/// <summary>
        /// Gets the code that assigns the target property back to the source property.
        /// </summary>
        public override IEnumerable <StatementSyntax> GetTransformBackCode()
        {
            var leftSide  = SyntaxFactory.ParseName("source." + PropertyMember.Name);
            var rightSide = SyntaxFactory.ParseName("target." + PropertyMember.TargetName);

            yield return(SyntaxHelper.GenerateAssignmentStatement(leftSide, rightSide));
        }
Пример #2
0
        /// <summary>
        /// Gets the code that assigns the target property back to the source property.
        /// </summary>
        public override IEnumerable <StatementSyntax> GetTransformBackCode()
        {
            ExpressionSyntax leftSide  = SyntaxFactory.ParseName("source." + PropertyMember.Name);
            ExpressionSyntax rightSide = SyntaxFactory.ParseName("target." + PropertyMember.TargetName);

            rightSide = SyntaxHelper.GenerateStaticMethodCall("Convert", typeof(PropertyConverter).FullName, new[]
            {
                SyntaxFactory.Argument(rightSide)
            }, typeArguments: new[] { PropertyMember.TargetType, PropertyMember.Type }).Expression;

            yield return(SyntaxHelper.GenerateAssignmentStatement(leftSide, rightSide));
        }
        /// <summary>
        /// Generates the collection null check.
        /// </summary>
        protected StatementSyntax GenerateCollectionNullCheck(Type collectionType, string identifier, string propertyName)
        {
            if (collectionType.IsInterface)
            {
                collectionType = typeof(List <>).MakeGenericType(collectionType.GetGenericArguments().First());
            }

            var value = SyntaxFactory.ObjectCreationExpression(SyntaxHelper.GenerateTypeSyntax(collectionType), SyntaxFactory.ArgumentList(), null);

            return(SyntaxFactory.IfStatement(
                       SyntaxFactory.BinaryExpression(SyntaxKind.EqualsExpression, SyntaxFactory.ParseName(identifier + "." + propertyName), SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)),
                       SyntaxHelper.GenerateAssignmentStatement(SyntaxFactory.ParseName(identifier + "." + propertyName), value)
                       ));
        }
        /// <summary>
        /// Gets the transform back code.
        /// </summary>
        public override IEnumerable <StatementSyntax> GetTransformBackCode()
        {
            // declare converter
            var converterName = GenerateCollectionHandlerName(PropertyMember.Name);
            var converterType = GetCollectionHandlerType(typeof(SyncCollectionHandler <,>), PropertyMember.TargetType, PropertyMember.Type);
            var elementTypes  = converterType.GetGenericArguments();

            yield return(SyntaxHelper.GenerateVariableDeclarationAndObjectCreationStatement(converterName, converterType));

            // set key accessors
            yield return(SyntaxHelper.GenerateAssignmentStatement(
                             SyntaxFactory.ParseName(converterName + ".KeySelector1"),
                             SyntaxFactory.ParseExpression("__x => __x." + KeyPropertyName)
                             ));

            yield return(SyntaxHelper.GenerateAssignmentStatement(
                             SyntaxFactory.ParseName(converterName + ".KeySelector2"),
                             SyntaxFactory.ParseExpression("__x => __x." + KeyPropertyName)
                             ));

            yield return(SyntaxHelper.GenerateAssignmentStatement(
                             SyntaxFactory.ParseName(converterName + ".NewItemFactory"),
                             SyntaxHelper.GenerateGenericName(typeof(PropertyConverter).FullName + ".Convert", elementTypes)
                             ));

            yield return(SyntaxHelper.GenerateAssignmentStatement(
                             SyntaxFactory.ParseName(converterName + ".UpdateItemAction"),
                             SyntaxHelper.GenerateGenericName(typeof(PropertyConverter).FullName + ".Populate", elementTypes)
                             ));

            yield return(GenerateCollectionNullCheck(PropertyMember.Type, "source", PropertyMember.Name));

            yield return(GenerateCollectionNullCheck(PropertyMember.TargetType, "target", PropertyMember.TargetName));

            // call the converter
            yield return(SyntaxHelper.GenerateStaticMethodCall("SynchronizeCollections", converterName, new[]
            {
                SyntaxFactory.Argument(SyntaxFactory.ParseName("target." + PropertyMember.TargetName)),
                SyntaxFactory.Argument(SyntaxFactory.ParseName("source." + PropertyMember.Name))
            }));
        }