(无意对编码风格发起宗教 war ,我只需要解决手头的问题)
我刚刚被要求更改整个解决方案中的所有变量声明以使用显式类型,即:
Dim Result = 3 + 5
应转换为:
Dim Result As Integer = 3 + 5
显然,这不能通过查找/替换甚至使用正则表达式来完成。我想到的另外两种可能性是:
有没有其他方法可以使这项乏味的工作自动化?该解决方案包含数十万行代码,因此无法手动完成。
这是针对 VS2017 社区的。
最佳答案
最后,我能够使用多种技术解决我的问题。这是我所做的,以防万一这有助于其他人:
Dim X1 = 45 + 43 'Type can be inferred by the reader easily
Dim X2 = DirectCast(obj, DataReader) 'Type explicitly mentioned on RHS
Dim X3 = New SomeClass() 'Type explicitly mentioned on RHS
Dim X4 = SomeMethod() 'Cannot infer X4's type without peeking or using Go To Definition
Dim \w+ = (?!(New|\d|"|DirectCast))
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using System.Linq;
using Microsoft.CodeAnalysis.VisualBasic;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
public class C : VisualCommanderExt.ICommand
{
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
serviceProvider = package as System.IServiceProvider;
Microsoft.VisualStudio.Text.Editor.IWpfTextView textView = GetTextView();
Microsoft.VisualStudio.Text.SnapshotPoint caretPosition = textView.Caret.Position.BufferPosition;
Microsoft.CodeAnalysis.Document document = caretPosition.Snapshot.GetOpenDocumentInCurrentContextWithChanges();
Microsoft.CodeAnalysis.VisualBasic.Syntax.LocalDeclarationStatementSyntax invocationExpressionNode =
document.GetSyntaxRootAsync().Result.FindToken(caretPosition).Parent.AncestorsAndSelf().
OfType<Microsoft.CodeAnalysis.VisualBasic.Syntax.LocalDeclarationStatementSyntax>().FirstOrDefault();
if (invocationExpressionNode != null)
{
Microsoft.CodeAnalysis.SemanticModel semanticModel = document.GetSemanticModelAsync().Result;
var VD = invocationExpressionNode.ChildNodes().FirstOrDefault(c => c.IsKind(SyntaxKind.VariableDeclarator));
if (VD != null)
{
var EV = VD.ChildNodes().FirstOrDefault(c => c.IsKind(SyntaxKind.EqualsValue) || c.IsKind(SyntaxKind.EqualsExpression));
if (EV != null)
{
object IE = EV.ChildNodes().FirstOrDefault(c => c.IsKind(SyntaxKind.InvocationExpression));
if (IE != null)
{
Microsoft.CodeAnalysis.IMethodSymbol methodSymbol =
semanticModel.GetSymbolInfo((InvocationExpressionSyntax)IE).Symbol as Microsoft.CodeAnalysis.IMethodSymbol;
string TypeName = methodSymbol.ReturnType.ToString();
string ToInsert = " As " + TypeName;
textView.TextBuffer.Insert(((InvocationExpressionSyntax)IE).SpanStart - 2, ToInsert);
}
}
}
}
}
private Microsoft.VisualStudio.Text.Editor.IWpfTextView GetTextView()
{
Microsoft.VisualStudio.TextManager.Interop.IVsTextManager textManager =
(Microsoft.VisualStudio.TextManager.Interop.IVsTextManager)serviceProvider.GetService(
typeof(Microsoft.VisualStudio.TextManager.Interop.SVsTextManager));
Microsoft.VisualStudio.TextManager.Interop.IVsTextView textView;
textManager.GetActiveView(1, null, out textView);
return GetEditorAdaptersFactoryService().GetWpfTextView(textView);
}
private Microsoft.VisualStudio.Editor.IVsEditorAdaptersFactoryService GetEditorAdaptersFactoryService()
{
Microsoft.VisualStudio.ComponentModelHost.IComponentModel componentModel =
(Microsoft.VisualStudio.ComponentModelHost.IComponentModel)serviceProvider.GetService(
typeof(Microsoft.VisualStudio.ComponentModelHost.SComponentModel));
return componentModel.GetService<Microsoft.VisualStudio.Editor.IVsEditorAdaptersFactoryService>();
}
private System.IServiceProvider serviceProvider;
}
GoToFindResult1NextLocation
和 GoToFindResult1PrevLocation
命令并遍历查找结果,修复我需要的结果。 最后,我能够在几分钟内解决解决方案中的所有问题。此外,我还学到了很多有关 Roslyn API 结构的知识。
关于vb.net - 将所有变量更改为显式类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48202311/