Partager via


EnvDTE - Adding a reference to a project

It was confusing to see a property named "Object" on EndDTE.Project class (which returns an object of type System.Object), especially when the tooltip didnt provide any useful information. (It's actual type is a ComObject) Anyway, actually it returns an object of type VSLangProj.VSProject (assuming you have a reference to VSLangProj). I was just trying to add a reference to my project within the VS Package I was working on and that class gives me the functionality I was looking for. Long story short, in order to add a reference (GAC) programmatically to your project here's what needs to be done:

private void AddReference(EnvDTE.Project proj) {

VSLangProj.

VSProject vsProject = (VSLangProj.VSProject)proj.Object;

vsProject.References.Add(

"MyLibrary");

}

Comments

  • Anonymous
    July 30, 2008
    PingBack from http://blog.a-foton.ru/2008/07/envdte-adding-a-reference-to-a-project/

  • Anonymous
    May 01, 2009
    What if it has multiple versions? Example: I want to add system.configuration.install as a reference to the current project. it has a 1.0 and a 2.0 version in the GAC on my machine. Basically, I'm writing an add-on. I'm allowing it to save references and allow you to add them to your future projects. this is just an "extra" for the main app. now if i try to get a reference from a VSWebSite, I can't get the actual path, just the strong name (ex: "System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"). I was hoping I could add this reference on to a regular project using ((VSLangProj.VSProject)project.Object).References.Add(strStrongName) but it doesn't seem to work.

  • Anonymous
    May 01, 2009
    haha. got it to work. no idea why it wasn't working before. greeeeat.

  • Anonymous
    May 01, 2009
    Nope. looks like i can pass just the name, and it will pick the first one with that name. ugh.

  • Anonymous
    May 05, 2009
    I have tried adding a reference using a fully qualified assembly name like "System.Data.SqlServerCe, Version=3.5.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" and it worked. (I have the same assembly with another version in my GAC and it added a reference to the right one) Also, if you wish to add a reference to a project (not to a dll directly), I'd suggest you to take a look at the AddProject method. URL = http://msdn.microsoft.com/en-us/library/vslangproj.references.addproject(VS.80).aspx