Twenty
Twenty

Reputation: 5871

Get assemblyname of a project

What I need

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.

Setup

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;
}

Upvotes: 2

Views: 2229

Answers (2)

Twenty
Twenty

Reputation: 5871

For anyone else who has the same issue, I actually was pretty close to my goal.

How I found it

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();

Solution

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.

Upvotes: 2

melody zhou
melody zhou

Reputation: 148

Maybe you can try:

System.Reflection.Assembly.GetAssembly(Property's Type).FullName

Upvotes: -1

Related Questions