Ejemplo n.º 1
0
        public static NonEscapedString Render(Job job)
        {
            if (job == null)
            {
                return(new NonEscapedString("<em>Can not find the target method.</em>"));
            }

            var builder = new StringBuilder();

            builder.Append(WrapKeyword("using"));
            builder.Append(" ");
            builder.Append(Encode(job.Type.Namespace));
            builder.Append(";");
            builder.AppendLine();
            builder.AppendLine();

            if (!job.Method.IsStatic)
            {
                var serviceName = GetNameWithoutGenericArity(job.Type);

                if (job.Type.IsInterface && serviceName[0] == 'I' && Char.IsUpper(serviceName[1]))
                {
                    serviceName = serviceName.Substring(1);
                }

                serviceName = Char.ToLower(serviceName[0]) + serviceName.Substring(1);

                builder.Append(WrapType(job.Type.ToGenericTypeString()));
                builder.AppendFormat(
                    " {0} = Activate<{1}>();",
                    Encode(serviceName),
                    WrapType(Encode(job.Type.ToGenericTypeString())));

                builder.AppendLine();

                builder.Append(Encode(serviceName));
            }
            else
            {
                builder.Append(WrapType(Encode(job.Type.ToGenericTypeString())));
            }

            builder.Append(".");
            builder.Append(Encode(job.Method.Name));
            builder.Append("(");

            var parameters                   = job.Method.GetParameters();
            var renderedArguments            = new List <string>(parameters.Length);
            var renderedArgumentsTotalLength = 0;

            const int splitStringMinLength = 100;

            for (var i = 0; i < parameters.Length; i++)
            {
                var parameter = parameters[i];

                if (i < job.Arguments.Length)
                {
                    var    argument = job.Arguments[i]; // TODO: check bounds
                    string renderedArgument;

                    var enumerableArgument = GetIEnumerableGenericArgument(parameter.ParameterType);

                    object argumentValue;
                    bool   isJson = true;

                    try
                    {
                        argumentValue = JobHelper.FromJson(argument, parameter.ParameterType);
                    }
                    catch (Exception)
                    {
                        // If argument value is not encoded as JSON (an old
                        // way using TypeConverter), we should display it as is.
                        argumentValue = argument;
                        isJson        = false;
                    }

                    if (argumentValue == null)
                    {
                        renderedArgument = WrapKeyword("null");
                    }
                    else
                    {
                        if (enumerableArgument == null)
                        {
                            var argumentRenderer = ArgumentRenderer.GetRenderer(parameter.ParameterType);
                            renderedArgument = argumentRenderer.Render(isJson, argumentValue.ToString(), argument);
                        }
                        else
                        {
                            var renderedItems = new List <string>();

                            foreach (var item in (IEnumerable)argumentValue)
                            {
                                var argumentRenderer = ArgumentRenderer.GetRenderer(enumerableArgument);
                                renderedItems.Add(argumentRenderer.Render(isJson, item.ToString(),
                                                                          JobHelper.ToJson(item)));
                            }

                            renderedArgument = String.Format(
                                WrapKeyword("new") + "{0} {{ {1} }}",
                                parameter.ParameterType.IsArray ? " []" : "",
                                String.Join(", ", renderedItems));
                        }
                    }

                    renderedArguments.Add(renderedArgument);
                    renderedArgumentsTotalLength += renderedArgument.Length;
                }
                else
                {
                    renderedArguments.Add(Encode("<NO VALUE>"));
                }
            }

            for (int i = 0; i < renderedArguments.Count; i++)
            {
                // TODO: be aware of out of range
                var parameter       = parameters[i];
                var tooltipPosition = "top";

                var renderedArgument = renderedArguments[i];
                if (renderedArgumentsTotalLength > splitStringMinLength)
                {
                    builder.AppendLine();
                    builder.Append("    ");

                    tooltipPosition = "left";
                }
                else if (i > 0)
                {
                    builder.Append(" ");
                }

                builder.AppendFormat(
                    "<span title=\"{0}:\" data-placement=\"{1}\">",
                    parameter.Name,
                    tooltipPosition);

                builder.Append(renderedArgument);

                builder.Append("</span>");

                if (i < renderedArguments.Count - 1)
                {
                    builder.Append(",");
                }
            }

            builder.Append(");");

            return(new NonEscapedString(builder.ToString()));
        }
