![]() |
||||
|
This article describes how to get the .NET language of a project or file from a Visual Studio .NET macro or add-in. More Information The extensibility model of Visual Studio .NET provides the EnvDTE.Project class to represent a project, and its Project.CodeModel property returns an EnvDTE.CodeModel object representing the code model of the whole project. The EnvDTE.CodeModel class has a Language property to return the language of the project. In a similar way, the EnvDTE.ProjectItem class represents a project item (normally a file), its ProjectItem.FileCodeModel property returns an EnvDTE.FileCodeModel object representing the code model of the file, and the FileCodeModel.Language property returns the language of the file. Since .NET is an open platform which allows multiple languages (anyone can add a new .NET language), there is no enum value to group the languages together. Instead, each language is identified by a GUID string. This way the platform can accommodate languages that are not created yet. The extensibility model provides the EnvDTE.CodeModelLanguageConstants class to group some known language GUIDs, but in some cases you will have to hardcode the values of the GUIDs for other languages. The following macro illustrates these concepts: Sub GuessNetLanguage()
Dim sLanguageGuid As String
Try
' Get the language of a project
'sLanguageGuid = DTE.ActiveDocument.ProjectItem.ContainingProject.CodeModel.Language
' Get the language of a file
sLanguageGuid = DTE.ActiveDocument.ProjectItem.FileCodeModel.Language
Select Case sLanguageGuid
Case EnvDTE.CodeModelLanguageConstants.vsCMLanguageVB
MessageBox.Show("Visual Basic .NET")
Case EnvDTE.CodeModelLanguageConstants.vsCMLanguageCSharp
MessageBox.Show("C#")
Case EnvDTE.CodeModelLanguageConstants.vsCMLanguageVC
MessageBox.Show("Visual C++")
Case EnvDTE.CodeModelLanguageConstants.vsCMLanguageMC
MessageBox.Show("Managed C++")
Case EnvDTE80.CodeModelLanguageConstants2.vsCMLanguageJSharp ' Only Visual Studio 2005
MessageBox.Show("Visual J#")
End Select
Catch objException As System.Exception
MessageBox.Show(objException.ToString)
End Try
End Sub
Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this
|
|||||||||||||||||||||||||||||||||||||||||||||||||