コード例 #1
0
        public string Transform(string originalText, TransformingContext context)
        {
            var genericCount = context.RepeatIndex + 2;

            var content = originalText
                          // 替换泛型。
                          .Replace("<out T>", FromTemplate("<{0}>", "out T{n}", ", ", genericCount))
                          .Replace("Task<T>", FromTemplate("Task<({0})>", "T{n}", ", ", genericCount))
                          .Replace("Func<T, Task>", FromTemplate("Func<{0}, Task>", "T{n}", ", ", genericCount))
                          .Replace(" T, Task>", FromTemplate(" {0}, Task>", "T{n}", ", ", genericCount))
                          .Replace("(T, bool", FromTemplate("({0}, bool", "T{n}", ", ", genericCount))
                          .Replace("var (t, ", FromTemplate("var ({0}, ", "t{n}", ", ", genericCount))
                          .Replace(", t)", FromTemplate(", {0})", "t{n}", ", ", genericCount))
                          .Replace("return (t, ", FromTemplate("return ({0}, ", "t{n}", ", ", genericCount))
                          .Replace("<T>", FromTemplate("<{0}>", "T{n}", ", ", genericCount))
                          .Replace("{T}", FromTemplate("{{{0}}}", "T{n}", ", ", genericCount))
                          .Replace("(T value)", FromTemplate("(({0}) value)", "T{n}", ", ", genericCount))
                          .Replace("(T t)", FromTemplate("({0})", "T{n} t{n}", ", ", genericCount))
                          .Replace("(t)", FromTemplate("({0})", "t{n}", ", ", genericCount))
                          .Replace("var t =", FromTemplate("var ({0}) =", "t{n}", ", ", genericCount))
                          .Replace(" T ", FromTemplate(" ({0}) ", "T{n}", ", ", genericCount))
                          .Replace(" t;", FromTemplate(" ({0});", "t{n}", ", ", genericCount))
                          // 生成 [GeneratedCode]。
                          .Replace("    public interface ", $"    {Generatedattribute}{Environment.NewLine}    public interface ")
                          .Replace("    public class ", $"    {Generatedattribute}{Environment.NewLine}    public class ")
                          .Replace("    public sealed class ", $"    {Generatedattribute}{Environment.NewLine}    public sealed class ");

            return(content.Trim() + Environment.NewLine + GeneratedFooter);
        }
コード例 #2
0
        /// <summary>
        /// 将原始的单泛型参数的类型源码转换成多泛型参数的源码。
        /// </summary>
        /// <param name="originalText">原始的单泛型参数的类型源码。</param>
        /// <param name="context">转换所需要的上下文信息。</param>
        /// <returns>转换后的新文本。</returns>
        public string Transform(string originalText, TransformingContext context)
        {
            // 重复序号为 0, 1, 2…… 但是我们的起始转换泛型个数是 2(1 是手写的,无需转换),所以要增加 2。
            var genericCount = context.RepeatIndex + 2;

            var content = new StringBuilder(originalText)
                          // 替换泛型。
                          .Replace("<out T>", FromTemplate("<{0}>", "out T{n}", ", ", genericCount))
                          .Replace("Task<T>", FromTemplate("Task<({0})>", "T{n}", ", ", genericCount))
                          .Replace("Func<T, Task>", FromTemplate("Func<{0}, Task>", "T{n}", ", ", genericCount))
                          .Replace(" T, Task>", FromTemplate(" {0}, Task>", "T{n}", ", ", genericCount))
                          .Replace("(T, bool", FromTemplate("({0}, bool", "T{n}", ", ", genericCount))
                          .Replace("var (t, ", FromTemplate("var ({0}, ", "t{n}", ", ", genericCount))
                          .Replace(", t)", FromTemplate(", {0})", "t{n}", ", ", genericCount))
                          .Replace("return (t, ", FromTemplate("return ({0}, ", "t{n}", ", ", genericCount))
                          .Replace("<T>", FromTemplate("<{0}>", "T{n}", ", ", genericCount))
                          .Replace("{T}", FromTemplate("{{{0}}}", "T{n}", ", ", genericCount))
                          .Replace("(T value)", FromTemplate("(({0}) value)", "T{n}", ", ", genericCount))
                          .Replace("(T t)", FromTemplate("({0})", "T{n} t{n}", ", ", genericCount))
                          .Replace("(t)", FromTemplate("({0})", "t{n}", ", ", genericCount))
                          .Replace("var t =", FromTemplate("var ({0}) =", "t{n}", ", ", genericCount))
                          .Replace(" T ", FromTemplate(" ({0}) ", "T{n}", ", ", genericCount))
                          .Replace(" t;", FromTemplate(" ({0});", "t{n}", ", ", genericCount))
                          // 生成 [GeneratedCode]。
                          .Replace("    public interface ", $"    {Generatedattribute}{NewLine}    public interface ")
                          .Replace("    public class ", $"    {Generatedattribute}{NewLine}    public class ")
                          .Replace("    public sealed class ", $"    {Generatedattribute}{NewLine}    public sealed class ")
                          .AppendLine();

            return(GeneratedHeader + content);
        }