Integrating a Custom Compiler with the Visual Studio IDE

淺唱寂寞╮ 提交于 2020-01-01 04:16:09

问题


Background: I want to create a custom VB compiler, extending the "original" compiler, to handle my custom compile-time attributes.

Question: after I've created my custom compiler and I've got an executable file capable of compiling VB code via the standard command-line interface, how do I integrate this compiler with the Visual Studio IDE? (such that pressing "compile" or "build" will make use of my compiler instead of the default compiler).

EDIT: (Correct me if i'm wrong)

From the reactions here, I see this question is a bit shocking, so I shall further explain my needs and background: .NET provides us with a great mechanism called Attributes. As far as I understand, making attributes apply their intended behavior upon the attributed element (assembly, module, class, method, etc.) - attributes must be reflected upon. So the real trick here is reflecting and applying behavior at the right spot.

Lets take Serialization for example: We decorate a class with the Serializable attribute. We then pass an instance of the class to the formatter's Serialize method. The formatter reflects upon the instance, checking if it has the Serializable attribute, and acting accordingly.

Now, if we examine the Synchronization, Flags, Obsolete and CLSCompliant attributes, then the real question is: who reflects upon them? At least in some cases, it has to be the compiler (and/or IDE). Therefore, it seems that if I wish to create custom attributes that change an element's behavior regardless of any specific consumer, i must extend the compiler to reflect upon them at compilation.

Of course, these are not my personal insights: the book "Applied .NET Attributes" provides a complete example of creating a custom attribute and a custom C# compiler to reflect upon that attribute at compilation (the example is used to implement "java-style checked exceptions").


回答1:


Check out "MSBuild Overview" at http://msdn.microsoft.com/en-us/library/ms171452.aspx. Visual Studio projects are MSBuild projects. I believe all you would have to do is make a couple minor changes. I have not done it for a compiler, but I did it with ILMerge after compiling and it works great. Seamless and even possible to debug the merged assemblies.




回答2:


Check out "BuildAction Property" and "CustomTool Property".

A few years ago, I read an excellent article about creating a custom compiler, registering it, and setting these properties. I cannot find the article now, but this should get you started.



来源:https://stackoverflow.com/questions/2628914/integrating-a-custom-compiler-with-the-visual-studio-ide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!