How to generate a compiler error based on a missing attribute in C #?

I am creating several add-ons for the Revit Structure API. Each tool must have a class that implements the IExternalCommand interface.

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

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

The values ​​in parentheses may vary, but there must be something. Often times I find myself forgetting to include attributes and then when it comes to runtime it crashes. Is there a way in Visual Studio 2010 to add a compiler warning or error saying that if your class implements this interface, it must have these 2 attributes? I have resharper if that helps.

Can anyone point me in the right direction?

+2


a source to share


2 answers


Unfortunately not. (Though I don't know about Resharer)



If you have VS2010 Ultimate, you can write your own code analysis rule.

+4


a source


Not at compile time, but I think it would be easy with reflection.

I suggest a separate program that uses reflection to validate the assembled assembly, finds all classes with the specified interface, and then validates the attributes on those classes, quickly returning a welcome error message.



You will still have to run this program after compiling your program, but depending on your IDE, you might want to install it as a post-build step.

+2


a source







All Articles