Ejemplo n.º 2
0
        public static NonEscapedString Render(Job job)
        {
            if (job == null)
            {
                return(new NonEscapedString($"<em>{Encode(Strings.Common_CannotFindTargetMethod)}</em>"));
            }

            var builder = new StringBuilder();

            builder.Append(WrapKeyword("using"));
            builder.Append(" ");
            builder.Append(Encode(job.Type.Namespace));
            builder.Append(";");
            builder.AppendLine();
            builder.AppendLine();

            string serviceName = null;

            if (!job.Method.IsStatic)
            {
                serviceName = GetNameWithoutGenericArity(job.Type);

                if (job.Type.GetTypeInfo().IsInterface&& serviceName[0] == 'I' && Char.IsUpper(serviceName[1]))
                {
                    serviceName = serviceName.Substring(1);
                }

                serviceName = Char.ToLower(serviceName[0]) + serviceName.Substring(1);

                builder.Append(WrapKeyword("var"));
                builder.Append(
                    $" {Encode(serviceName)} = Activate&lt;{WrapType(Encode(job.Type.ToGenericTypeString()))}&gt;();");

                builder.AppendLine();
            }

            if (job.Method.GetCustomAttribute <AsyncStateMachineAttribute>() != null ||
                job.Method.ReturnType.IsTaskLike(out _))
            {
                builder.Append($"{WrapKeyword("await")} ");
            }

            builder.Append(!job.Method.IsStatic ? Encode(serviceName) : WrapType(Encode(job.Type.ToGenericTypeString())));

            builder.Append(".");
            builder.Append(Encode(job.Method.Name));

            if (job.Method.IsGenericMethod)
            {
                var genericArgumentTypes = job.Method.GetGenericArguments()
                                           .Select(x => WrapType(x.Name))
                                           .ToArray();

                builder.Append($"&lt;{String.Join(", ", genericArgumentTypes)}&gt;");
            }

            builder.Append("(");

            var parameters                   = job.Method.GetParameters();
            var renderedArguments            = new List <string>(parameters.Length);
            var renderedArgumentsTotalLength = 0;

            const int splitStringMinLength = 100;

            for (var i = 0; i < parameters.Length; i++)
            {
                var parameter = parameters[i];
#pragma warning disable 618
                var arguments = job.Arguments;
#pragma warning restore 618

                if (i < arguments.Length)
                {
                    var argument = arguments[i];

                    if (argument != null && argument.Length > MaxArgumentToRenderSize)
                    {
                        renderedArguments.Add(Encode("<VALUE IS TOO BIG>"));
                        continue;
                    }

                    string renderedArgument;

                    var enumerableArgument = GetIEnumerableGenericArgument(parameter.ParameterType);

                    object argumentValue;
                    bool   isJson = true;

                    try
                    {
                        argumentValue = SerializationHelper.Deserialize(argument, parameter.ParameterType, SerializationOption.User);
                    }
                    catch (Exception)
                    {
                        // If argument value is not encoded as JSON (an old
                        // way using TypeConverter), we should display it as is.
                        argumentValue = argument;
                        isJson        = false;
                    }

                    if (enumerableArgument == null || argumentValue == null)
                    {
                        var argumentRenderer = ArgumentRenderer.GetRenderer(parameter.ParameterType);
                        renderedArgument = argumentRenderer.Render(isJson, argumentValue?.ToString(), argument);
                    }
                    else
                    {
                        var renderedItems = new List <string>();

                        // ReSharper disable once LoopCanBeConvertedToQuery
                        foreach (var item in (IEnumerable)argumentValue)
                        {
                            var argumentRenderer = ArgumentRenderer.GetRenderer(enumerableArgument);
                            renderedItems.Add(argumentRenderer.Render(isJson, item?.ToString(),
                                                                      SerializationHelper.Serialize(item, SerializationOption.User)));
                        }

                        // ReSharper disable once UseStringInterpolation
                        renderedArgument = String.Format(
                            "{0}{1} {{ {2} }}",
                            WrapKeyword("new"),
                            parameter.ParameterType.IsArray ? " []" : "",
                            String.Join(", ", renderedItems));
                    }

                    renderedArguments.Add(renderedArgument);
                    renderedArgumentsTotalLength += renderedArgument.Length;
                }
                else
                {
                    renderedArguments.Add(Encode("<NO VALUE>"));
                }
            }

            for (int i = 0; i < renderedArguments.Count; i++)
            {
                // TODO: be aware of out of range
                var parameter       = parameters[i];
                var tooltipPosition = "top";

                var renderedArgument = renderedArguments[i];
                if (renderedArgumentsTotalLength > splitStringMinLength)
                {
                    builder.AppendLine();
                    builder.Append("    ");

                    tooltipPosition = "left";
                }
                else if (i > 0)
                {
                    builder.Append(" ");
                }

                builder.Append($"<span title=\"{parameter.Name}\" data-placement=\"{tooltipPosition}\">");
                builder.Append(renderedArgument);
                builder.Append("</span>");

                if (i < renderedArguments.Count - 1)
                {
                    builder.Append(",");
                }
            }

            builder.Append(");");

            return(new NonEscapedString(builder.ToString()));
        }
