想法延长C#语法 [英] Idea for extending C# syntax

查看:130
本文介绍了想法延长C#语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#不幸不允许额外的用户定义的语法。但我不知道是否有可能通过点击到Visual Studio onbuild事件超过这个限制。

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.

假设我有一些语法糖它可以很容易地转化为实际的C#代码。
。如果我是自动翻译含有这种新的语法为有效的CS文件的CS文件,一个C# - 项目建成权利之前,则该项目可以成功地建立。总的来说我仿佛延长了C#语言,因为我开始与一个无效的CS文件包含非官方的语法,但它无论如何编译,这将发挥作用。

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.

我知道这有几个问题,例如,这种翻译是永久性的。这或许可以通过恢复原来的CS被规避(调试结束后应恢复,否则某些IDE功能也将丢失)。但这些都是次要问题。

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.

请让我知道你怎么想这个主意。这是可能的,如果是这样,可能有人告诉我一些有用的教程,以便实现这一目标?在特定轻敲 - 到 - 一个-onbuild事件。
我搜索MSDN,但主题(http://msdn.microsoft.com/en-us/library/hthab0h8.aspx)并没有帮助我。

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.

推荐答案

我不会说这是否是一个好主意或没有,因为我不知道有足够的了解你想要做什么。我建议这一点,虽然:什么你建议是有某种扩展C#源代码文件,获取在生成过程中转化为常规的CS

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.

就个人而言,我会首先摆脱你是扩展C#语言的一念之差澄清这一点;我反而认为它是定义恰好是语法类似于C#(我认为)一种新的语言。使用不同的文件扩展名,这样的Visual Studio不会尝试编译你的语言为C#。 (也许.csx?人们喜欢将字母x,对吧?)

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中已经做这样的事情在其他方面可能不会那么明显。如果您添加一个资源文件到一个项目时,Visual Studio将通常还包括一个动态生成designer.cs与生成的代码根据您的.resx文件的内容。如果你看一下.resx文件的属性,你会注意到,在自定义工具酒店ResXFileCodeGenerator的值。从理论上讲,你应该能够实现自己的发电机来执行你提到的转换步骤。事实上,这种转换并不一定像你说的是一次性的事情。在翻译过程中应该产生一个新的文件,但保留原文件不变。原始文件的任何更改将导致Visual Studio中重新生成自动生成的文件。

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.

我还没有尝试实现一个自定义生成自己,但我认为这些文章相关:实现单个文件发电机和< A HREF =http://msdn.microsoft.com/en-us/library/bb166527%28v=VS.80%29.aspx相对=nofollow>注册单个文件发电机

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

您.csproj的文件将包含一些诸如以下内容:

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>



在哪里Example.csx是包含扩展语法和Example.cs源代码文件是生成的输出翻译的进入Example.csx正常的C#代码。

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.

这篇关于想法延长C#语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