【问题标题】:Vb.Net MustOverride vs New vs MustInheritVb.Net MustOverride vs New vs MustInherit
【发布时间】:2017-10-26 08:48:46
【问题描述】:

我正在尝试使用来自 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

只有这段代码我得到一个错误,它需要下面的代码

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

但是如果我在下面一行上面添加代码就会出错,

"New 不能用于声明为 MustInherit 的类"

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

【问题讨论】:

  • 这三者之间没有“对”。 New 初始化一个对象的新实例,MustInherit 表示这是一个只能被其他类继承的基类(即你不能创建它的实例,除非它被继承),MustOverride 表示必须“重写”属性或方法(即不能按原样使用它,必须先在继承基类的类中重写它)。
  • 文档:NewMustInheritMustOverride

标签: vb.net roslyn-code-analysis


【解决方案1】:

MustInherit 是抽象类的 VB .NET 语法。

这些类不能直接实例化。但是,派生自它们的类可以实例化为新对象。

例如,Animal 可能是 MustInherit 的一个很好的例子。 Dog 可能是一个派生自 Animal 的类,并且没有用 MustInherit 标记。

开发人员可以创建 Dog 的新实例,但不能创建 Animal。

MustOverride 是派生类必须为其提供实现的方法。在 Animal 示例中,假设它有一个名为 MakeNoise 的方法。您永远无法在 Animal 级别实现该方法,因为您不知道通用动物会发出什么噪音,但在派生的 Dog 类中,您可以。

在您的情况下,如果您想从 SynatxEditorFixAllProvider 派生,请确保您提供派生类的 FixAllAsync 方法的实现。

【讨论】:

    【解决方案2】:

    MustInherit 表示您的Class 是抽象的,无法实例化。这样的类只能间接实例化,也就是说,你 Inherit 一个 Class 从它们实现未实现的方法并实例化那个子 Class

    MustOverride 表示您的方法没有实现,应该在第一个非抽象后代类中实现(这样做的最新点)。

    New 是实例化的关键字。如果你说

    New SolutionChangeAction(title, Function(underscore) Task.FromResult(currentSolution))
    

    然后您打算创建(实例化)ObjectSolutionChangeAction Class。从你的问题来看,SolutionChangeAction 似乎也是抽象的,所以你需要从它继承一个非抽象的Class 并实例化它。

    【讨论】:

      【解决方案3】:

      谢谢大家,我需要改变

      MustInherit Class SynatxEditorFixAllProvider
      

      MustInherit Class SynatxEditorFixAllProviderBase
      

      然后把类改成

      Partial Public Class SynatxEditorFixAllProvider
         Inherits SynatxEditorFixAllProviderBase
      

      我用作示例用例的 C# 代码将抽象类与实现分开,这在 VB 中不起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-29
        • 1970-01-01
        • 2010-09-26
        • 2020-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多