Exemplo n.º 1
0
        /// <summary>
        /// Creates and adds a public auto-property (property and backening field) to the class.
        /// </summary>
        public static CodeTypeMemberCollection CreateAutoProperty(string name,
                                                                  string summaryComment,
                                                                  CodeTypeReference propertyType,
                                                                  IEnumerable <string> usedNames,
                                                                  bool readOnly,
                                                                  Type implementedInterface)
        {
            // Validate parameters.
            name.ThrowIfNullOrEmpty("name");
            propertyType.ThrowIfNull("propertyType");
            usedNames.ThrowIfNull("usedNames");

            // Generate the property name.
            string propertyName = GeneratorUtils.GetPropertyName(name, usedNames);

            // Create backening field.
            var    field        = CreateBackingField(name, propertyType, usedNames.Concat(propertyName));
            string fieldName    = field.Name;
            var    fieldNameRef = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldName);

            // Add property.
            var property = new CodeMemberProperty();

            property.Name       = propertyName;
            property.Attributes = MemberAttributes.Public;
            if (implementedInterface != null)
            {
                property.ImplementationTypes.Add(implementedInterface);
            }

            if (summaryComment.IsNotNullOrEmpty())
            {
                property.Comments.Add(new CodeCommentStatement("<summary>" + summaryComment + "</summary>", true));
            }
            property.Type   = propertyType;
            property.HasGet = true;
            property.HasSet = !readOnly;

            // Add getter and setter.
            property.GetStatements.Add(new CodeMethodReturnStatement(fieldNameRef));
            if (property.HasSet)
            {
                property.SetStatements.Add(
                    new CodeAssignStatement(fieldNameRef, new CodePropertySetValueReferenceExpression()));
            }

            // Return the result.
            var col = new CodeTypeMemberCollection();

            col.Add(field);
            col.Add(property);
            return(col);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a backening field for the name provided.
        /// </summary>
        /// <param name="name">The name of the property.</param>
        public static CodeMemberField CreateBackingField(string name,
                                                         CodeTypeReference fieldType,
                                                         IEnumerable <string> usedWords)
        {
            // Validate parameters.
            name.ThrowIfNullOrEmpty("name");
            fieldType.ThrowIfNull("fieldType");
            usedWords.ThrowIfNull("usedWords");

            // Generate field name.
            var fieldName = GeneratorUtils.GetFieldName(name, usedWords);

            // Create the field.
            var field = new CodeMemberField(fieldType, fieldName);

            field.Attributes = MemberAttributes.Private;
            return(field);
        }