14

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").

M.A. Hanin
  • 8,044
  • 33
  • 51
  • 1
    Really? You _really_ want to do this? – Sam Holder Apr 13 '10 at 10:59
  • 3
    Why not? It is not like "M.A. Hanin" is trying to do something outside of his field. – AMissico Apr 13 '10 at 11:04
  • @M.A. Hanin: I won't explain. If they are shocked then they shouldn't be answering the question. Correct? – AMissico Apr 13 '10 at 11:47
  • 1
    @AMissico, I think that fear and shock should be addressed with reason. Sure, it isn't fun when people let you feel that "this is outside of your league", especially when they won't rationalize their claim, but I'm willing to explain myself - just because there is a chance my explanation will draw insightful comments. BTW, your answers are very appreciated, +2 from me. – M.A. Hanin Apr 13 '10 at 11:55
  • @M.A. Hanin: Good book, I added to my books to read. I am very into custom attributes. Especially on Enum's. – AMissico Apr 13 '10 at 12:17

2 Answers2

4

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.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
AMissico
  • 21,470
  • 7
  • 78
  • 106
  • The original link was dead so I replaced it but I can't be sure the new links are correct. They seem to be more or less on topic however. – StayOnTarget Apr 12 '18 at 15:57
2

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.

AMissico
  • 21,470
  • 7
  • 78
  • 106