3

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.)

Joe
  • 41,484
  • 20
  • 104
  • 125
  • For those looking for an answer for VS2012 and above (macro support has been dropped) look here: http://stackoverflow.com/questions/6948428/vs-add-on-to-copy-full-type-name-of-identifier-under-the-cursor-into-clipboard/26364337#26364337 – sliderhouserules Nov 12 '14 at 19:58

1 Answers1

2

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
Skurmedel
  • 21,515
  • 5
  • 53
  • 66
  • I tried this, and the first call to get DTE.Solution fails with RPC_E_SYS_CALL_FAILED. – Joe Feb 12 '11 at 01:31
  • @Joe: Woot... what version of VS you got? – Skurmedel Feb 12 '11 at 01:37
  • 2010. I'm finding that the code here works fairly consistently for getting the class/interface... http://www.mztools.com/articles/2006/MZ2006009.aspx – Joe Feb 12 '11 at 02:06
  • Using that code, and using Clipboard.SetDataObject(CStr(x), True) in the external thread, I seem to have this working. I'm accepting this though since it put me on the right track. – Joe Feb 12 '11 at 02:17