CSharpConvertAnonymousTypeToClassCodeRefactoringProvider throws exception when used

William Moore 0 Reputation points
2024-07-31T22:09:58.58+00:00

With either VS 2022 or VS 2022 Preview edition, I'm trying to use the Ctrl . Convert to class option. In either case, I see this error as I float over the option--I don't even need to select the option.image

at the top of the screen. The full text of the exception is:

System.NullReferenceException : Object reference not set to an instance of an object.
   at Microsoft.CodeAnalysis.Shared.Extensions.IAssemblySymbolExtensions.IsSameAssemblyOrHasFriendAccessTo(IAssemblySymbol assembly,IAssemblySymbol toAssembly)
   at Microsoft.CodeAnalysis.Shared.Extensions.ISymbolExtensions.IsNamedTypeAccessible(INamedTypeSymbol type,ISymbol within)
   at Microsoft.CodeAnalysis.Shared.Extensions.ISymbolExtensions.IsSymbolAccessibleCore(ISymbol symbol,ISymbol within,ITypeSymbol throughType,Boolean& failedThroughTypeCheck)
   at Microsoft.CodeAnalysis.GenerateEqualsAndGetHashCodeFromMembers.AbstractGenerateEqualsAndGetHashCodeService.CreateGetHashCodeStatements(SyntaxGenerator factory,SyntaxGeneratorInternal generatorInternal,Compilation compilation,INamedTypeSymbol namedType,ImmutableArray`1 members)
   at Microsoft.CodeAnalysis.GenerateEqualsAndGetHashCodeFromMembers.AbstractGenerateEqualsAndGetHashCodeService.CreateGetHashCodeMethod(SyntaxGenerator factory,SyntaxGeneratorInternal generatorInternal,Compilation compilation,INamedTypeSymbol namedType,ImmutableArray`1 members)
   at async Microsoft.CodeAnalysis.GenerateEqualsAndGetHashCodeFromMembers.AbstractGenerateEqualsAndGetHashCodeService.GenerateGetHashCodeMethodAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.ConvertAnonymousType.AbstractConvertAnonymousTypeToClassCodeRefactoringProvider`6.GenerateFinalNamedTypeAsync[TExpressionSyntax,TNameSyntax,TIdentifierNameSyntax,TObjectCreationExpressionSyntax,TAnonymousObjectCreationExpressionSyntax,TNamespaceDeclarationSyntax](<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.ConvertAnonymousType.AbstractConvertAnonymousTypeToClassCodeRefactoringProvider`6.ConvertAsync[TExpressionSyntax,TNameSyntax,TIdentifierNameSyntax,TObjectCreationExpressionSyntax,TAnonymousObjectCreationExpressionSyntax,TNamespaceDeclarationSyntax](<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputePreviewOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass16_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,119 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,928 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tianyu Sun-MSFT 30,566 Reputation points Microsoft Vendor
    2024-08-01T09:24:25.5966667+00:00

    Hello @William Moore,

    Welcome to Microsoft Q&A forum.

    Please click "Enable" to enable that feature first and then try following methods:

    1. Close VS, run VS as administrator, create a new C# project and test again.
    2. If you are using any extensions, try to disable them and test again.
    3. Try to repair VS 2022 and VS 2022 Preview from VS Installer > More > Repair.
    4. Try to reset VS settings by running devenv /ResetSettings in Developer Command Prompt for VS 2022 and Developer Command Prompt for VS 2022 Preview.
    5. Check if there’s any limitation on your machine, like permission limitations, group policy, security management software which may block some features of VS.

    Besides, are you using the latest version of VS 2022(17.10.5)/VS 2022 Preview(17.11.0 Preview 6.0)? If not, please try to update VS.

    Feel free to let us know the results.

    Best Regards,

    Tianyu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.