We've all been in the situation where we wanted the compiler to interpret custom attributes like it does for Obsolete
. Sadly, the compiler is hardwired to recognize only that one.
Until now! This project is a 2-in-1 library that contains a Roslyn analyzer and a couple of attributes that you can use to create compiler-recognized attributes to your heart's content!
It's published via a Nuget package that gets installed both as an analyzer and as a library reference. Here's how it works:
Install-Package CompilerAttributes
This attribute is what you'll use to decorate identifiers that should generate a warning or error.
public class MyAttributeAttribute : Attribute
{
}
NOTE By convention, attribute class names are suffixed with "Attribute". When they're used, the suffix can be omitted. Thus using the above attribute will look like [MyAttribute]
.
CompilerAttributes provides two attributes to mark your attribute as one that generates compiler output: GeneratesWarningAttribute
and GeneratesErrorAttribute
. Pick one and decorate your attribute with it.
[GeneratesWarning("{0} generated a warning")]
public class MyAttributeAttribute : Attribute
{
}
The single argument that is passed into the attribute is the message that will appear in the compiler output. It supports up to a single string format argument (i.e. {0}
) that will be replaced with the identifier name.
Now whenever you use the MyAttribute
attribute on something that thing will generate a compiler warning.
[MyAttribute]
public class Class1
{
}
// later
var instance = new Class1(); // this line generates a warning that says "Class1 generated a warning"
I originally created this because I needed a way to indicate that some features I was working on for Manatee.Json were going to be experimental, kind of an opposite of the Obsolete
attribute.
If you have questions, experience problems, or feature ideas, please create an issue.
If you'd like to help out with the code, please feel free to fork and create a pull request.
This code uses C# 7 features, so a compiler/IDE that supports these features is required.
There are three solutions:
- ComplierAttributes - This is the main solution that builds the Nuget package. It contains three projects the main project, a test project, and a VISX generating project. Though I don't publish from the VISX project, it's immensely useful for debugging. It will start a new instance of VS under a different profile and install the VISX. From there, you just need to open a solution. That's where the other solution comes in.
- ClassLibrary1 - This has some boilerplate code that gives as many examples of usage of a symbol that I cared to think of. Happily, all of these usages are underlined with little green squigglies, and the error output shows a bunch of warnings.
- AutoBuild - This one just contains the main project. I use it to isolate this project as part of my CI process. Just ignore it.
During development, building within Visual Studio should be fine.
I don't use the test project right now. It was added as part of the template. It could use some test cases... eventually.
I use Jetbrains Resharper in Visual Studio to maintain the code style (and for many of the other things that it does). The solution is set up with team style settings, so if you're using Resharper the settings should automatically load. Please follow the suggestions.