question

TomW-9429 avatar image
0 Votes"
TomW-9429 asked ·

Save Visual Studio extended assembly reference properties in C# projects

Using the following site I was able to add custom properties to assembly references in a C# project: https://lehmamic.wordpress.com/2013/07/10/extending-cs-file-properties-in-a-vspackage/.

This blog page also explains how extended properties on file items can be saved in the csproj file. However, I would like to save changes made to custom properties on references instead of files. How can I retrieve the 'IVsBuildPropertyStorage' for a particular 'VSLangProj.Reference' instance?

VSLangProj.Reference reference = ...;
IVsBuildPropertyStorage buildPropertyStorage = ?????
buildPropertyStorage.SetItemAttribute(....);

vs-extensions
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DylanZhu-MSFT avatar image
0 Votes"
DylanZhu-MSFT answered ·

Hi TomW,

I try to write a function, which is based on this sample, maybe it can help you:

        private void GetProjectReferences(Project project)
         {
             var vsProject = project.Object as VSLangProj.VSProject;
             var references = vsProject.References as VSLangProj.References;
    
             string uniqueName = project.UniqueName;
             IVsSolution solution = (IVsSolution)Package.GetGlobalService(typeof(SVsSolution));
             IVsHierarchy hierarchy;
             solution.GetProjectOfUniqueName(uniqueName, out hierarchy);
             IVsBuildPropertyStorage buildPropertyStorage = hierarchy as IVsBuildPropertyStorage;
    
             for (int i= 0; i < references.Count; i++)
             {
                 var reference = references.Item(i);
    
                 if (buildPropertyStorage != null && reference.Name == "ClassLibrary1")
                 {
                     uint itemId;
                     string fullPath = reference.Path;
                     hierarchy.ParseCanonicalName(fullPath, out itemId);
                     buildPropertyStorage.SetItemAttribute(itemId, "MyAttribute", "MyValue");
                     break;
                 }
             }
         }

Best Regards,
Dylan


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 4 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@DylanZhu-MSFT thanks a lot for your anwser!

 string fullPath = reference.Path;
 hierarchy.ParseCanonicalName(fullPath, out itemId);

What will happen here if there are two references with the same path? (with a different version and alias, for example)

0 Votes 0 ·

Hi @TomW-9429 , as far as I know, we cannot put two assemblies with same name in one directory and reference them in one project. If it is wrong, please correct me.

 if (buildPropertyStorage != null && reference.Name == "ClassLibrary1")
 {
 }
0 Votes 0 ·

@DylanZhu-MSFT
Just tested your code, but 'itemID' is always 0, which indicates that it could not find the path in the hierarchy.
I think the method expects the path of a file in the project, not the path to a DLL reference.

                  string fullPath = reference.Path;
                  hierarchy.ParseCanonicalName(fullPath, out itemId);
0 Votes 0 ·

Hi @TomW-9429 , thank you for feedback. I test it in a sample, and the SetItemAttribute method cannot work for assembly file. And I found a thread, maybe it can help you: https://stackoverflow.com/a/51512487/11557747

0 Votes 0 ·