private void GenerateBehaviorActions(ActionCollection actionCollection, CodeTypeDeclaration classType, CodeMemberMethod method, CodeVariableReferenceExpression behaviorVarRef, string behaviorName)
        {
            for (int i = 0; i < actionCollection.Count; i++)
            {
                var    action     = actionCollection[i];
                string actionName = behaviorName + "_ACT_" + i;
                Type   type       = action.GetType();

                CodeVariableDeclarationStatement variable = new CodeVariableDeclarationStatement(type.Name, actionName, new CodeObjectCreateExpression(type.Name));
                method.Statements.Add(variable);
                var actionVarRef = new CodeVariableReferenceExpression(actionName);

                method.Statements.Add(new CodeMethodInvokeExpression(
                                          behaviorVarRef, "Actions.Add", actionVarRef));

                ValueGenerator valueGenerator      = new ValueGenerator();
                MethodInfo     generateFieldMethod = typeof(CodeComHelper).GetMethod("GenerateField");

                LocalValueEnumerator enumerator = action.GetLocalValueEnumerator();
                while (enumerator.MoveNext())
                {
                    LocalValueEntry    entry    = enumerator.Current;
                    DependencyProperty property = entry.Property;
                    Type valueType = entry.Value.GetType();
                    if (CodeComHelper.IsValidForFieldGenerator(entry.Value))
                    {
                        if (valueGenerator.Generators.ContainsKey(property.PropertyType) || valueGenerator.Generators.ContainsKey(valueType))
                        {
                            CodeExpression propValue = valueGenerator.ProcessGenerators(classType, method, entry.Value, actionName);
                            if (propValue != null)
                            {
                                method.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(actionVarRef, property.Name), propValue));
                            }
                        }
                        else if (entry.Value is PropertyPath)
                        {
                            PropertyPath path = entry.Value as PropertyPath;
                            method.Statements.Add(new CodeAssignStatement(
                                                      new CodeFieldReferenceExpression(actionVarRef, property.Name),
                                                      new CodeObjectCreateExpression("PropertyPath", new CodePrimitiveExpression(path.Path))));
                        }
                        else
                        {
                            MethodInfo generic = generateFieldMethod.MakeGenericMethod(property.PropertyType);
                            if (generic == null)
                            {
                                throw new NullReferenceException("Generic method not created for type - " + property.PropertyType);
                            }

                            generic.Invoke(null, new object[] { method, actionVarRef, action, property });
                        }
                    }
                }

                CodeComHelper.GenerateBindings(method, actionVarRef, action, actionName, behaviorVarRef);
                //CodeComHelper.GenerateResourceReferences(method, actionVarRef, action);
            }
        }
        private void GenerateBehaviors(BehaviorCollection behaviors, CodeTypeDeclaration classType, CodeMemberMethod method, FrameworkElement element, CodeExpression fieldReference)
        {
            for (int i = 0; i < behaviors.Count; i++)
            {
                var    behavior     = behaviors[i];
                string behaviorName = element.Name + "_BEH_" + i;
                Type   type         = behavior.GetType();
                CodeVariableDeclarationStatement variable = new CodeVariableDeclarationStatement(type.Name, behaviorName, new CodeObjectCreateExpression(type.Name));
                method.Statements.Add(variable);
                var behaviorVarRef = new CodeVariableReferenceExpression(behaviorName);

                method.Statements.Add(new CodeMethodInvokeExpression(
                                          new CodeVariableReferenceExpression("Interaction"), "GetBehaviors(" + element.Name + ").Add", behaviorVarRef));

                ValueGenerator valueGenerator      = new ValueGenerator();
                MethodInfo     generateFieldMethod = typeof(CodeComHelper).GetMethod("GenerateField");

                LocalValueEnumerator enumerator = behavior.GetLocalValueEnumerator();
                while (enumerator.MoveNext())
                {
                    LocalValueEntry    entry    = enumerator.Current;
                    DependencyProperty property = entry.Property;
                    Type valueType = entry.Value.GetType();
                    if (CodeComHelper.IsValidForFieldGenerator(entry.Value))
                    {
                        if (valueGenerator.Generators.ContainsKey(property.PropertyType) || valueGenerator.Generators.ContainsKey(valueType))
                        {
                            CodeExpression propValue = valueGenerator.ProcessGenerators(classType, method, entry.Value, behaviorName);
                            if (propValue != null)
                            {
                                method.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(behaviorVarRef, property.Name), propValue));
                            }
                        }
                        else if (entry.Value is ActionCollection)
                        {
                            GenerateBehaviorActions(entry.Value as ActionCollection, classType, method, behaviorVarRef, behaviorName);
                        }
                        else
                        {
                            MethodInfo generic = generateFieldMethod.MakeGenericMethod(property.PropertyType);
                            if (generic == null)
                            {
                                throw new NullReferenceException("Generic method not created for type - " + property.PropertyType);
                            }

                            generic.Invoke(null, new object[] { method, behaviorVarRef, behavior, property });
                        }
                    }
                }

                CodeComHelper.GenerateBindings(method, behaviorVarRef, behavior, behaviorName);
                CodeComHelper.GenerateResourceReferences(method, behaviorVarRef, behavior);
            }
        }
        /// <summary>
        /// Generates control fields
        /// </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);

            Path path = source as Path;

            if (CodeComHelper.IsValidForFieldGenerator(source.ReadLocalValue(Path.DataProperty)))
            {
                CodeExpression dataValueExpression = CodeComHelper.GetValueExpression(classType, method, path.Data, path.Name + "_G");
                method.Statements.Add(
                    new CodeAssignStatement(
                        new CodeFieldReferenceExpression(new CodeVariableReferenceExpression(path.Name), "Data"),
                        dataValueExpression));
            }

            return(fieldReference);
        }
        /// <summary>
        /// Generates control fields
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="classType">Type of the class.</param>
        /// <param name="method">The method.</param>
        /// <param name="generateField">if set to <c>true</c> [generate field].</param>
        /// <returns></returns>
        public virtual CodeExpression Generate(DependencyObject source, CodeTypeDeclaration classType, CodeMemberMethod method, bool generateField)
        {
            DataGridColumn column   = source as DataGridColumn;
            string         typeName = source.GetType().Name;
            string         name     = ColumnName;

            CodeExpression    fieldReference             = new CodeVariableReferenceExpression(name);
            CodeTypeReference variableType               = new CodeTypeReference(typeName);
            CodeVariableDeclarationStatement declaration = new CodeVariableDeclarationStatement(variableType, name);

            declaration.InitExpression = new CodeObjectCreateExpression(typeName);
            method.Statements.Add(declaration);

            if (CodeComHelper.IsValidForFieldGenerator(source.ReadLocalValue(DataGridColumn.WidthProperty)))
            {
                DataGridLength value = (DataGridLength)source.GetValue(DataGridColumn.WidthProperty);
                CodeTypeReferenceExpression dglType = new CodeTypeReferenceExpression("DataGridLength");

                switch (value.UnitType)
                {
                case DataGridLengthUnitType.Auto:
                    // AUTO is default value so we don't need to generate any code
                    break;

                case DataGridLengthUnitType.Pixel:
                    method.Statements.Add(new CodeAssignStatement(
                                              new CodeFieldReferenceExpression(fieldReference, DataGridColumn.WidthProperty.Name),
                                              new CodePrimitiveExpression((float)value.Value)));
                    break;

                case DataGridLengthUnitType.SizeToCells:
                    method.Statements.Add(new CodeAssignStatement(
                                              new CodeFieldReferenceExpression(fieldReference, DataGridColumn.WidthProperty.Name),
                                              new CodeFieldReferenceExpression(dglType, "SizeToCells")));
                    break;

                case DataGridLengthUnitType.SizeToHeader:
                    method.Statements.Add(new CodeAssignStatement(
                                              new CodeFieldReferenceExpression(fieldReference, DataGridColumn.WidthProperty.Name),
                                              new CodeFieldReferenceExpression(dglType, "SizeToHeader")));
                    break;

                case DataGridLengthUnitType.Star:
                    method.Statements.Add(new CodeAssignStatement(
                                              new CodeFieldReferenceExpression(fieldReference, DataGridColumn.WidthProperty.Name),
                                              new CodeObjectCreateExpression("DataGridLength",
                                                                             new CodePrimitiveExpression(Convert.ToSingle(value.Value)),
                                                                             new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("DataGridLengthUnitType"), "Star"))));
                    break;

                default:
                    break;
                }
            }

            CodeComHelper.GenerateFieldDoubleToFloat(method, fieldReference, source, DataGridColumn.MaxWidthProperty);
            CodeComHelper.GenerateFieldDoubleToFloat(method, fieldReference, source, DataGridColumn.MinWidthProperty);
            CodeComHelper.GenerateEnumField <Visibility>(method, fieldReference, source, DataGridColumn.VisibilityProperty);
            CodeComHelper.GenerateField <string>(method, fieldReference, source, DataGridColumn.SortMemberPathProperty);

            UIElement header = column.Header as UIElement;

            if (header != null)
            {
                TypeGenerator  headerGenerator = new TypeGenerator();
                CodeExpression headerExpr      = headerGenerator.ProcessGenerators(header, classType, method, false);

                method.Statements.Add(new CodeAssignStatement(
                                          new CodeFieldReferenceExpression(fieldReference, "Header"), headerExpr));
            }
            else if (column.Header != null)
            {
                CodeComHelper.GenerateField <object>(method, fieldReference, source, DataGridColumn.HeaderProperty);
                // TODO content can be another class, so this will not work
            }

            CodeComHelper.GenerateTemplateStyleField(classType, method, fieldReference, source, DataGridColumn.HeaderStyleProperty, name + "_h");

            return(fieldReference);
        }