Vb.Net MustOverride 与 New 与 MustInherit
Vb.Net MustOverride vs New vs MustInherit
我正在尝试使用 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 can not be used on a class that is declared MustInherit"
Public Shared ReadOnly Property Instance() As FixAllProvider = New SynatxEditorFixAllProvider()
MustInherit 是抽象 class.
的 VB .NET 语法
这些class不能直接实例化。但是,class派生自它们的实体可以实例化为新对象。
例如,Animal 可能是 MustInherit 的一个很好的例子。 Dog 将是一个 class,可能派生自 Animal,并且未标记为 MustInherit。
开发人员可以创建 Dog 的新实例,但不能创建 Animal 的实例。
MustOverride 是派生的 class 必须为其提供实现的方法。在 Animal 示例中,假设它有一个名为 MakeNoise 的方法。您永远无法在 Animal 级别实现该方法,因为您不知道一般动物会发出什么声音,但在派生的 Dog class 中,您可以。
在您的情况下,如果您想从 SynatxEditorFixAllProvider 派生,请确保您提供了派生 class.
的 FixAllAsync 方法的实现
MustInherit 表示您的 Class
是抽象的,无法实例化。这样的 classes 只能间接实例化,也就是说,你 Inherit
从它们实现未实现的方法的 Class
并实例化那个子 Class
.
MustOverride 表示您的方法未实现,应在第一个非抽象后代中实现 class(最新点)。
New是实例化的关键字。如果你说
New SolutionChangeAction(title, Function(underscore) Task.FromResult(currentSolution))
那么您打算创建(实例化)SolutionChangeAction
Class
的 Object
。从你的问题看来 SolutionChangeAction
也是抽象的,所以你需要从它继承一个非抽象的 Class
并实例化它。
谢谢大家,我需要改变
MustInherit Class SynatxEditorFixAllProvider
到
MustInherit Class SynatxEditorFixAllProviderBase
然后把class改成
Partial Public Class SynatxEditorFixAllProvider
Inherits SynatxEditorFixAllProviderBase
我用作示例的 C# 代码用例将抽象 class 与实现分开,这在 VB 中不起作用。
我正在尝试使用 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 can not be used on a class that is declared MustInherit"
Public Shared ReadOnly Property Instance() As FixAllProvider = New SynatxEditorFixAllProvider()
MustInherit 是抽象 class.
的 VB .NET 语法这些class不能直接实例化。但是,class派生自它们的实体可以实例化为新对象。
例如,Animal 可能是 MustInherit 的一个很好的例子。 Dog 将是一个 class,可能派生自 Animal,并且未标记为 MustInherit。
开发人员可以创建 Dog 的新实例,但不能创建 Animal 的实例。
MustOverride 是派生的 class 必须为其提供实现的方法。在 Animal 示例中,假设它有一个名为 MakeNoise 的方法。您永远无法在 Animal 级别实现该方法,因为您不知道一般动物会发出什么声音,但在派生的 Dog class 中,您可以。
在您的情况下,如果您想从 SynatxEditorFixAllProvider 派生,请确保您提供了派生 class.
的 FixAllAsync 方法的实现MustInherit 表示您的 Class
是抽象的,无法实例化。这样的 classes 只能间接实例化,也就是说,你 Inherit
从它们实现未实现的方法的 Class
并实例化那个子 Class
.
MustOverride 表示您的方法未实现,应在第一个非抽象后代中实现 class(最新点)。
New是实例化的关键字。如果你说
New SolutionChangeAction(title, Function(underscore) Task.FromResult(currentSolution))
那么您打算创建(实例化)SolutionChangeAction
Class
的 Object
。从你的问题看来 SolutionChangeAction
也是抽象的,所以你需要从它继承一个非抽象的 Class
并实例化它。
谢谢大家,我需要改变
MustInherit Class SynatxEditorFixAllProvider
到
MustInherit Class SynatxEditorFixAllProviderBase
然后把class改成
Partial Public Class SynatxEditorFixAllProvider
Inherits SynatxEditorFixAllProviderBase
我用作示例的 C# 代码用例将抽象 class 与实现分开,这在 VB 中不起作用。