有没有一种在C#中使用编译时源代码生成器高效生成文件夹的方法?

huangapple go评论59阅读模式
英文:

Is there an efficient way to Generate Folders using compiled-time source generator in c#?

问题

用于文件夹的编译时源代码生成器:

有没有一种有效的方法可以使源代码生成器在编译时生成文件夹,而不仅仅是文件?

注意:我知道可以使用System.IO,但我认为这不是最佳解决方案,我更喜欢按照以下方式编写:

context.AddSource("folder/file.g.cs", "source");
  • 我尝试过 context.AddSource("folder/file.g.cs", "source");

我期望它会在编译时生成一个名为“folder”的文件夹,里面有一个名为“file.g.cs”的文件,而此文件在编译时包含单词“source”。

这样做将不起作用,但会显示一个带有字符数 '\ ' 的错误消息。

  • 我尝试使用 System.IO 库处理文件,但由于这个原因,文件夹将不会在编译时被修改,并且不会被添加到默认路径中。
英文:

Compiled-time source generator for folders:

Is there an efficient way to make the Source Generators generate folders at compile time, not just files?

Note: I know that I can use System.IO, but I do not think that this is the best solution, and I prefer to write as follows:

context.AddSource(" folder /file.g.cs", "source");
  • I tried context.AddSource(" folder /file.g.cs", "source");

I expected that it would generate a folder named "folder" and inside it a file "file.g.cs" and this file contains the word "source" at compile time.

This will not work but will give an error message with the character number '\'.

  • I tried to deal with the files using the System.IO library, but because of this, the folder will not be modified at compile time, and it will not be added to the default path.

答案1

得分: 0

根据以下GitHub对话:允许在源生成器输出中使用目录,解决此问题的方法是不在VS 17.5.2中,因此您将需要使用VS 17.6预览版

英文:

Based on the following GitHub conversation: Allow directories in source generator outputs , the solution to this problem is not in VS 17.5.2, so you will have to use VS 17.6 Preview

huangapple
  • 本文由 发表于 2023年3月12日 17:56:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定