C# Roslyn VSIX扩展因在修复提供程序中添加函数而崩溃

C# Roslyn VSIX扩展因在修复提供程序中添加函数而崩溃,c#,runtime,roslyn,C#,Runtime,Roslyn,我一直在处理一个问题,我将假设它是特定于环境的。 当我向CodeFixProvider添加任何函数(无论是否引用)时,我安装并重新安装了。尝试打开预览窗口时,VS出现系统聚合异常: 这是完整的代码修复提供程序唯一修改的是添加新函数 [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AggregateBugAnalyzerCodeFixProvider)), Shared] public class AggregateBugAn

我一直在处理一个问题,我将假设它是特定于环境的。 当我向CodeFixProvider添加任何函数(无论是否引用)时,我安装并重新安装了。尝试打开预览窗口时,VS出现系统聚合异常:

这是完整的代码修复提供程序唯一修改的是添加新函数

[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AggregateBugAnalyzerCodeFixProvider)), Shared]
public class AggregateBugAnalyzerCodeFixProvider : CodeFixProvider
{
    private const string title = "Make uppercase";

    public sealed override ImmutableArray<string> FixableDiagnosticIds
    {
        get { return ImmutableArray.Create(AggregateBugAnalyzerAnalyzer.DiagnosticId); }
    }

    public sealed override FixAllProvider GetFixAllProvider()
    {
        // See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/FixAllProvider.md for more information on Fix All Providers
        return WellKnownFixAllProviders.BatchFixer;
    }

    public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
    {
        var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

        // TODO: Replace the following code with your own analysis, generating a CodeAction for each fix to suggest
        var diagnostic = context.Diagnostics.First();
        var diagnosticSpan = diagnostic.Location.SourceSpan;

        // Find the type declaration identified by the diagnostic.
        var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<TypeDeclarationSyntax>().First();

        // Register a code action that will invoke the fix.
        context.RegisterCodeFix(
            CodeAction.Create(
                title: title,
                createChangedSolution: c => MakeUppercaseAsync(context.Document, declaration, c),
                equivalenceKey: title),
            diagnostic);
    }

    private async Task<Solution> MakeUppercaseAsync(Document document, TypeDeclarationSyntax typeDecl, CancellationToken cancellationToken)
    {
        // Compute new uppercase name.
        var identifierToken = typeDecl.Identifier;
        var newName = identifierToken.Text.ToUpperInvariant();

        // Get the symbol representing the type to be renamed.
        var semanticModel = await document.GetSemanticModelAsync(cancellationToken);
        var typeSymbol = semanticModel.GetDeclaredSymbol(typeDecl, cancellationToken);

        // Produce a new solution that has all references to that type renamed, including the declaration.
        var originalSolution = document.Project.Solution;
        var optionSet = originalSolution.Workspace.Options;
        var newSolution = await Renamer.RenameSymbolAsync(document.Project.Solution, typeSymbol, newName, optionSet, cancellationToken).ConfigureAwait(false);

        // Return the new solution with the now-uppercase type name.
        return newSolution;
    }

    //Just the existance of this function causes VS to throw
    public string Blowup()
    {
        return "Why";
    }
}
[ExportCodeFixProvider(LanguageNames.CSharp,Name=nameof(AggregateBugAnalyzerCodeFixProvider)),共享]
公共类AggregateBugAnalyzerCodeFixProvider:CodeFixProvider
{
private const string title=“使大写”;
公共密封覆盖ImmutableArray FixableDiagnostics
{
获取{return ImmutableArray.Create(AggregateBugAnalyzerAnalyzer.DiagnosticId);}
}
公共密封覆盖FixAllProvider GetFixAllProvider()
{
//看https://github.com/dotnet/roslyn/blob/master/docs/analyzers/FixAllProvider.md 有关修复所有提供程序的详细信息
返回WellKnownFixAllProviders.BatchFixer;
}
公共密封覆盖异步任务注册表CodeFixesAsync(CodeFixContext上下文)
{
var root=await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
//TODO:用您自己的分析替换以下代码,为每个修复生成一个CodeAction以提供建议
var diagnostic=context.Diagnostics.First();
var diagnosticSpan=diagnostic.Location.SourceSpan;
//查找诊断所标识的类型声明。
var declaration=root.FindToken(diagnosticSpan.Start).Parent.antestorsandself().OfType().First();
//注册将调用修复程序的代码操作。
context.RegisterCodeFix(
代码动作。创建(
标题:标题,,
createChangedSolution:c=>MakeUpperCaseAync(context.Document,声明,c),
等效键:标题),
诊断性);
}
专用异步任务makeUpperCaseAync(文档文档、类型声明语法、类型声明、取消令牌取消令牌)
{
//计算新的大写名称。
var identifierToken=类型标识符;
var newName=identifierToken.Text.ToUpperInvariant();
//获取表示要重命名的类型的符号。
var semanticModel=await document.GetSemanticModelAsync(cancellationToken);
var typeSymbol=semanticModel.GetDeclaredSymbol(typeDecl,cancellationToken);
//生成一个新的解决方案,该解决方案重命名了对该类型的所有引用,包括声明。
var originalSolution=document.Project.Solution;
var optionstart=originalSolution.Workspace.Options;
var newSolution=wait Renamer.RenameSymbolAsync(document.Project.Solution,typeSymbol,newName,optionstart,cancellationToken)。configurewait(false);
//返回具有现在大写类型名称的新解决方案。
返回新的解决方案;
}
//仅此函数的存在就导致VS抛出
公共字符串放大()
{
返回“为什么”;
}
}
当我启用“所有异常”时,我捕捉到以下错误:

