开发者

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

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 i开发者_Python百科mplements 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?


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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