Save Visual Studio extended assembly reference properties in C# projects

Tom W 1 Reputation point
2021-02-21T13:27:43.5+00:00

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

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,251 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Dylan Zhu-MSFT 6,406 Reputation points
    2021-02-22T06:49:34.333+00:00

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