Exemplo n.º 1
0
        protected virtual CodeMemberProperty GenerateProperty(ResourceMapping mapping)
        {
            var resource     = mapping.Resource;
            var resourceType = resource.Type != null?Code.Type(resource.Type.Type).Local( ) : Code.Type <object> ( );

            var summary = resource.Type == TypeNames.String ?
                          Format(StringPropertySummary, GeneratePreview((string)resource.Value)) :
                          Format(NonStringPropertySummary, resource.Name);

            return(Declare.Property(resourceType, mapping.Property)
                   .Modifiers(settings.AccessModifiers)
                   .Get(get => get.Return(GenerateResourceGetter(resource)))
                   .AddSummary(summary + FormatResourceComment(resource.Comment)));
        }
Exemplo n.º 2
0
        protected virtual CodeMemberMethod GenerateFormatMethod(ResourceMapping mapping)
        {
            var resource          = mapping.Resource;
            var numberOfArguments = mapping.NumberOfArguments;

            if (numberOfArguments <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(numberOfArguments), numberOfArguments, "Number of argument must be greater than zero");
            }

            var localizer = (CodeExpression)null;

            if (settings.LocalizerType != null)
            {
                localizer = Code.Static( ).Property(LocalizerPropertyName);
            }

            var format           = Code.Type <string> ( ).Static( ).Method(nameof(string.Format));
            var formatExpression = (CodeExpression)Code.Instance(settings.AccessModifiers).Property(mapping.Property);

            if (localizer != null)
            {
                format           = localizer.Method(nameof(ILocalizer.Format));
                formatExpression = Code.Constant(resource.Name);
            }

            var summary      = Format(FormatMethodSummary, GeneratePreview((string)resource.Value));
            var formatMethod = Declare.Method <string> (mapping.FormatMethod, settings.AccessModifiers)
                               .AddSummary(summary + FormatResourceComment(resource.Comment));

            var objectType = Code.Type <object> ( );
            var start      = localizer != null ? 3 : 2;
            var parameters = new CodeExpression [start + numberOfArguments];

            parameters [0] = Code.Instance(settings.AccessModifiers).Field(CultureInfoFieldName);
            parameters [1] = formatExpression;
            if (localizer != null)
            {
                parameters [2] = parameters [1];
                parameters [1] = parameters [0];
            }

            for (var index = 0; index < numberOfArguments; index++)
            {
                var parameterName = Format(CultureInfo.InvariantCulture, FormatMethodParameterName, index);

                formatMethod.Parameters.Add(objectType.Parameter(parameterName));

                parameters [start + index] = Code.Variable(parameterName);

                if (numberOfArguments > 1)
                {
                    formatMethod.AddParameterComment(parameterName, FormatMultiParameterComment, Ordinals [Math.Min(index, Ordinals.Length - 1)]);
                }
                else
                {
                    formatMethod.AddParameterComment(parameterName, FormatParameterComment, index);
                }
            }

            if (numberOfArguments > 3)
            {
                formatMethod.Attributed(Declare.Attribute <SuppressMessageAttribute> ("Microsoft.Design", "CA1025:ReplaceRepetitiveArgumentsWithParamsArray"));
            }

            return(formatMethod.Define(method => method.Return(format.Invoke(parameters)))
                   .AddReturnComment(FormatReturnComment));
        }