ObservableProperty breaks ExtractInterfaceCodeRefactoringProvider
#68,748 创建于 2023年6月23日
仓库指标
- Star
- (20,414 star)
- PR 合并指标
- (平均合并 6天 17小时) (30 天内合并 256 个 PR)
描述
Version Used: Visual Studio Enterprise 2022, v17.6.4
Steps to Reproduce:
- Create a new project with the "WPF Application" template for C#.
- Add the Communmity.Toolkit.Mvvm package (8.2.0).
- Add a partial public class and inherit from ObservableObject.
- Add a private field with the attribute ObservableProperty.
- Right click on the class name and choose the refactoring action "Extract interface".
public partial class ViewModel : ObservableObject
{
[ObservableProperty]
private int _myProperty;
}
A minimal repro, with source-code provided, is ideal. Using sharplab is preferred for compiler/language issues whenever possible.
Diagnostic Id: none
If this is a report about a bug in an analyzer, please include the diagnostic if possible (e.g. "IDE0030").
Expected Behavior:
The interface should be extracted from the class.
If I use this code - just with a public property with out using the ObservableProperty attribute, the interface could be extracted.
public partial class ViewModel : ObservableObject
{
public int MyProperty { get; set; }
}
Actual Behavior:
Visual Studio shows the following error:
"ExtractInterfaceCodeRefactoringProvider encountered an error and has been disabled."
System.InvalidOperationException : Operation is not valid due to the current state of the object.
at Microsoft.CodeAnalysis.Shared.Extensions.ISolutionExtensions.GetRequiredDocument(Solution solution,SyntaxTree syntaxTree)
at async Microsoft.CodeAnalysis.Shared.Utilities.AnnotatedSymbolMapping.CreateAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.ExtractInterface.AbstractExtractInterfaceService.ExtractInterfaceToNewFileAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.ExtractInterface.AbstractExtractInterfaceService.ExtractInterfaceFromAnalyzedTypeAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.ExtractInterface.ExtractInterfaceCodeAction.ComputeOperationsAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeActions.CodeActionWithOptions.GetOperationsAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.InvokeWorkerAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformActionAsync(<Unknown Parameters>)
I first reported this to the CommunitiyToolkit/dotnet Project: https://github.com/CommunityToolkit/dotnet/issues/726 but as @333fred mentioned there, this is a roslyn bug.