问题
Is there a way to copy the full type name of a class/interface/etc. in Visual Studio to the clipboard? In particular, I need them for Castle Windsor configuration and would love to figure out how to do this effortlessly. (Example: highlight IInterface in the code editor and end up with My.Full.Namespace.Is.Here.IInterface on the clipboard.)
VS puts the full type name in a read-only combobox in the upper left (which is utterly useless for copying purposes); does anyone know a way?
(I have ReSharper, if there's a way to do it using that.)
回答1:
Here's a macro which does which should get you going. The error handling is abysmal but I couldn't muster more, I absolutely hate VB :)
Also note that it only captures a class type name or interface type name, you can run it where ever you want while your cursor is inside a class or interface definition. It will capture the name of the class/interface scope.
It runs the Clipboard call in a thread, because it's a Windows Forms component and they need to run in a STAThread.
It copies the full typename to the clipboard.
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module SkurmedelMacros
Public Sub SetClipboard(ByVal x As Object)
System.Windows.Forms.Clipboard.SetText(CStr(x), System.Windows.Forms.TextDataFormat.Text)
End Sub
Public Sub GetTypeName()
Dim solution As Solution = DTE.Solution
Dim activePoint As TextPoint = CType(DTE.ActiveDocument.Selection, TextSelection).ActivePoint
Dim codeElem As CodeElement = _
DTE.ActiveDocument.ProjectItem.FileCodeModel.CodeElementFromPoint(activePoint, vsCMElement.vsCMElementClass)
If codeElem Is Nothing Then
codeElem = DTE.ActiveDocument.ProjectItem.FileCodeModel.CodeElementFromPoint(activePoint, vsCMElement.vsCMElementInterface)
End If
Dim ClipBoardThread As System.Threading.Thread = New System.Threading.Thread(AddressOf SetClipboard)
With ClipBoardThread
.ApartmentState = System.Threading.ApartmentState.STA
.IsBackground = True
.Start(codeElem.FullName)
.Join()
End With
ClipBoardThread = Nothing
End Sub
End Module
来源:https://stackoverflow.com/questions/4971997/vs-2010-easy-way-to-copy-full-type-name-of-a-class-interface