I’d like to create a custom attribute that is executed during the build process and can break the build according to some logic, pretty similar to System.ObsoleteAttribute.
For instance, consider the following C# code (note that this is a simplified example):
public class MyClass
{
[Check]
public int MyProp { get; private set; }
}
This Check attribute should check that the property it decorates has a public setter. Since MyProp property doesn’t have a public setter (it has a private setter), the Check attribute should break the build.
Is it possible?
No – at least, not at the moment, with the standard build tools. It’s entirely possible that when the “compiler as a service” project ships, that may expose this functionality. But the C# compiler only really changes its behaviour a few well-known attributes.
Two options you might want to consider:
Personally I’d probably go with the latter… although really, there’s not much obvious benefit – if you remember to declaratively add the
[Check]attribute, aren’t you going to remember to add the public setter? Or to put it another way: if you forget to put on the public setter, aren’t you just as likely to forget to include the attribute?