How to add references by automation
It's easy but I always forget the steps. I have to look in my code in order to remember. So here the steps:
1. Add a reference to VSLangProj. If you are writing a macro
you don't need this step.
2. Add the using clause
(C#)
using VSLangProj;
3. And use this function
public bool
AddReference(EnvDTE.Project project, string reference)
{
VSProject
proj = m_Project.Object
as
VSProject;
System.Diagnostics.Debug.Assert(proj
!= null);
// This project is not a VSProject
if (proj ==
null)
return
false;
try
{
proj.References.Add(reference);
}
catch
(Exception ex)
{
string
message = String.Format("Could not add {0}. \n Exception:
{1}", reference, ex.Message);
System.Diagnostics.Trace.WriteLine(message);
return
false;
}
return true;
}
In order to add a reference to a project you have to use proj.References.AddProject insted proj.References.Add.
If you wanna add a reference to an assembly that is in the GAC then pass just the dll name insted of the full path.