示例#1
0
        public static async Task <TSyntax> FromTemplate <TSyntax>(TemplateProvider provider, string name, object templateData = null)
            where TSyntax : CSharpSyntaxNode
        {
            provider = provider ?? throw new ArgumentNullException(nameof(provider));
            name     = name ?? throw new ArgumentNullException(name, nameof(name));

            string template = await provider.ReadTemplate(name, templateData);

            if (string.IsNullOrEmpty(template))
            {
                throw new ArgumentException($"The template provided by '{name}' does not represent a valid C# syntax tree.");
            }

            var tree = CSharpSyntaxTree.ParseText(template);
            var root = tree.GetRootAsync();

            if (root.Result is CompilationUnitSyntax compilationUnit)
            {
                var member = compilationUnit.Members.OfType <TSyntax>().FirstOrDefault();
                if (member is object)
                {
                    return(member);
                }
            }

            throw new InvalidOperationException($"The template does not represent a {typeof(TSyntax).Name}.");
        }
示例#2
0
        public async Task FromTemplate_ReturnsTemplate_AsTargetSyntax()
        {
            // Arrange
            var provider = TemplateProvider.FromAssembly(
                typeof(CodeGenStructTests).Assembly,
                "Alium.CodeGeneration");

            // Act
            var @class = await CodeGen.FromTemplate <ClassDeclarationSyntax>(provider, "TestClass");

            // Assert
            Assert.NotNull(@class);
        }