Phoenix
Phoenix

Reputation: 913

What is the difference between a Roslyn CodeFix and Refactoring?

I have been scouring all possible documentation I could find about the Roslyn APIs but I could not find an answer to this simple question.

I know a CodeFix inherits from CodeFixProvider and provides a Code Fix. I also know that a Refactoring inherits from CodeRefactoringProvider and provides a Refactoring. One difference I found is that for some reason you cannot redistribute Refactorings using Nuget, only through a VSIX, while you can redistribute a CodeFix using both.

But what exactly is a Code Fix and what is a Refactoring? What can one do that the other cannot?

Upvotes: 7

Views: 1148

Answers (3)

Nathan Williams
Nathan Williams

Reputation: 41

The conceptual difference in the accepted answer is also in a way the practical difference.

Both CodeFixProviders and CodeRefactoringProviders generate CodeActions that users can apply to their code. The key difference is that each CodeAction offered by a CodeFixProvider has to be associated with (and is presumed to address) one or more Diagnostics produced by a Roslyn Analyzer, whereas a CodeAction offered by a CodeRefactoringProvider is not associated with any Diagnostic.

Upvotes: 1

SENya
SENya

Reputation: 1348

In addition to the previous answer there is another functional difference between code fix and code refactoring. You can easily add support to solution wide code fix by overriding GetFixAllProvider:

public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;

But as far as I know there isn't such an easy way to provide mass refactoring

Upvotes: 0

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239764

A Code Fix is for where you've identified an error or mistake in the code and can deduce how to correct the code.

A Refactoring is a change to the code that usually will make it neither more nor less correct. It's not unusual to offer multiple refactorings that will transform the code between various forms, including, often, back to the form it was in before someone accepted any refactoring. In contrast, it would be rare in the extreme to find another Code Fix (in the same package) that transformed code into a form that another Code Fix could apply to.

Upvotes: 4

Related Questions