iansmirlis / jni4net

Fast, object oriented, intraprocess bridge between JVM and CLR
https://jni4net.github.io/
1 stars 0 forks source link

Use roslyn on proxygen instead of codedom #4

Open iansmirlis opened 10 months ago

iansmirlis commented 10 months ago

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

pavelsavara commented 10 months ago

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 https://github.com/dotnet/roslyn/issues/57608

iansmirlis commented 10 months ago

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 commented 10 months ago

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 commented 10 months ago

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 commented 10 months ago

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 commented 10 months ago

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

iansmirlis commented 10 months ago

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

Yes, I generated this example with this :)