I want to obtain the assembly name of a Project in an Visual Studio Extension.
Since I know that you can see it when right clicking on a Project in the Solution Explorer and select Properties
. It will show the Assemblyname
in the tab Application
.
I have a "Command" in my extension project which shows when you right click something in the solution explorer. As soon as someone clicks the command the following code will execute:
var dte = await GetDTE2Async();
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var selectedItem = GetSelectedItem(dte);
var props = selectedItem.ContainingProject.Properties;
//I just assumed that the `AssemblyName` could be somewhere in the `Properties` Property.
Where the GetSelectedItem
method looks like this:
public static ProjectItem GetSelectedItem(DTE2 dte)
{
ThreadHelper.ThrowIfNotOnUIThread();
var items = (Array)dte.ToolWindows.SolutionExplorer.SelectedItems;
return items.Cast<UIHierarchyItem>().FirstOrDefault().Object as ProjectItem;
}
For anyone else who has the same issue, I actually was pretty close to my goal.
It was just about guessing, in my case I assumed that the Properties
is a bunch of Property
items.
After that I just collected all the key pair values that the IEnumerable
had. Then saw that there is acutally an Item called AssemblyName
which indeed matches the correct value.
var items = selectedItem.ContainingProject.Properties.Cast<Property>()
var sb = new StringBuilder();
foreach (var item in items)
{
sb.Append($"Name : {item.Name} ");
try
{
sb.Append("Value : " + item.Value.ToString());
}
catch (Exception)
{
sb.Append("Value : NOT DEFINED (EXCEPTION)");
}
finally
{
sb.AppendLine();
}
}
var output = sb.ToString();
It is a fairly short part, but here we go:
var assemblyName = selectedItem.ContainingProject.Properties.Cast<Property>().FirstOrDefault(x=> x.Name == "AssemblyName").Value;
Basically I am just searching the first element which matches the name AssemblyName
and get its value.