Ejemplo n.º 3
0
        public static IHtmlString Render(Job job)
        {
            if (job == null)
            {
                return(new HtmlString("<em>Can not find the target method.</em>"));
            }

            var builder = new StringBuilder();

            builder.Append(WrapKeyword("using"));
            builder.Append(" ");
            builder.Append(Encode(job.Type.Namespace));
            builder.Append(";");
            builder.AppendLine();
            builder.AppendLine();

            if (!job.Method.IsStatic)
            {
                var serviceName = Char.ToLower(job.Type.Name[0]) + job.Type.Name.Substring(1);

                builder.Append(WrapType(job.Type.Name));
                builder.AppendFormat(
                    " {0} = Activate<{1}>();",
                    Encode(serviceName),
                    WrapType(Encode(job.Type.Name)));

                builder.AppendLine();

                builder.Append(Encode(serviceName));
            }
            else
            {
                builder.Append(WrapType(Encode(job.Type.Name)));
            }

            builder.Append(".");
            builder.Append(Encode(job.Method.Name));
            builder.Append("(");

            var parameters                   = job.Method.GetParameters();
            var renderedArguments            = new List <string>(parameters.Length);
            var renderedArgumentsTotalLength = 0;

            const int splitStringMinLength = 200;

            for (var i = 0; i < parameters.Length; i++)
            {
                var parameter = parameters[i];

                if (i < job.Arguments.Length)
                {
                    var argument = job.Arguments[i]; // TODO: check bounds

                    var argumentRenderer = ArgumentRenderer.GetRenderer(parameter.ParameterType);

                    var renderedArgument = argumentRenderer.Render(parameter.Name, argument);
                    renderedArguments.Add(renderedArgument);
                    renderedArgumentsTotalLength += renderedArgument.Length;
                }
                else
                {
                    renderedArguments.Add(Encode("<NO VALUE>"));
                }
            }

            for (int i = 0; i < renderedArguments.Count; i++)
            {
                var renderedArgument = renderedArguments[i];
                if (renderedArgumentsTotalLength > splitStringMinLength)
                {
                    builder.AppendLine();
                    builder.Append("    ");
                }
                else if (i > 0)
                {
                    builder.Append(" ");
                }

                builder.Append(renderedArgument);

                if (i < renderedArguments.Count - 1)
                {
                    builder.Append(",");
                }
            }

            builder.Append(");");

            return(new HtmlString(builder.ToString()));
        }