扩展 C# 语法的想法

发布于 2024-09-28 05:09:10 字数 460 浏览 2 评论 0 原文

不幸的是,C# 不允许额外的用户定义语法。但我想知道是否有可能通过利用 Visual Studio onbuild-event 来超越这个限制。

假设我有一些语法糖,可以轻松地将其转换为实际的 C# 代码。 如果我在构建 C# 项目之前自动将包含此新语法的 cs 文档转换为有效的 cs 文档,那么该项目就可以成功构建。总的来说,这就像我扩展了 C# 语言一样,因为我从包含非官方语法的无效 cs 文档开始,但无论如何它都编译了。

我意识到这有一些问题,例如这个翻译是永久性的。这也许可以通过恢复原始cs来规避(应该在调试结束后恢复,否则一些IDE功能将丢失)。但这些都是次要问题。

请告诉我您对这个想法的看法。这是否可能,如果可以,有人可以指导我一些有用的教程来实现这一目标吗?具体来说是点击构建事件。 我搜索了 MSDN,但该主题(http://msdn.microsoft.com/en-us/library/hthab0h8.aspx)对我没有帮助。

C# unfortunately does not allow for extra user-defined syntax. But I was wondering whether it was possible to surpass this limitation by tapping into the visual studio onbuild-event.

Suppose I have some syntactic sugar which could be easily translated into actual C# code.
If I were to automatically translate a cs document containing this new syntax into a valid cs document, right before a C#-project is built, then the project could build succesfully. Overall this would function as if I had extended the C# language, because I started with an invalid cs document containing unoffical syntax, but it compiled anyway.

I realize that this has a few problems, such as that this translation is permanent. This could perhaps be circumvented by restoring the original cs(which should be restored after the debugging has ended, otherwise some IDE functionality would be lost). But these are secondary problems.

Please let me know what you think of this idea. Is this possible, and if so, could someone direct me to some useful tutorials so achieve this? In specific the tapping-into-a-onbuild-event.
I've searched MSDN, but the topic(http://msdn.microsoft.com/en-us/library/hthab0h8.aspx) didn't help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缱绻入梦 2024-10-05 05:09:10

我不会说这是否是一个好主意,因为我对你想要做什么还不够了解。不过,我建议这样做:您提议的是拥有某种“扩展 C#”源代码文件,该文件在构建过程中被转换为常规 cs。

就我个人而言,我会首先摆脱“扩展”C# 语言的想法来澄清这一点;相反,我认为它定义了一种新语言,它在语法上恰好类似于 C#(我认为)。使用不同的文件扩展名,以便 Visual Studio 不会尝试将您的语言编译为 C#。 (也许是 .csx?人们喜欢添加字母 x,对吧?)

Visual Studio 已经以其他可能不那么明显的方式执行此类操作。如果将资源文件添加到项目中,Visual Studio 通常还会包含动态生成的“designer.cs”,其中包含根据 .resx 文件的内容生成的代码。如果您查看 .resx 文件的属性,您会注意到“自定义工具”属性的值为“ResXFileCodeGenerator”。理论上,您应该能够实现自己的生成器来执行您提到的翻译步骤。事实上,这个翻译并不一定像你说的那样是一次性的。翻译过程应该生成一个新文件,但保持原始文件不变。对原始文件的任何更改都会导致 Visual Studio 重新生成自动生成的文件。

我没有尝试自己实现自定义生成器,但我认为这些文章是相关的: 实现单个文件生成器注册单个文件生成器

您的 .csproj 文件将包含如下内容:

<Content Include="Example.csx">
  <Generator>ExtendedCSharpCodeGenerator</Generator>
  <LastGenOutput>Example.cs</LastGenOutput>
</Content>
<Compile Include="Example.cs">
  <AutoGen>True</AutoGen>
  <DesignTime>True</DesignTime>
  <DependentUpon>Example.csx</DependentUpon>
</Compile>

其中,Example.csx 是包含扩展语法的源代码文件,Example.cs 是翻译 Example 的结果输出.csx 转换为普通 C# 代码。

I won't say whether this is a good idea or not, since I don't know enough about what you're trying to do. I would suggest this, though: What you're proposing is to have some kind of "extended C#" source code file that gets translated into regular cs during the build process.

Personally, I would clarify this by first breaking away from the idea that you are "extending" the C# language; I would instead think of it as defining a new language that happens to be syntactically similar to C# (I assume). Use a different file extension so that Visual Studio does not try to compile your language as C#. (Maybe .csx? People love adding the letter x, right?)

Visual Studio already does this sort of thing in other ways that might not be quite so obvious. If you add a resource file to a project, Visual Studio will typically also include a dynamically generated "designer.cs" with code generated based on the content of your .resx file. If you look at the properties of the .resx file, you'll note that the "Custom Tool" property has a value of "ResXFileCodeGenerator". In theory you should be able to implement your own generator to perform the translation step that you mentioned. In fact, this translation does not have to be a one-time thing as you said. The translation process should generate a new file, but leave the original file intact. Any changes to the original file causes Visual Studio to regenerate the auto-generated file.

I've not tried to implement a custom generator myself, but I think these articles are relevant: Implementing Single File Generators, and Registering Single File Generators

Your .csproj file would contain something such as the following:

<Content Include="Example.csx">
  <Generator>ExtendedCSharpCodeGenerator</Generator>
  <LastGenOutput>Example.cs</LastGenOutput>
</Content>
<Compile Include="Example.cs">
  <AutoGen>True</AutoGen>
  <DesignTime>True</DesignTime>
  <DependentUpon>Example.csx</DependentUpon>
</Compile>

Where Example.csx is the source code file containing your extended syntax and Example.cs is the resulting output of translating Example.csx into normal C# code.

ら栖息 2024-10-05 05:09:10

您正在谈论的事情似乎是 Visual Studio 中 T4 模板的完美任务。

http://msdn.microsoft.com/en-us/library/bb126445.aspx

你可以定义任何你想要的东西;一定格式的文本文件、UML模型、数据库;您的 T4 模板可以按照您希望的方式将其转换为代码。

What you are talking about doing seems like a perfect task for T4 templates in Visual Studio.

http://msdn.microsoft.com/en-us/library/bb126445.aspx

You can define anything you'd like; text files with a certain format, UML models, a database; and your T4 template can transform it into code in what ever way you wish.

三生路 2024-10-05 05:09:10

我不确定这是一个好主意,但我只是有一个想法:也许你可以看看 扩展Visual Studio,下载SDK并查看文档。也许有可能实现您想要实现的目标。

I'm not sure it's a good idea, but I just had an idea: maybe you can have a look at Extending Visual Studio, download the SDK and check the doc. Maybe it would be possible to do what you are trying to achieve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文