예제 #1
0
        /// <summary>
        /// Generates the automatic properties.
        /// </summary>
        /// <param name="type">Represents a type declaration for a class, structure, interface, or enumeration.</param>
        private void GenerateAutomaticProperties(CodeTypeDeclaration type)
        {
            if (Equals(GeneratorContext.GeneratorParams.Language, GenerationLanguage.CSharp))
            {
                // If databinding is disable, use automatic property
                if (GeneratorContext.GeneratorParams.PropertyParams.AutomaticProperties)
                {
                    foreach (var item in this.autoPropertyListField)
                    {
                        var  cm = new CodeSnippetTypeMember();
                        bool transformToAutomaticproperty = true;

                        var attributesString = new List <string>();
                        foreach (var attribute in item.CustomAttributes)
                        {
                            var attrib = attribute as CodeAttributeDeclaration;
                            if (attrib != null)
                            {
                                // Don't transform property with default value.
                                if (attrib.Name == "System.ComponentModel.DefaultValueAttribute")
                                {
                                    transformToAutomaticproperty = false;
                                }
                                else
                                {
                                    string attributesArguments = string.Empty;
                                    foreach (var arg in attrib.Arguments)
                                    {
                                        var argument = arg as CodeAttributeArgument;
                                        if (argument != null)
                                        {
                                            attributesArguments += AttributeArgumentToString(argument) + ",";
                                        }
                                    }
                                    // Remove last ","
                                    if (attributesArguments.Length > 0)
                                    {
                                        attributesArguments = attributesArguments.Remove(attributesArguments.Length - 1);
                                    }

                                    attributesString.Add(string.Format("[{0}({1})]", attrib.Name, attributesArguments));
                                }
                            }
                        }

                        if (transformToAutomaticproperty)
                        {
                            foreach (var attribute in attributesString)
                            {
                                cm.Text += "    " + attribute + "\n";
                            }
                            var ct   = new CodeTypeReferenceExpression(item.Type);
                            var prop = ExpressionToString(ct);
                            var text = string.Format("    public {0} {1} ", prop, item.Name);
                            cm.Text += string.Concat(text, "{get; set;}\n");
                            cm.Comments.AddRange(item.Comments);

                            type.Members.Add(cm);
                            type.Members.Remove(item);
                            var field = type.GetField(item.GetFieldNameFromProperty());
                            if (field != null)
                            {
                                type.Members.Remove(field);
                            }
                        }
                    }

                    // Now remove all private fileds

                    /*     foreach (var item in this.fieldListToRemoveField)
                     *   {
                     *       if (item.Name == "mailClassField" && type.Name == "uspsSummaryType")
                     *       {
                     *           ;
                     *       }
                     *       type.Members.Remove(item);
                     *   }*/
                }
            }
        }