背景:我想创建一个自定义 VB 编译器,扩展“原始”编译器,以处理我的自定义编译时属性。
问题:创建自定义编译器并获得能够通过标准命令行界面编译 VB 代码的可执行文件后,如何将此编译器与 Visual Studio IDE 集成? (这样按“编译”或“构建”将使用我的编译器而不是默认编译器)。
编辑:(如果我错了,请纠正我)
从这里的反应来看,我发现这个问题有点令人震惊,所以我将进一步解释我的需求和背景:
.NET 为我们提供了一种称为属性的出色机制。据我了解,使属性将其预期行为应用于属性元素(程序集、模块、类、方法等) - 必须反映属性。因此,这里真正的技巧是在正确的位置反映和应用行为。
让我们以序列化为例:我们用 Serialized 属性修饰一个类。然后,我们将该类的实例传递给格式化程序的 Serialize 方法。格式化程序反映实例,检查它是否具有可序列化属性,并采取相应的操作。
现在,如果我们检查 Synchronization、Flags、Obsolete 和 CLSCompliant 属性,那么真正的问题是:谁反映它们?至少在某些情况下,它必须是编译器(和/或 IDE)。因此,如果我希望创建自定义属性来更改元素的行为,而不管任何特定的使用者如何,我必须扩展编译器以在编译时反映它们。
当然,这些不是我个人的见解:《Applied .NET Attributes一书” 提供了创建自定义属性和自定义 C# 编译器以在编译时反映该属性的完整示例(该示例用于实现“java 样式检查异常”)。
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").
发布评论
评论(2)
查看“BuildAction 属性”和“自定义工具属性”。
几年前,我读了一篇关于创建自定义编译器、注册它以及设置这些属性的优秀文章。我现在找不到这篇文章,但这应该可以帮助您开始。
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.
查看“MSBuild 概述”,网址为 http://msdn.microsoft.com/en -us/library/ms171452.aspx。 Visual Studio 项目是 MSBuild 项目。我相信您所要做的就是做一些小的改变。我还没有为编译器做过它,但我在编译后用 ILMerge 做了它,效果很好。无缝甚至可以调试合并的程序集。
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.