Report and update deprecated APIs
When evolving an API, you have to introduce breaking changes sooner or later. The traditional way of dealing with that is to mark the deprecated types and members with the [Obsolete]
attribute and explain how to migrate to the new API using the attribute's message.
JetBrains Rider provides a more elegant solution that allows API users to find and automatically convert the old API to a new one. As the API author, you need to mark the obsolete type or member with the [CodeTemplate] attribute from JetBrains.Annotations where you can specify a search pattern to match the old API and a replacement pattern for it. The attribute will act as a custom code inspection with the corresponding quick-fix and fixes in scope.
[CodeTemplateAttribute]
is recognized by all other JetBrains products that analyze C# code, for example, JetBrains ReSharper JetBrains Fleet and InspectCode Command-Line Tool.
Let's see how it works using an example:
Let's annotate the deprecated IsTrue()
method with the [CodeTemplate]
attribute:
Now JetBrains Rider will report all usages of MyAssert.IsTrue()
and suggest a migration fix:
![JetBrains Rider: Using [CodeTemplate] attribute to suggest migration fixes for deprecated APIs JetBrains Rider: Using [CodeTemplate] attribute to suggest migration fixes for deprecated APIs](https://resources.jetbrains.com.cn/help/img/rider/2024.2/CodeTemplateAttribute_example.png)