Example #1
0
        public CodeExpression Generate(CodeTypeDeclaration parentClass, CodeMemberMethod method, object value, string baseName, ResourceDictionary dictionary = null)
        {
            var refExpr2 = new CodeObjectCreateExpression(ValueType, new CodeExpression[] { });
            CodeVariableDeclarationStatement variable = new CodeVariableDeclarationStatement(
                ValueType, baseName, refExpr2
                );

            method.Statements.Add(variable);

            var refExpr = new CodeVariableReferenceExpression(baseName);

            foreach (var pi in ValueType.GetProperties())
            {
                var getM = pi.GetGetMethod();

                if (getM.GetParameters().Length == 0 && pi.GetSetMethod() != null)
                {
                    object o = getM.Invoke(value, new object[] { });
                    CodeComHelper.GenerateFieldNonGeneric(method, refExpr, pi.Name, o);
                }
            }



            return(refExpr);
        }
        /// <summary>
        /// Generates code
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="classType">Type of the class.</param>
        /// <param name="method">The initialize method.</param>
        /// <param name="generateField">if set to <c>true</c> [generate field].</param>
        /// <returns></returns>
        public override CodeExpression Generate(DependencyObject source, CodeTypeDeclaration classType, CodeMemberMethod method, bool generateField)
        {
            CodeExpression fieldReference = base.Generate(source, classType, method, generateField);

            TextBlock textBlock = source as TextBlock;

            CodeComHelper.GenerateBrushField(method, fieldReference, source, TextBlock.BackgroundProperty);
            CodeComHelper.GenerateBrushField(method, fieldReference, source, TextBlock.ForegroundProperty);

            if (textBlock.Inlines.Count > 1)
            {
                StringBuilder text = new StringBuilder();
                foreach (var line in textBlock.Inlines)
                {
                    Run runLine = line as Run;
                    if (runLine != null)
                    {
                        text.AppendLine(runLine.Text);
                    }
                }

                CodeComHelper.GenerateFieldNonGeneric(method, fieldReference, "Text", text.ToString());
            }
            else
            {
                CodeComHelper.GenerateField <string>(method, fieldReference, source, TextBlock.TextProperty);
            }

            CodeComHelper.GenerateThicknessField(method, fieldReference, source, TextBlock.PaddingProperty);
            CodeComHelper.GenerateEnumField <TextAlignment>(method, fieldReference, source, TextBlock.TextAlignmentProperty);
            CodeComHelper.GenerateEnumField <TextWrapping>(method, fieldReference, source, TextBlock.TextWrappingProperty);

            if (!CodeComHelper.IsDefaultValue(source, TextBlock.FontFamilyProperty) ||
                !CodeComHelper.IsDefaultValue(source, TextBlock.FontSizeProperty) ||
                !CodeComHelper.IsDefaultValue(source, TextBlock.FontStyleProperty) ||
                !CodeComHelper.IsDefaultValue(source, TextBlock.FontWeightProperty))
            {
                FontGenerator.Instance.AddFont(textBlock.FontFamily, textBlock.FontSize, textBlock.FontStyle, textBlock.FontWeight, method);
            }

            CodeComHelper.GenerateFontFamilyField(method, fieldReference, source, TextBlock.FontFamilyProperty);
            CodeComHelper.GenerateFieldDoubleToFloat(method, fieldReference, source, TextBlock.FontSizeProperty);
            CodeComHelper.GenerateFontStyleField(method, fieldReference, source, TextBlock.FontStyleProperty, TextBlock.FontWeightProperty);

            return(fieldReference);
        }