3

I create a number of add-ins for the Revit Structure API. Each tool has to habe a class which implements the interface IExternalCommand.

In the latest version of Revit, for your tool to work you need to have two attributes on the class that implements that interface:

[Regeneration(RegenerationOption.Manual)] [Transaction(TransactionMode.Automatic)]

The values in brackets can change, but there must be something there. Often I am finding myself forgetting to put the attributes on, then when it comes to runtime it crashes. Is there any way in Visual Studio 2010 to add a compiler warning or error saying that if your class implements that interface it must have those 2 attributes? I have resharper if that helps.

Can anyone point me into the right direction?

RodH257
  • 3,552
  • 5
  • 36
  • 46
  • Very closely related: http://stackoverflow.com/questions/19454/enforce-attribute-decoration-of-classes-methods – Aaronaught May 06 '10 at 01:21
  • sort of, but I'm after a compiler error, this code is not under unit testing (for various reasons) – RodH257 May 06 '10 at 05:21

2 Answers2

4

Unfortunately not. (I don't know about Resharper, though)

If you have VS2010 Ultimate, you could write a custom Code Analysis rule.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

Not during compile time, but I think it'd be easy to with reflection.

I suggest a separate program that uses reflection to examine your compiled assembly, finds all classes with the specified interface, then checks attributes on those classes, returning a nice friendly error message very quickly.

You'd still have to run this program after you compile your program, but depending on your IDE, you could set it as a post-build step.

abelenky
  • 63,815
  • 23
  • 109
  • 159