其他信息:无法将“System.Reflection.RuntimeMethodInfo”类型的对象强制转换为“System.Reflection.ConstructorInfo”类型

这可能是由于运行时不匹配造成的。如果我把函数移到它自己的类中,一切都很好

内部异常堆栈跟踪

>   at Microsoft.VisualStudio.Composition.Reflection.ResolverExtensions.Resolve(ConstructorRef constructorRef)
   at Microsoft.VisualStudio.Composition.RuntimeComposition.RuntimePart.get_ImportingConstructor()
   at Microsoft.VisualStudio.Composition.RuntimeExportProviderFactory.RuntimeExportProvider.RuntimePartLifecycleTracker.CreateValue()
   at Microsoft.VisualStudio.Composition.ExportProvider.PartLifecycleTracker.Create()
   at Microsoft.VisualStudio.Composition.ExportProvider.PartLifecycleTracker.MoveNext(PartLifecycleState nextState)
   at Microsoft.VisualStudio.Composition.ExportProvider.PartLifecycleTracker.MoveToState(PartLifecycleState requiredState)
   at Microsoft.VisualStudio.Composition.ExportProvider.PartLifecycleTracker.GetValueReadyToExpose()
   at Microsoft.VisualStudio.Composition.RuntimeExportProviderFactory.RuntimeExportProvider.<>c__DisplayClass15_0.<GetExportedValueHelper>b__0()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.VisualStudio.Composition.RuntimeExportProviderFactory.RuntimeExportProvider.<>c__DisplayClass15_0.<GetExportedValueHelper>b__0()
   at Microsoft.VisualStudio.Composition.DelegateServices.<>c__DisplayClass2_0`1.<As>b__0()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Lazy`1.get_Value()
   at Microsoft.CodeAnalysis.CodeFixes.CodeFixService.<>c__DisplayClass24_0.<GetFixerPerLanguageMap>b__0()
   at System.Lazy`1.CreateValue()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Lazy`1.get_Value()
   at Microsoft.CodeAnalysis.CodeFixes.CodeFixService.<AppendFixesAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.CodeFixes.CodeFixService.<GetFixesAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionsSourceProvider.Source.<>c__DisplayClass15_1.<<GetCodeFixes>b__0>d.MoveNext()
Microsoft.VisualStudio.Composition.Reflection.ResolveX.ResolveX.Resolve(ConstructorRef ConstructorRef)上的
>
在Microsoft.VisualStudio.Composition.RuntimeComposition.RuntimePart.get_ImportingConstructor()上
在Microsoft.VisualStudio.Composition.RuntimeExportProviderFactory.RuntimeExportProvider.RuntimePartLifecycleTracker.CreateValue()中
在Microsoft.VisualStudio.Composition.ExportProvider.PartLifecycleTracker.Create()上
位于Microsoft.VisualStudio.Composition.ExportProvider.PartLifecycleTracker.MoveNext(PartLifecycleState下一个状态)
位于Microsoft.VisualStudio.Composition.ExportProvider.PartLifecycleTracker.MoveToState(PartLifecycleState requiredState)
在Microsoft.VisualStudio.Composition.ExportProvider.PartLifecycleTracker.GetValueReadyToExpose()上
在Microsoft.VisualStudio.Composition.RuntimeExportProviderFactory.RuntimeExportProvider.c__DisplayClass15_0.b__0()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在Microsoft.VisualStudio.Composition.RuntimeExportProviderFactory.RuntimeExportProvider.c__DisplayClass15_0.b__0()中
在Microsoft.VisualStudio.Composition.DelegateServices.c__DisplayClass2_0`1.b_0()
在System.Lazy`1.CreateValue()处
在System.Lazy`1.LazyInitValue()处
在System.Lazy`1.get_Value()
在Microsoft.CodeAnalysis.CodeFixes.CodeFixesService.c__DisplayClass24_0.b__0()上
在System.Lazy`1.CreateValue()处
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Lazy`1.get_Value()
在Microsoft.CodeAnalysis.CodeFixes.CodeFixesService.d_u13.MoveNext()上
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.CodeAnalysis.CodeFixes.CodeFixesService.d_u12.MoveNext()上
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestizedActionsSourceProvider.Source.c_uuDisplayClass15_1.d.MoveNext()中

我怀疑VS MEF缓存了来自您的类的较旧版本的CIL令牌,并且