Exemplo n.º 1
0
        /// <summary>
        /// Generates the syntax definition of an event in c# syntax.
        /// </summary>
        /// <example>
        /// With Keywords [security] [keywords] event [event handler type] [name];
        /// Without Keywords [security] [keywords] event [event handler type] [name];
        /// </example>
        /// <param name="source">The source <see cref="CsEvent"/> model to generate.</param>
        /// <param name="manager">Namespace manager used to format type names.This is an optional parameter.</param>
        /// <param name="includeSecurity">Includes the security scope which was defined in the model.</param>
        /// <param name="eventSecurity">Optional parameter that sets the target security scope for the event.</param>
        /// <param name="includeKeywords">Optional parameter that determines if it will include all keywords assigned to the source model, default is false.</param>
        /// <param name="includeAbstractKeyword">Optional parameter that determines if it 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>
        /// <returns>Fully formatted event definition or null if the event data could not be generated.</returns>
        public static string CSharpFormatEventDeclaration(this CsEvent source, NamespaceManager manager = null, bool includeSecurity = true, CsSecurity eventSecurity = CsSecurity.Unknown,
                                                          bool includeKeywords        = true, bool includeAbstractKeyword = false, bool requireStaticKeyword = false, bool requireSealedKeyword = false, bool requireAbstractKeyword = false,
                                                          bool requireOverrideKeyword = false, bool requireVirtualKeyword = false)
        {
            if (source == null)
            {
                return(null);
            }
            if (!source.IsLoaded)
            {
                return(null);
            }

            StringBuilder eventFormatting = new StringBuilder();

            CsSecurity security = eventSecurity == CsSecurity.Unknown
                ? security = source.Security
                : security = eventSecurity;

            if (includeKeywords & source.IsSealed)
            {
                eventFormatting.Append($"{Keywords.Sealed} ");
            }

            if (includeSecurity)
            {
                eventFormatting.Append($"{security.CSharpFormatKeyword()} ");
            }

            if (includeKeywords)
            {
                if (source.IsStatic)
                {
                    eventFormatting.Append($"{Keywords.Static} ");
                }
                if (includeAbstractKeyword & source.IsAbstract)
                {
                    eventFormatting.Append($"{Keywords.Abstract} ");
                }
                if (source.IsOverride)
                {
                    eventFormatting.Append($"{Keywords.Override} ");
                }
                if (source.IsVirtual)
                {
                    eventFormatting.Append($"{Keywords.Virtual} ");
                }
            }

            var signature = source.CSharpFormatEventSignature(manager, includeSecurity, security, includeKeywords,
                                                              includeAbstractKeyword, requireStaticKeyword, requireSealedKeyword, requireAbstractKeyword,
                                                              requireOverrideKeyword, requireVirtualKeyword);

            eventFormatting.Append($"{signature};");

            return(eventFormatting.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generates the initial definition portion of a property.
        /// </summary>
        /// <example>
        /// Format with Keywords [Security] [Keywords*] [ReturnType] [PropertyName] = public static string FirstName
        /// Format without Keywords [Security] [ReturnType] [PropertyName] = public string FirstName
        /// </example>
        /// <param name="manager">Namespace manager used to format type names.</param>
        /// <param name="source">The source property to use for formatting.</param>
        /// <param name="includeSecurity">Optional flag that determines if the security scope will be applied to the property definition. Default is true.</param>
        /// <param name="includeKeyWords">Optional flag that determines if keywords assigned to the property should be included in the signature. 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 to override the models security and set your own security.</param>
        /// <returns>The formatted signature or null if the model data was not loaded.</returns>
        public static string CSharpFormatPropertyDeclaration(this CsProperty source, NamespaceManager manager = null, bool includeSecurity = true, bool includeKeyWords = false,
                                                             bool includeAbstractKeyword = false, CsSecurity propertySecurity = CsSecurity.Unknown)
        {
            if (!source.IsLoaded)
            {
                return(null);
            }

            StringBuilder propertyBuilder = new StringBuilder();

            if (includeKeyWords & source.IsSealed)
            {
                propertyBuilder.Append($"{Keywords.Sealed} ");
            }

            if (includeSecurity)
            {
                propertyBuilder.Append(propertySecurity == CsSecurity.Unknown
                    ? source.Security.CSharpFormatKeyword()
                    : propertySecurity.CSharpFormatKeyword());
            }

            if (includeKeyWords)
            {
                if (source.IsStatic)
                {
                    propertyBuilder.Append($" {Keywords.Static}");
                }
                if (source.IsOverride)
                {
                    propertyBuilder.Append($" {Keywords.Override}");
                }
                if (source.IsAbstract & !source.IsOverride & includeAbstractKeyword)
                {
                    propertyBuilder.Append($" {Keywords.Abstract}");
                }
                if (source.IsVirtual & !source.IsOverride)
                {
                    propertyBuilder.Append($" {Keywords.Virtual}");
                }
            }

            propertyBuilder.Append($" {source.PropertyType.CSharpFormatTypeName(manager)}");
            propertyBuilder.Append($" {source.Name}");

            return(propertyBuilder.ToString());
        }
Exemplo n.º 3
0
        /// <summary>
        /// Extension method that formats the set statement of a property definition.
        /// </summary>
        /// <example>
        /// With the same security   [set] will return example: set
        /// With different security [security] [set] will return example: public set
        /// </example>
        /// <param name="source">the source property definition</param>
        /// <param name="propertySecurity">Optional parameter that defined the security used by the implementing property.</param>
        /// <param name="setSecurity">Optional parameter that allows you to set the set security level.</param>
        /// <returns>Will return the formatted set statement or null if the property model is empty or the property does not support set.</returns>
        public static string CSharpFormatSetStatement(this CsProperty source, CsSecurity propertySecurity = CsSecurity.Unknown,
                                                      CsSecurity setSecurity = CsSecurity.Unknown)
        {
            if (!source.IsLoaded)
            {
                return(null);
            }
            if (!source.HasSet & setSecurity == CsSecurity.Unknown)
            {
                return(null);
            }
            CsSecurity security = propertySecurity == CsSecurity.Unknown ? source.Security : propertySecurity;

            CsSecurity accessSecurity = setSecurity == CsSecurity.Unknown ? source.SetSecurity : setSecurity;

            return(security == accessSecurity ? "set" : $"{accessSecurity.CSharpFormatKeyword()} set");
        }
        /// <summary>
        /// Generates the syntax definition of field in c# syntax. The default definition with all options turned off will return the filed signature and constants if defined and the default values.
        /// </summary>
        /// <example>
        /// With Keywords [Security] [Keywords] [FieldType] [Name];
        /// With Keywords and a constant [Security] [Keywords] [FieldType] [Name] = [Constant Value];
        /// Without Keywords [Security] [FieldType] [Name];
        /// Without Keywords and a constant [Security] [FieldType] [Name] = [Constant Value];
        /// </example>
        /// <param name="source">The source <see cref="CsField"/> model to generate.</param>
        /// <param name="manager">Namespace manager used to format type names.This is an optional parameter.</param>
        /// <param name="includeKeywords">Optional parameter that will include all keywords assigned to the field from the source model. This is true by default.</param>
        /// <param name="fieldSecurity">Optional parameter to set the target security for the field.</param>
        /// <returns>Fully formatted field definition or null if the field data could not be generated.</returns>
        public static string CSharpFormatFieldDeclaration(this CsField source, NamespaceManager manager = null,
                                                          bool includeKeywords = true, CsSecurity fieldSecurity = CsSecurity.Unknown)

        {
            if (source == null)
            {
                return(null);
            }

            StringBuilder fieldFormatting = new StringBuilder();


            CsSecurity security = fieldSecurity == CsSecurity.Unknown ? source.Security : fieldSecurity;

            fieldFormatting.Append($"{security.CSharpFormatKeyword()} ");

            if (includeKeywords)
            {
                if (source.IsStatic)
                {
                    fieldFormatting.Append($"{Keywords.Static} ");
                }
                if (source.IsReadOnly)
                {
                    fieldFormatting.Append($"{Keywords.Readonly} ");
                }
            }

            if (source.IsConstant)
            {
                fieldFormatting.Append($"{Keywords.Constant} ");
            }
            fieldFormatting.Append($"{source.DataType.CSharpFormatTypeName(manager)} ");
            fieldFormatting.Append($"{source.Name}");
            if (source.IsConstant)
            {
                fieldFormatting.Append($" = {source.DataType.CSharpFormatValueSyntax(source.ConstantValue)}");
            }
            fieldFormatting.Append(";");

            return(fieldFormatting.ToString());
        }
        /// <summary>
        /// Extension method that generates a the full class declaration syntax based on the provided model.
        /// </summary>
        /// <example>
        /// Format with no generics [security] class [name] [:[base class*], [inherited interfaces*]]
        /// Format with generics [security] class [name] &lt;[generic parameters]&gt; [: [base class*], [inherited interfaces*]] [Generic Where Clauses*]
        /// </example>
        /// <param name="source">The source class model to format.</param>
        /// <param name="security">The security level the class should be implemented as.</param>
        /// <param name="manager">Namespace manager used to format type names.This is an optional parameter.</param>
        /// <param name="className">Optional parameter that allows you to specify a new name for the class.</param>
        /// <returns>The full class declaration or null if model data was missing.</returns>
        public static string CSharpFormatDeclaration(this CsClass source, CsSecurity security, NamespaceManager manager = null,
                                                     string className = null)
        {
            if (source == null)
            {
                return(null);
            }
            if (!source.IsLoaded)
            {
                return(null);
            }
            bool          hasBaseClass = false;
            var           name         = className ?? source.Name;
            StringBuilder classBuilder = new StringBuilder($"{security.CSharpFormatKeyword()} {Keywords.Class} {name}");

            if (source.IsGeneric)
            {
                classBuilder.Append(source.GenericParameters.CSharpFormatGenericParametersSignature(manager));
            }

            if (source.BaseClass != null)
            {
                if (source.BaseClass.Namespace.ToLowerInvariant() == "system" &
                    source.BaseClass.Name.ToLowerInvariant() == "object")
                {
                    hasBaseClass = false;
                }
                else
                {
                    hasBaseClass = true;
                }
            }

            if (hasBaseClass)
            {
                var baseName = source.BaseClass.CSharpFormatBaseTypeName(manager);
                if (string.IsNullOrEmpty(baseName))
                {
                    return(null);
                }
                classBuilder.Append($":{baseName}");
            }

            if (source.InheritedInterfaces.Any())
            {
                var interfaces = source.InheritedInterfaces;

                int totalCount       = interfaces.Count;
                int currentInterface = 0;

                classBuilder.Append(hasBaseClass ? ", " :":");

                foreach (var csInterface in interfaces)
                {
                    currentInterface++;

                    var interfaceType = csInterface.CSharpFormatInheritanceTypeName(manager);

                    if (interfaceType == null)
                    {
                        continue;
                    }

                    classBuilder.Append(interfaceType);
                    if (totalCount > currentInterface)
                    {
                        classBuilder.Append(", ");
                    }
                }
            }

            if (source.IsGeneric)
            {
                classBuilder.Append(" ");

                foreach (var sourceGenericParameter in source.GenericParameters)
                {
                    var whereClause = sourceGenericParameter.CSharpFormatGenericWhereClauseSignature(manager);
                    if (string.IsNullOrEmpty(whereClause))
                    {
                        continue;
                    }

                    classBuilder.Append($"{whereClause} ");
                }
            }

            return(classBuilder.ToString());
        }
Exemplo n.º 6
0
        /// <summary>
        /// Generates the syntax definition of field in c# syntax. The default definition with all options turned off will return the filed signature and constants if defined and the default values.
        /// </summary>
        /// <example>
        /// With Keywords [Security] [Keywords] [FieldType] [Name];
        /// With Keywords and a constant [Security] [Keywords] [FieldType] [Name] = [Constant Value];
        /// Without Keywords [Security] [FieldType] [Name];
        /// Without Keywords and a constant [Security] [FieldType] [Name] = [Constant Value];
        /// </example>
        /// <param name="source">The source <see cref="CsField"/> model to generate.</param>
        /// <param name="manager">Namespace manager used to format type names.This is an optional parameter.</param>
        /// <param name="includeKeywords">Optional parameter that will include all keywords assigned to the field from the source model. This is true by default.</param>
        /// <param name="fieldSecurity">Optional parameter to set the target security for the field.</param>
        /// <param name="implementConstant">Determines if the filed is implemented as constant is should be returned as a constant, default is true.</param>
        /// <param name="requireStaticKeyword">Adds the static keyword to the signature, default is false.</param>
        /// <param name="requireReadOnlyKeyword">Adds the readonly keyword to the signature, default is false.</param>
        /// <param name="requireConstant">Implements the field as a constant, default is false.</param>
        /// <param name="requireConstantValue">The value to set the constant to if required.</param>
        /// <returns>Fully formatted field definition or null if the field data could not be generated.</returns>
        public static string CSharpFormatFieldDeclaration(this CsField source, NamespaceManager manager = null,
                                                          bool includeKeywords        = true, CsSecurity fieldSecurity     = CsSecurity.Unknown, bool implementConstant = true,
                                                          bool requireStaticKeyword   = false, bool requireReadOnlyKeyword = false, bool requireConstant                = false,
                                                          string requireConstantValue = null)
        {
            if (source == null)
            {
                return(null);
            }

            StringBuilder fieldFormatting = new StringBuilder();


            CsSecurity security = fieldSecurity == CsSecurity.Unknown ? source.Security : fieldSecurity;

            fieldFormatting.Append($"{security.CSharpFormatKeyword()} ");

            string constantValue = null;

            bool staticKeyword   = false;
            bool readOnlyKeyword = false;
            bool constantKeyword = false;

            if (includeKeywords)
            {
                if (source.IsStatic)
                {
                    staticKeyword = true;
                }
                if (source.IsReadOnly)
                {
                    readOnlyKeyword = true;
                }
            }

            if (source.IsConstant & implementConstant)
            {
                constantKeyword = true;
                constantValue   = source.ConstantValue;
            }

            if (!staticKeyword)
            {
                staticKeyword = requireStaticKeyword;
            }
            if (!readOnlyKeyword)
            {
                readOnlyKeyword = requireReadOnlyKeyword;
            }
            if (!constantKeyword)
            {
                constantKeyword = requireConstant;
            }

            if (constantKeyword & string.IsNullOrEmpty(constantValue))
            {
                constantValue = requireConstantValue;
            }


            if (staticKeyword)
            {
                fieldFormatting.Append($"{Keywords.Static} ");
            }
            if (readOnlyKeyword)
            {
                fieldFormatting.Append($"{Keywords.Readonly} ");
            }
            if (constantKeyword)
            {
                fieldFormatting.Append($"{Keywords.Constant} ");
            }

            fieldFormatting.Append($"{source.DataType.CSharpFormatTypeName(manager)} ");
            fieldFormatting.Append($"{source.Name}");
            if (constantKeyword)
            {
                fieldFormatting.Append($" = {source.DataType.CSharpFormatValueSyntax(constantValue)}");
            }
            fieldFormatting.Append(";");

            return(fieldFormatting.ToString());
        }
        /// <summary>
        /// Extension method that generates a the full interface declaration syntax based on the provided model.
        /// </summary>
        /// <example>
        /// Format with no generics [security] interface [name] [:[inherited interfaces*]]
        /// Format with generics [security] interface [name] &lt;[generic parameters]&gt; [: [base class*], [inherited interfaces*]] [Generic Where Clauses*]
        /// </example>
        /// <param name="source">The source interface model to format.</param>
        /// <param name="security">The security level the interface should be implemented as.</param>
        /// <param name="manager">Namespace manager used to format type names.This is an optional parameter.</param>
        /// <param name="interfaceName">Optional parameter that allows you to specify a new name for the interface.</param>
        /// <returns>The full interface declaration or null if model data was missing.</returns>
        public static string CSharpFormatDeclaration(this CsInterface source, CsSecurity security, NamespaceManager manager = null,
                                                     string interfaceName = null)
        {
            if (source == null)
            {
                return(null);
            }
            if (!source.IsLoaded)
            {
                return(null);
            }

            var           name             = interfaceName ?? source.Name;
            StringBuilder interfaceBuilder = new StringBuilder($"{security.CSharpFormatKeyword()} {Keywords.Interface} {name}");

            if (source.IsGeneric)
            {
                interfaceBuilder.Append(source.GenericParameters.CSharpFormatGenericParametersSignature(manager));
            }


            if (source.InheritedInterfaces.Any())
            {
                var interfaces = source.InheritedInterfaces;

                int totalCount       = interfaces.Count;
                int currentInterface = 0;

                interfaceBuilder.Append(": ");
                foreach (var csInterface in interfaces)
                {
                    currentInterface++;

                    var interfaceType = csInterface.CSharpFormatInheritanceTypeName(manager);

                    if (interfaceType == null)
                    {
                        continue;
                    }

                    interfaceBuilder.Append(interfaceType);
                    if (totalCount > currentInterface)
                    {
                        interfaceBuilder.Append(", ");
                    }
                }
            }

            if (source.IsGeneric)
            {
                interfaceBuilder.Append(" ");

                foreach (var sourceGenericParameter in source.GenericParameters)
                {
                    var whereClause = sourceGenericParameter.CSharpFormatGenericWhereClauseSignature(manager);
                    if (string.IsNullOrEmpty(whereClause))
                    {
                        continue;
                    }

                    interfaceBuilder.Append($"{whereClause} ");
                }
            }

            return(interfaceBuilder.ToString());
        }
Exemplo n.º 8
0
        /// <summary>
        /// Generates a C# method signature from model data. This provides a fully customizable method for generating the signature.
        /// </summary>
        /// <param name="source">The source method data to generate the signature from.</param>
        /// <param name="includeAsyncKeyword">Include the async keyword if the return type is Task</param>
        /// <param name="includeSecurity">Includes the security scope which was defined in the model.</param>
        /// <param name="methodSecurity">Optional parameter that allows you to set the security scope for the method.</param>
        /// <param name="includeKeywords">Includes all keywords assigned to the source model.</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="manager">Optional parameter that contains all the using statements from the source code, when used will replace namespaces on type definition in code.</param>
        /// <returns>Fully formatted method deceleration or null if the method data was missing.</returns>
        public static string CSharpFormatMethodSignature(this CsMethod source, NamespaceManager manager = null, bool includeAsyncKeyword = true,
                                                         bool includeSecurity = true, CsSecurity methodSecurity = CsSecurity.Unknown, bool includeKeywords = true, bool includeAbstractKeyword = false)
        {
            if (source == null)
            {
                return(null);
            }


            StringBuilder methodFormatting = new StringBuilder();

            if (includeSecurity)
            {
                var formattedSecurity = methodSecurity == CsSecurity.Unknown
                    ? source.Security.CSharpFormatKeyword()
                    : methodSecurity.CSharpFormatKeyword();
                methodFormatting.Append($"{formattedSecurity} ");
            }

            if (includeKeywords)
            {
                if (source.IsStatic)
                {
                    methodFormatting.Append($"{Keywords.Static} ");
                }
                if (source.IsSealed)
                {
                    methodFormatting.Append($"{Keywords.Sealed} ");
                }
                if (includeAbstractKeyword & source.IsAbstract)
                {
                    methodFormatting.Append($"{Keywords.Abstract} ");
                }
                if (source.IsOverride)
                {
                    methodFormatting.Append($"{Keywords.Override} ");
                }
                if (source.IsVirtual)
                {
                    methodFormatting.Append($"{Keywords.Virtual} ");
                }
            }

            if (includeAsyncKeyword)
            {
                //Bug fix for issue #14
                if (source.ReturnType != null)
                {
                    if (source.ReturnType.Name == "Task" & source.ReturnType.Namespace == "System.Threading.Tasks")
                    {
                        methodFormatting.Append("async ");
                    }
                }
            }

            methodFormatting.Append(source.IsVoid ? $"{Keywords.Void} {source.Name}" : $"{source.ReturnType.CSharpFormatTypeName(manager)} {source.Name}");
            if (source.IsGeneric)
            {
                methodFormatting.Append($"{source.GenericParameters.CSharpFormatGenericParametersSignature(manager)}");
            }

            methodFormatting.Append(source.HasParameters
                ? source.Parameters.CSharpFormatParametersSignature(manager)
                : $"{Symbols.ParametersDefinitionStart}{Symbols.ParametersDefinitionEnd}");

            if (!source.IsGeneric)
            {
                return(methodFormatting.ToString());
            }

            foreach (var sourceGenericParameter in source.GenericParameters)
            {
                var whereClause = sourceGenericParameter.CSharpFormatGenericWhereClauseSignature(manager);

                if (!string.IsNullOrEmpty(whereClause))
                {
                    methodFormatting.Append($" {whereClause}");
                }
            }

            return(methodFormatting.ToString());
        }
        /// <summary>
        /// Generates the initial definition portion of a property.
        /// </summary>
        /// <example>
        /// Format with Keywords [Security] [Keywords*] [ReturnType] [PropertyName] = public static string FirstName
        /// Format without Keywords [Security] [ReturnType] [PropertyName] = public string FirstName
        /// </example>
        /// <param name="manager">Namespace manager used to format type names.</param>
        /// <param name="source">The source property to use for formatting.</param>
        /// <param name="includeSecurity">Optional flag that determines if the security scope will be applied to the property definition. Default is true.</param>
        /// <param name="includeKeyWords">Optional flag that determines if keywords assigned to the property should be included in the signature. 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 to override the models security and set your own security.</param>
        /// <returns>The formatted signature or null if the model data was not loaded.</returns>
        public static string CSharpFormatPropertyDeclaration(this CsProperty source, NamespaceManager manager = null, bool includeSecurity = true, bool includeKeyWords = false,
                                                             bool includeAbstractKeyword = false, bool requireStaticKeyword   = false, bool requireSealedKeyword        = false, bool requireAbstractKeyword = false, bool requireOverrideKeyword = false,
                                                             bool requireVirtualKeyword  = false, CsSecurity propertySecurity = CsSecurity.Unknown)
        {
            if (!source.IsLoaded)
            {
                return(null);
            }

            StringBuilder propertyBuilder = new StringBuilder();

            if (includeKeyWords & source.IsSealed)
            {
                propertyBuilder.Append($"{Keywords.Sealed} ");
            }

            if (includeSecurity)
            {
                propertyBuilder.Append(propertySecurity == CsSecurity.Unknown
                    ? source.Security.CSharpFormatKeyword()
                    : propertySecurity.CSharpFormatKeyword());
            }

            bool staticKeyword   = false;
            bool sealedKeyword   = false;
            bool abstractKeyword = false;
            bool overrideKeyword = false;
            bool virtualKeyword  = false;

            if (includeKeyWords)
            {
                if (source.IsStatic)
                {
                    staticKeyword = true;
                }
                if (source.IsSealed)
                {
                    sealedKeyword = true;
                }
                if (includeAbstractKeyword & source.IsAbstract)
                {
                    abstractKeyword = true;
                }
                if (source.IsOverride)
                {
                    overrideKeyword = true;
                }
                if (source.IsVirtual)
                {
                    virtualKeyword = true;
                }
            }

            if (!staticKeyword)
            {
                staticKeyword = requireStaticKeyword;
            }
            if (!sealedKeyword)
            {
                sealedKeyword = requireSealedKeyword;
            }
            if (!abstractKeyword)
            {
                abstractKeyword = requireAbstractKeyword;
            }
            if (!overrideKeyword)
            {
                overrideKeyword = requireOverrideKeyword;
            }
            if (!virtualKeyword)
            {
                virtualKeyword = requireVirtualKeyword;
            }

            if (staticKeyword)
            {
                propertyBuilder.Append($"{Keywords.Static} ");
            }
            if (sealedKeyword)
            {
                propertyBuilder.Append($"{Keywords.Sealed} ");
            }
            if (abstractKeyword)
            {
                propertyBuilder.Append($"{Keywords.Abstract} ");
            }
            if (overrideKeyword)
            {
                propertyBuilder.Append($"{Keywords.Override} ");
            }
            if (virtualKeyword)
            {
                propertyBuilder.Append($"{Keywords.Virtual} ");
            }


            propertyBuilder.Append($" {source.PropertyType.CSharpFormatTypeName(manager)}");
            propertyBuilder.Append($" {source.Name}");

            return(propertyBuilder.ToString());
        }
Exemplo n.º 10
0
        /// <summary>
        /// Generates the source code for a standard raise event method.
        /// </summary>
        /// <param name="eventData">The target event model to generate the raise method for.</param>
        /// <param name="manager">The namespace manager to manage type naming.</param>
        /// <param name="eventName">the name of implemented event to raise.</param>
        /// <param name="security">The target security level to set the event to.</param>
        /// <param name="eventHandler">Optional field, that provides the name of the direct event handler field to raise.</param>
        /// <param name="methodName">Optional parameter of the custom name of the method. If not set will be On[eventName]</param>
        /// <returns></returns>
        public static string GenerateStandardRaiseEventMethod(CsEvent eventData, NamespaceManager manager,
                                                              string eventName, CsSecurity security, string eventHandler = null, string methodName = null)
        {
            //Bounds checking to make sure all data that is needed is provided. If any required data is missing will return null.
            if (eventData == null)
            {
                return(null);
            }
            if (!eventData.IsLoaded)
            {
                return(null);
            }
            if (manager == null)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(eventName))
            {
                return(null);
            }
            if (security == CsSecurity.Unknown)
            {
                return(null);
            }

            var raiseMethod = eventData.EventHandlerDelegate.InvokeMethod;

            if (raiseMethod == null)
            {
                return(null);
            }
            if (!raiseMethod.IsLoaded)
            {
                return(null);
            }

            var parameters = raiseMethod.Parameters.CSharpFormatParametersSignature(manager, false);

            string raiseHandler = string.IsNullOrEmpty(eventHandler) ? eventName : eventHandler;

            StringBuilder parametersBuilder = new StringBuilder();
            int           parameterCount    = 0;

            foreach (var raiseMethodParameter in raiseMethod.Parameters)
            {
                parameterCount++;
                if (parameterCount > 1)
                {
                    parametersBuilder.Append(", ");
                }

                parametersBuilder.Append(raiseMethodParameter.Name);
            }


            //C# helper used to format output syntax.
            var formatter = new SourceFormatter();


            string raiseMethodName = string.IsNullOrEmpty(methodName) ? $"On{eventName}" : methodName;

            formatter.AppendCodeLine(0, "/// <summary>");
            formatter.AppendCodeLine(0, $"/// Raises the event {eventName} when there are subscribers to the event.");
            formatter.AppendCodeLine(0, "/// </summary>");
            formatter.AppendCodeLine(0, $"{security.CSharpFormatKeyword()} {Keywords.Void} {raiseMethodName}{parameters}");
            formatter.AppendCodeLine(0, "{");
            formatter.AppendCodeLine(0);
            formatter.AppendCodeLine(1, $"var raiseEvent = {raiseHandler};");
            formatter.AppendCodeLine(1, $"raiseEvent?.Invoke({parametersBuilder});");
            formatter.AppendCodeLine(0);
            formatter.AppendCodeLine(0, "}");
            formatter.AppendCodeLine(0);

            return(formatter.ReturnSource());
        }
Exemplo n.º 11
0
        /// <summary>
        /// Generates the signature of an event in c# syntax.
        /// </summary>
        /// <example>
        /// With Keywords [security] [keywords] event [event handler type] [name]
        /// Without Keywords [security] [keywords] event [event handler type] [name]
        /// </example>
        /// <param name="source">The source <see cref="CsEvent"/> model to generate.</param>
        /// <param name="manager">Namespace manager used to format type names.This is an optional parameter.</param>
        /// <param name="includeSecurity">Includes the security scope which was defined in the model.</param>
        /// <param name="eventSecurity">Optional parameter that sets the target security scope for the event.</param>
        /// <param name="includeKeywords">Optional parameter that determines if it will include all keywords assigned to the source model, default is false.</param>
        /// <param name="includeAbstractKeyword">Optional parameter that determines if it 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>
        /// <returns>Fully formatted event definition or null if the event data could not be generated.</returns>
        public static string CSharpFormatEventSignature(this CsEvent source, NamespaceManager manager = null, bool includeSecurity = true, CsSecurity eventSecurity = CsSecurity.Unknown,
                                                        bool includeKeywords       = true, bool includeAbstractKeyword = false, bool requireStaticKeyword = false, bool requireSealedKeyword = false, bool requireAbstractKeyword = false, bool requireOverrideKeyword = false,
                                                        bool requireVirtualKeyword = false)
        {
            if (source == null)
            {
                return(null);
            }
            if (!source.IsLoaded)
            {
                return(null);
            }

            StringBuilder eventFormatting = new StringBuilder();

            CsSecurity security = eventSecurity == CsSecurity.Unknown
                ? security = source.Security
                : security = eventSecurity;

            if (includeKeywords & source.IsSealed)
            {
                eventFormatting.Append($"{Keywords.Sealed} ");
            }

            if (includeSecurity)
            {
                eventFormatting.Append($"{security.CSharpFormatKeyword()} ");
            }

            bool staticKeyword   = false;
            bool sealedKeyword   = false;
            bool abstractKeyword = false;
            bool overrideKeyword = false;
            bool virtualKeyword  = false;

            if (includeKeywords)
            {
                if (source.IsStatic)
                {
                    staticKeyword = true;
                }
                if (source.IsSealed)
                {
                    sealedKeyword = true;
                }
                if (includeAbstractKeyword & source.IsAbstract)
                {
                    abstractKeyword = true;
                }
                if (source.IsOverride)
                {
                    overrideKeyword = true;
                }
                if (source.IsVirtual)
                {
                    virtualKeyword = true;
                }
            }

            if (!staticKeyword)
            {
                staticKeyword = requireStaticKeyword;
            }
            if (!sealedKeyword)
            {
                sealedKeyword = requireSealedKeyword;
            }
            if (!abstractKeyword)
            {
                abstractKeyword = requireAbstractKeyword;
            }
            if (!overrideKeyword)
            {
                overrideKeyword = requireOverrideKeyword;
            }
            if (!virtualKeyword)
            {
                virtualKeyword = requireVirtualKeyword;
            }

            if (staticKeyword)
            {
                eventFormatting.Append($"{Keywords.Static} ");
            }
            if (sealedKeyword)
            {
                eventFormatting.Append($"{Keywords.Sealed} ");
            }
            if (abstractKeyword)
            {
                eventFormatting.Append($"{Keywords.Abstract} ");
            }
            if (overrideKeyword)
            {
                eventFormatting.Append($"{Keywords.Override} ");
            }
            if (virtualKeyword)
            {
                eventFormatting.Append($"{Keywords.Virtual} ");
            }

            eventFormatting.Append($"{Keywords.Event} {source.EventType.CSharpFormatTypeName(manager)} {source.Name}");

            return(eventFormatting.ToString());
        }