/// <summary>
        /// Generates a default property definition with a backing field. Will determine security modifiers and append to get and set statements when needed.
        /// </summary>
        /// <example>
        /// With Keywords [security] [keywords] [property type] [property name] { [get when used] => [backingField]; [set when used] =>[backingField] = value;  }
        /// Without Keywords [security] [property type] [property name] { [get when used] => [backingField]; [set when used] =>[backingField] = value;  }
        /// </example>
        /// <param name="source">Property model used for generation.</param>
        /// <param name="backingFieldName">the name of the backing field to be managed by the property.</param>
        /// <param name="manager">Namespace manager used to format type names.</param>
        /// <param name="includeKeyword">Optional parameter that determines if the keywords will be appended. Default is false.</param>
        /// <param name="includeAbstractKeyword">Will include the definition for the abstract keyword in the definition if it is defined. default is false.</param>
        /// <param name="requireStaticKeyword">Adds the static keyword to the signature, default is false.</param>
        /// <param name="requireSealedKeyword">Adds the sealed keyword to the signature, default is false.</param>
        /// <param name="requireAbstractKeyword">Adds the abstract keyword to the signature, default is false.</param>
        /// <param name="requireOverrideKeyword">Adds the override keyword to the signature, default is false.</param>
        /// <param name="requireVirtualKeyword">Adds the virtual keyword to the signature, default is false.</param>
        /// <param name="propertySecurity">Optional parameter that overrides the models property security and sets a new security access level.</param>
        /// <param name="setSecurity">Optional parameter that overrides the models set security level with a new access level. Will also define a set statement even if it is not defined.</param>
        /// <param name="getSecurity">Optional parameter that overrides the models get security level with a new access level. Will also define a get statement even if it is not defined.</param>
        /// <returns>Formatted property or null if model data was missing.</returns>
        public static string CSharpFormatDefaultExpressionBodyPropertySignatureWithBackingField(this CsProperty source,
                                                                                                string backingFieldName, NamespaceManager manager = null, bool includeKeyword = false,
                                                                                                bool includeAbstractKeyword = false, bool requireStaticKeyword           = false, bool requireSealedKeyword  = false,
                                                                                                bool requireAbstractKeyword = false, bool requireOverrideKeyword         = false, bool requireVirtualKeyword = false,
                                                                                                CsSecurity propertySecurity = CsSecurity.Unknown, CsSecurity setSecurity = CsSecurity.Unknown,
                                                                                                CsSecurity getSecurity      = CsSecurity.Unknown)
        {
            if (!source.IsLoaded)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(backingFieldName))
            {
                return(null);
            }

            string propertyDeclaration = CSharpFormatPropertyDeclaration(source, manager, true, includeKeyword,
                                                                         includeAbstractKeyword, requireStaticKeyword, requireSealedKeyword, requireAbstractKeyword,
                                                                         requireOverrideKeyword, requireVirtualKeyword, propertySecurity);

            if (string.IsNullOrEmpty(propertyDeclaration))
            {
                return(null);
            }

            StringBuilder propertyDefinition = new StringBuilder($"{propertyDeclaration} {{ ");

            if (source.HasGet | getSecurity != CsSecurity.Unknown)
            {
                var getStatement = source.CSharpFormatGetStatement(propertySecurity, getSecurity);
                if (getStatement == null)
                {
                    return(null);
                }
                propertyDefinition.Append($"{getStatement} => {backingFieldName}; ");
            }

            if (source.HasSet | setSecurity != CsSecurity.Unknown)
            {
                var setStatement = source.CSharpFormatSetStatement(propertySecurity, setSecurity);
                if (setStatement == null)
                {
                    return(null);
                }
                propertyDefinition.Append($"{setStatement} => {backingFieldName} = value; ");
            }

            propertyDefinition.Append("}");

            return(propertyDefinition.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// Generates a default property definition with no backing properties. Will determine security modifiers and append to get and set statements when needed.
        /// </summary>
        /// <example>
        /// With Keywords [security] [keywords] [property type] [property name] { [get when used]; [set when used]; }
        /// No Keywords [security] [property type] [property name] { [get when used]; [set when used]; }
        /// </example>
        /// <param name="source">Property model used for generation.</param>
        /// <param name="manager">Namespace manager used to format type names.This is an optional parameter.</param>
        /// <param name="includeKeyword">Optional parameter that determines if the keywords will be appended. Default is false.</param>
        /// <param name="includeAbstractKeyword">Will include the definition for the abstract keyword in the definition if it is defined. default is false.</param>
        /// <param name="propertySecurity">Optional parameter that overrides the models property security and sets a new security access level.</param>
        /// <param name="setSecurity">Optional parameter that overrides the models set security level with a new access level. Will also define a set statement even if it is not defined.</param>
        /// <param name="getSecurity">Optional parameter that overrides the models get security level with a new access level. Will also define a get statement even if it is not defined.</param>
        /// <returns>Formatted property or null if model data was missing.</returns>
        public static string CSharpFormatDefaultPropertySignature(this CsProperty source, NamespaceManager manager = null, bool includeKeyword = false,
                                                                  bool includeAbstractKeyword = false, CsSecurity propertySecurity             = CsSecurity.Unknown, CsSecurity setSecurity = CsSecurity.Unknown,
                                                                  CsSecurity getSecurity      = CsSecurity.Unknown)
        {
            if (!source.IsLoaded)
            {
                return(null);
            }
            string propertyDeclaration = CSharpFormatPropertyDeclaration(source, manager, true, includeKeyword, includeAbstractKeyword,
                                                                         propertySecurity);

            if (string.IsNullOrEmpty(propertyDeclaration))
            {
                return(null);
            }

            StringBuilder propertyDefinition = new StringBuilder($"{propertyDeclaration} {{ ");

            if (source.HasGet | getSecurity != CsSecurity.Unknown)
            {
                var getStatement = source.CSharpFormatGetStatement(propertySecurity, getSecurity);
                if (getStatement == null)
                {
                    return(null);
                }
                propertyDefinition.Append($"{getStatement}; ");
            }

            if (source.HasSet | setSecurity != CsSecurity.Unknown)
            {
                var setStatement = source.CSharpFormatSetStatement(propertySecurity, setSecurity);
                if (setStatement == null)
                {
                    return(null);
                }
                propertyDefinition.Append($"{setStatement}; ");
            }

            propertyDefinition.Append("}");

            return(propertyDefinition.ToString());
        }