Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.Net MustOverride vs New vs MustInherit_Vb.net_Roslyn Code Analysis - Fatal编程技术网

Vb.Net MustOverride vs New vs MustInherit

Vb.Net MustOverride vs New vs MustInherit,vb.net,roslyn-code-analysis,Vb.net,Roslyn Code Analysis,我正在尝试使用Rolyn的一些私有代码实现代码修复 Partial Public Class SynatxEditorFixAllProvider Inherits FixAllProvider Public Overrides Async Function GetFixAsync(ByVal fixAllContext As FixAllContext) As Task(Of CodeAction) Dim documentsAndDiagn

我正在尝试使用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
但是如果我把代码添加到下一行上面,就会得到一个错误

“不能在声明为MustInherit的类上使用New”


MustInherit是抽象类的VB.NET语法

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

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

开发者可以创建狗的新实例,但不能创建动物的新实例

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

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

表示您的
类是抽象的,无法实例化。此类类只能间接实例化,也就是说,您
从实现未实现方法的类继承
,并实例化子类

表示您的方法没有实现,应该在第一个非抽象子类中实现(最新的实现点)

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

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

然后,您打算创建(实例化)一个
对象
,该对象是
SolutionChangeAction
类的
对象。从您的问题来看,
SolutionChangeAction
似乎也是抽象的,因此您需要从中继承一个非抽象的
,并将其实例化。

谢谢大家,我需要更改

MustInherit Class SynatxEditorFixAllProvider

然后将类更改为

Partial Public Class SynatxEditorFixAllProvider
   Inherits SynatxEditorFixAllProviderBase
我使用的C#代码作为示例用例,用于将抽象类与实现分离,但在VB中不起作用

你能提供一个答案吗?这三者之间没有“对”
New
初始化对象的新实例,
MustInherit
表示这是一个只能由其他类继承的基类(即,除非继承,否则不能创建它的实例),
MustOverride
表示必须“重写”属性或方法(即,不能按原样使用它,必须先在继承基类的类中重写它)。文档:。
MustInherit Class SynatxEditorFixAllProviderBase
Partial Public Class SynatxEditorFixAllProvider
   Inherits SynatxEditorFixAllProviderBase