Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use roslyn on proxygen instead of codedom #4

Open
iansmirlis opened this issue Oct 30, 2023 · 7 comments
Open

Use roslyn on proxygen instead of codedom #4

iansmirlis opened this issue Oct 30, 2023 · 7 comments
Assignees
Labels
enhancement New feature or request proxygen

Comments

@iansmirlis
Copy link
Owner

It's better to generate code with roslyn, codedom is obsolete

@iansmirlis iansmirlis added enhancement New feature or request proxygen labels Oct 30, 2023
@iansmirlis iansmirlis self-assigned this Oct 30, 2023
@pavelsavara
Copy link

But triggering it via roslyn alanyzer may have performance implication for Visual Studio. So external tool (possibly based on Roslyn) triggered from MSbuild would be better choice.

See also dotnet/roslyn#57608

@iansmirlis
Copy link
Owner Author

For some projects, I have used roslyn directly, i.e.

SyntaxFactory.CompilationUnit()
            .AddMembers( .....
.....
            .NormalizeWhitespace()
            .WriteTo(Console.Out);

which is extremely fast and there is no dependency on visual studio or msbuild.

@pavelsavara
Copy link

For some projects, I have used roslyn directly, i.e.
which is extremely fast and there is no dependency on visual studio or msbuild.

That's good, much better than my WriteLine 🤣

I was warning about https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview

@pavelsavara
Copy link

the dependency is created in the opposite direction with roslyn source generators, they are running your code on each key-press in VS. And that's nasty environment.

@iansmirlis
Copy link
Owner Author

I was warning about https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview

the dependency is created in the opposite direction with roslyn source generators, they are running your code on each key-press in VS. And that's nasty environment.

Yeah I know, so far I have used roslyn functionality directly for both analyzing and code generation and it works very well.

But it needs some custom layer to translate your intentions to roslyn calls, because it can become painful:

/*class Program
{
    public static void Main()
    {
        System.Console.WriteLine("Hello world!");
    }
}*/

CompilationUnit()
.WithMembers(
    SingletonList<MemberDeclarationSyntax>(
        ClassDeclaration("Program")
        .WithMembers(
            SingletonList<MemberDeclarationSyntax>(
                MethodDeclaration(
                    PredefinedType(
                        Token(SyntaxKind.VoidKeyword)),
                    Identifier("Main"))
                .WithModifiers(
                    TokenList(
                        new []{
                            Token(SyntaxKind.PublicKeyword),
                            Token(SyntaxKind.StaticKeyword)}))
                .WithBody(
                    Block(
                        SingletonList<StatementSyntax>(
                            ExpressionStatement(
                                InvocationExpression(
                                    MemberAccessExpression(
                                        SyntaxKind.SimpleMemberAccessExpression,
                                        MemberAccessExpression(
                                            SyntaxKind.SimpleMemberAccessExpression,
                                            IdentifierName("System"),
                                            IdentifierName("Console")),
                                        IdentifierName("WriteLine")))
                                .WithArgumentList(
                                    ArgumentList(
                                        SingletonSeparatedList<ArgumentSyntax>(
                                            Argument(
                                                LiteralExpression(
                                                    SyntaxKind.StringLiteralExpression,
                                                    Literal("Hello world!"))))))))))))))
.NormalizeWhitespace()

@pavelsavara
Copy link

BTW: https://roslynquoter.azurewebsites.net/

@iansmirlis
Copy link
Owner Author

BTW: https://roslynquoter.azurewebsites.net/

Yes, I generated this example with this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request proxygen
Projects
None yet
Development

No branches or pull requests

2 participants