[英]Is there any function or tool (free) to auto complete any .Net type name to fully qualified one?
I'm using VB.NET with Visual Studio 2010. Is there any function or tool to auto complete any.Net type name to fully qualified one?我正在将 VB.NET 与 Visual Studio 2010 一起使用。是否有任何 function 或工具可以自动将 any.Net 类型名称完成为完全合格的名称?
I mean a tool/function that actually changing the source code from short hand to fully qualified name like following:我的意思是一个工具/功能,它实际上将源代码从简写更改为完全限定名称,如下所示:
From String
to System.String
从String
到System.String
From Process.Start
to this System.Diagnostics.Process.Start
从Process.Start
到这个System.Diagnostics.Process.Start
UPDATE:更新:
Thanks for ReSharper suggestion.感谢 ReSharper 的建议。 But I'm not prepared to buy it or award bounty for a commercial product suggestion.但我不准备购买它或奖励商业产品建议。 Sorry about that .对此感到抱歉。 I should make my question clear about that requirement.我应该清楚地说明我的问题。 I already tried ReSharper before asking this question.在问这个问题之前,我已经尝试过 ReSharper。
Resharper is capable of doing this. Resharper能够做到这一点。
Although Resharper is not free, it is absolutely worth buying.虽然 Resharper 不是免费的,但绝对值得购买。 Once started using Resharper, you will not be able to proceed without it.一旦开始使用 Resharper,没有它您将无法继续。
Showing the step by step below, because it is not quite trivial how to do this:下面逐步显示,因为如何做到这一点并不简单:
Open VS and go to menu Rehsarper
=> Options
=> Languages
=> C#
=> Namespace Import
.打开 VS 和 go 到菜单Rehsarper
=> Options
=> Languages
=> C#
=> Namespace Import
。 Select Use fully qualified names
. Select Use fully qualified names
。 By default Insert using directives when necessary
is selected.默认情况下Insert using directives when necessary
被选中。
From now on, whenever you use Alt+Enter
to resolve namespace, Resharper will resolve fully qualified one.从现在开始,每当您使用Alt+Enter
解析命名空间时,Resharper 都会解析完全限定的命名空间。
Go to Rehsarper
=> Options
=> Tools
=> Code cleanup
. Go 到Rehsarper
=> Options
=> Tools
=> Code cleanup
。 Hit Add
button to add new Cleanup profile.点击Add
按钮添加新的清理配置文件。 Give a name to the new profile, eg Namespaces Reformat
.为新配置文件命名,例如Namespaces Reformat
。 Check Optimizing using directives
checkbox.选中Optimizing using directives
复选框。
Right click on the project and select Cleanup Code...
, or use shortcut Ctrl+E, Ctrl+C
.右键单击项目和 select Cleanup Code...
,或使用快捷键Ctrl+E, Ctrl+C
。 Select previously created profile and hit Run
button. Select 之前创建的配置文件并点击Run
按钮。
Test测试
Original code:原始代码:
using System;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Test");
}
}
}
Result code:结果代码:
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Test");
}
}
}
Here is a macro that works fine for C# code.这是一个适用于 C# 代码的宏。
When I tested it on a VB.Net project it didn't work for types in assembly references.当我在 VB.Net 项目上对其进行测试时,它不适用于程序集引用中的类型。 Seems like the project code model for VB.Net excludes external types.似乎 VB.Net 的项目代码 model 不包括外部类型。
I include the code here anyway.无论如何,我在这里包含代码。 Maybe someone else knows how to do it for VB也许其他人知道如何为 VB 做
First we need a function that finds the current context首先我们需要一个找到当前上下文的 function
Private Function FindCodeElement(ByVal caretPosition As TextPoint, ByVal elems As CodeElements) As CodeElement
If elems Is Nothing Then Return Nothing
Return elems.Cast(Of CodeElement) _
.Where(Function(x) x.StartPoint.LessThan(caretPosition) AndAlso _
x.EndPoint.GreaterThan(caretPosition)) _
.Select(Function(x) If(FindCodeElement(caretPosition, GetMembers(x)), x)) _
.FirstOrDefault()
End Function
We also need a function that create all candidate names based on using/import statements我们还需要一个基于 using/import 语句创建所有候选名称的 function
Private Sub FindAllCandidates(ByVal elem As Object, ByVal className As String)
If TypeOf elem Is CodeFunction Then
FindAllCandidates(CType(elem, CodeFunction).Parent, className)
ElseIf TypeOf elem Is CodeClass Then
mCandidates.Add(CType(elem, CodeClass).FullName & "." & className)
FindAllCandidates(CType(elem, CodeClass).Parent, className)
ElseIf TypeOf elem Is CodeStruct Then
mCandidates.Add(CType(elem, CodeStruct).FullName & "." & className)
FindAllCandidates(CType(elem, CodeStruct).Parent, className)
ElseIf TypeOf elem Is CodeNamespace Then
mCandidates.Add(CType(elem, CodeNamespace).FullName & "." & className)
For Each ns As String In CType(elem, CodeNamespace).Members.OfType(Of CodeImport) _
.Select(Function(x) x.Namespace)
mCandidates.Add(ns & "." & className)
Next
FindAllCandidates(CType(elem, CodeNamespace).Parent, className)
ElseIf TypeOf elem Is FileCodeModel Then
For Each ns As String In CType(elem, FileCodeModel).CodeElements.OfType(Of CodeImport) _
.Select(Function(x) x.Namespace)
mCandidates.Add(ns & "." & className)
Next
End If
End Sub
And then a function that loops all available items to find one of the candidates然后是一个 function 循环所有可用项目以找到其中一个候选人
Private Function FindClassInCodeElements(ByVal elems As CodeElements) As CodeElement
If elems Is Nothing Then Return Nothing
For Each elem As CodeElement In elems
If IsClassType(elem) Then
If mCandidates.Contains(elem.FullName) Then Return elem
ElseIf TypeOf elem Is CodeNamespace Then
For Each candidate As String In mCandidates
If candidate.StartsWith(elem.FullName) Then
Dim found As CodeElement = FindClassInCodeElements(GetMembers(elem))
If found IsNot Nothing Then Return found
Exit For
End If
Next
End If
Next
Return Nothing
End Function
Two small helper functions两个小辅助函数
Private Function IsClassType(ByVal elem As CodeElement) As Boolean
Return TypeOf elem Is CodeClass OrElse TypeOf elem Is CodeStruct OrElse TypeOf elem Is CodeInterface
End Function
Private Function GetMembers(ByVal elem As CodeElement) As CodeElements
If TypeOf elem Is CodeClass Then
Return CType(elem, CodeClass).Members
ElseIf TypeOf elem Is CodeNamespace Then
Return CType(elem, CodeNamespace).Members
ElseIf TypeOf elem Is CodeStruct Then
Return CType(elem, CodeStruct).Members
ElseIf TypeOf elem Is CodeInterface Then
Return CType(elem, CodeInterface).Members
End If
Return Nothing
End Function
And then we can write the main function which you can alter depending on how you want to use it.然后我们可以编写主要的 function,您可以根据自己的使用方式对其进行更改。
Dim mCandidates As New HashSet(Of String)
Sub ExpandFullNameOfSelection()
Dim selection As EnvDTE.TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
' Assume type is selected
Dim className As String = selection.Text
' Find current context
Dim currentFunction As CodeElement = FindCodeElement(selection.ActivePoint, DTE.ActiveDocument.ProjectItem.FileCodeModel.CodeElements)
mCandidates.Clear()
FindAllCandidates(currentFunction, className)
Dim classType As CodeElement = DTE.Solution.Projects.Cast(Of Project) _
.Where(Function(x) x.CodeModel IsNot Nothing) _
.Select(Function(x) FindClassInCodeElements(x.CodeModel.CodeElements)) _
.FirstOrDefault(Function(x) x IsNot Nothing)
If classType IsNot Nothing Then
selection.Text = classType.FullName ' replace with full name
End If
End Function
JetBrains' ReSharper can do this via the "Code Cleanup" function , along with many other code changes (such as explicit type usage instead of var
etc.). JetBrains 的 ReSharper 可以通过“代码清理” function以及许多其他代码更改(例如显式类型使用而不是var
等)来执行此操作。
Note that I'm not affiliated, but a big fan of R#.请注意,我不是附属机构,而是 R# 的忠实粉丝。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.