Beispiel #1
0
        private static MethodDeclarationSyntax MapMethodSyntax(IFileMetaData fileMetaData, IGeneratorConfiguration config, bool useExtensionSyntax = false)
        {
            var roslynGeneratorUtils = new RoslynGeneratorUtils();
            var className            = roslynGeneratorUtils.NormalizeIdentifiers(Path.GetFileNameWithoutExtension(fileMetaData.FileName),
                                                                                 config.PascalCase);

            var objParam = SyntaxFactory.Parameter(SyntaxFactory.Identifier("obj"))
                           .WithType(SyntaxFactory.ParseTypeName(className));

            if (useExtensionSyntax)
            {
                objParam = objParam.AddModifiers(SyntaxFactory.Token(SyntaxKind.ThisKeyword));
            }
            var parameterList = new List <ParameterSyntax>()
            {
                objParam,
                SyntaxFactory.Parameter(SyntaxFactory.Identifier("row"))
                .WithType(SyntaxFactory.ParseTypeName("IRow"))
            };

            var statements = new List <StatementSyntax>();

            foreach (var field in fileMetaData.Fields)
            {
                var propertyConfig = config.GetPropertyConfiguration(field.Term);
                var propertyName   = propertyConfig.PropertyName ?? roslynGeneratorUtils.NormalizeIdentifiers(field.Term, config.PascalCase);
                if (propertyConfig.Include)
                {
                    var statement = CreateStatement(propertyName, field.Term, propertyConfig)
                                    .WithTrailingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed);
                    statements.Add(statement);
                }
            }

            var methodSyntax = SyntaxFactory.MethodDeclaration(SyntaxFactory.ParseTypeName("void"), "MapRow")
                               .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                               .AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword))
                               .AddParameterListParameters(parameterList.ToArray())
                               .WithBody(SyntaxFactory.Block(statements.ToArray()));

            return(methodSyntax);
        }
        private PropertyDeclarationSyntax GenerateProperty(FieldType metaData,
                                                           PropertyConfiguration propertyConfiguration,
                                                           RoslynGeneratorUtils roslynGeneratorUtils,
                                                           IGeneratorConfiguration config)
        {
            var propertyName        = propertyConfiguration.PropertyName ?? roslynGeneratorUtils.NormalizeIdentifiers(metaData.Term, config.PascalCase);
            var propertyDeclaration = SyntaxFactory
                                      .PropertyDeclaration(SyntaxFactory.ParseTypeName(propertyConfiguration.TypeName), propertyName)
                                      .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                                      .AddAccessorListAccessors(
                SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
                SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));

            if (config.TermAttribute != TermAttributeType.none)
            {
                AttributeListSyntax attributeList = GenerateTermAttribute(metaData, config);
                propertyDeclaration = propertyDeclaration.AddAttributeLists(attributeList);
            }

            return(propertyDeclaration);
        }
        public ClassDeclarationSyntax GeneratClassSyntax(IFileMetaData fileMetaData, IGeneratorConfiguration config)
        {
            var roslynGeneratorUtils = new RoslynGeneratorUtils();
            var className            = roslynGeneratorUtils.NormalizeIdentifiers(Path.GetFileNameWithoutExtension(fileMetaData.FileName),
                                                                                 config.PascalCase);
            var classDeclaration = SyntaxFactory
                                   .ClassDeclaration(className)
                                   .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                                   .AddModifiers(SyntaxFactory.Token(SyntaxKind.PartialKeyword));

            foreach (var metaData in fileMetaData.Fields)
            {
                var propertyConfiguration = config.GetPropertyConfiguration(metaData.Term);

                if (propertyConfiguration.Include)
                {
                    var propertyDeclaration = GenerateProperty(metaData, propertyConfiguration, roslynGeneratorUtils, config);
                    classDeclaration = classDeclaration.AddMembers(propertyDeclaration);
                }
            }
            return(classDeclaration);
        }