Search code examples
vb.netroslyn-code-analysis

Vb.Net MustOverride vs New vs MustInherit


I am trying to implement a codefix using some private code from Rolyn

Partial Public Class SynatxEditorFixAllProvider
       Inherits FixAllProvider
       Public Overrides Async Function GetFixAsync(ByVal fixAllContext As FixAllContext) As Task(Of CodeAction)
            Dim documentsAndDiagnosticsToFixMap As ImmutableDictionary(Of Document, ImmutableArray(Of Diagnostic)) = Await fixAllContext.GetDocumentDiagnosticsToFixAsync().ConfigureAwait(False)
            Return Await GetFixAsync1(documentsAndDiagnosticsToFixMap, fixAllContext, fixAllContext.CancellationToken).ConfigureAwait(False)
       End Function


        Async Function GetFixAsync1(ByVal documentsAndDiagnosticsToFixMap As ImmutableDictionary(Of Document, ImmutableArray(Of Diagnostic)), ByVal _FixAllContext As FixAllContext, ByVal cancellationToken As CancellationToken) As Task(Of CodeAction)
        ' Process all documents in parallel.
        Dim updatedDocumentTasks As IEnumerable(Of Task(Of Document)) = documentsAndDiagnosticsToFixMap.Select(Function(kvp) FixDocumentAsync(kvp.Key, kvp.Value, cancellationToken))

        Await Task.WhenAll(updatedDocumentTasks).ConfigureAwait(False)

        Dim currentSolution As Solution = _FixAllContext.Solution
        For Each Task As Task(Of Document) In updatedDocumentTasks
            ' 'await' the tasks so that if any completed in a canceled manner then we'll
            ' throw the right exception here.  Calling .Result on the tasks might end up
            ' with AggregateExceptions being thrown instead.
            Dim updatedDocument As Document = Await Task.ConfigureAwait(False)
            currentSolution = currentSolution.WithDocumentSyntaxRoot(updatedDocument.Id, Await updatedDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(False))
        Next Task

        Dim title As String = GetFixAllTitle(_FixAllContext)
        Return New SolutionChangeAction(title, Function(underscore) Task.FromResult(currentSolution))
End Function

With just this code I get an error and it wants the code below

Partial Public MustInherit Class SynatxEditorFixAllProvider
    Protected MustOverride Function FixAllAsync(ByVal document As Document, ByVal diagnostics As ImmutableArray(Of Diagnostic), ByVal editor As SyntaxEditor, ByVal cancellationToken As CancellationToken) As Task
End Class

But if I add the code above the following line gets an error,

"New can not be used on a class that is declared MustInherit"

Public Shared ReadOnly Property Instance() As FixAllProvider = New SynatxEditorFixAllProvider()

Solution

  • MustInherit is the VB .NET syntax for an abstract class.

    These classes cannot be instantiated directly. However, classes that derive from them can be instantiated as new objects.

    For example, Animal might be a good example of MustInherit. Dog would be a class that might be derived from Animal, and is not marked with MustInherit.

    The developer can create a new instance of Dog, but not of Animal.

    MustOverride is a method that a derived class must provide an implementation for. In the Animal example, let's say that it has a method called MakeNoise. You could never implement that method at the Animal level, because you don't know what noise a generic animal makes, but in your derived Dog class, you can.

    In your case, if you want to derive from SynatxEditorFixAllProvider, make sure you provide an implemention the FixAllAsync method of your derived class.