简体   繁体   English

通过代码生成将关键字添加到C#?

[英]Add a keyword to C# with code generation?

I have a domain specific language that I would like to interact with C# by adding new keywords (or some keyword-like syntax). 我有一种特定领域的语言,我想通过添加新的关键字(或某些类似关键字的语法)与C#进行交互。 Using attributes would be insufficient (I can't use them in method bodies), and shoehorning it into 'valid' C# notation that gets compiled into something else would be ugly and ruin the analogy with the DSL (and the translation from DSL-like notation to C# is nontrivial, so just writing the C# each time is out of the question). 使用属性是不够的(我不能在方法主体中使用它们),并且将其折磨成“有效的” C#表示法,然后将其编译成其他内容将是丑陋的,并破坏了与DSL的类比(以及类似DSL的转换) C#的书写方式很简单,因此每次编写C#都是不可能的。

I already have a way to parse the .cs file and transform it into legitimate, nontrivial, C# code which can be compiled. 我已经有一种方法来解析.cs文件并将其转换为可以编译的合法,平凡的C#代码。

The problem is, even through I can do all the work of defining the DSL, parsing it, and translating it into valid C#, Visual Studio won't let me use notation it doesn't understand; 问题是,即使通过我可以完成定义DSL,解析它并将其转换为有效的C#的所有工作,Visual Studio也不会让我使用它无法理解的表示法。 it just adds red squiggles, emits an error "cannot resolve symbol", and then often fails to properly parse things after it. 它只是添加红色花体,发出错误“无法解析符号”,然后常常无法正确解析其后的内容。

Is there a way to to force visual studio to ignore specific strings in its analysis? 有没有一种方法可以迫使Visual Studio在其分析中忽略特定的字符串? I've looked at visual studio plugins but it looks like, although I can do syntax highlighting and other stuff, I can't force it to ignore something it doesn't know how to parse (unless I'm missing some way to do that in the extension API, which is certainly possible). 我看过Visual Studio插件,但看起来虽然我可以进行语法高亮显示和其他操作,但是我不能强迫它忽略它不知道如何解析的内容(除非我错过了一些方法)可以在扩展API中实现)。

I've skimmed through the Roslyn stuff and don't see offhand a way to do this there, either. 我浏览了Roslyn的内容,也看不到在该处执行此操作的简便方法。 (Again, may be missing something, it doesn't seem to have great documentation.) (再次,可能缺少一些内容,似乎没有很好的文档。)

Take a look at PowerLanguages.E: http://visualstudiogallery.msdn.microsoft.com/a512e0d0-f4f3-4435-bad4-8d5efbb1db4a 看看PowerLanguages.E: http ://visualstudiogallery.msdn.microsoft.com/a512e0d0-f4f3-4435-bad4-8d5efbb1db4a

No english docs yet, sorry 尚无英文文档,对不起

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM