Open iansmirlis opened 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.
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.
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
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.
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()
Yes, I generated this example with this :)
It's better to generate code with roslyn, codedom is obsolete