Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add project's reference to another project in same solution dynamically

I have a solution and three projects inside that which is all created dynamically. Now I have to add the first project's reference to the second and third one. When i tried the logic i'm able to build the project individually and when building the solution it throws me the "Reference could not be loaded properly" error. This happens when the .dll is added directly using theVSProject.References.Add() method.

So i tried of using the below code to which is posted as a solution to the above issue. But still i'm getting some error.

Imports VSLangProj
' Add the second project as a reference to the first project.
Sub AddProjectExample()
   ' First project is a Visual Basic or C# project.
   Dim theVSProject As VSProject = _
      CType(DTE.Solution.Projects.Item(1).Object, VSProject)

   ' Second project is any type of project.
   Dim secondProject As Project = DTE.Solution.Projects.Item(2)

   ' AddProject returns the newly created Reference object.
   Dim newReference As Reference
   newReference = theVSProject.References.AddProject(secondProject)
End Sub

The error is,

The referenced assembly 'EnvDTE, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' could not be found. 

Any solution for this?

We don't have permission for VS IDE folder for applying the below fix.

Note: Have also tried the below solution for application App.Config but still there is issue.

msdn

like image 577
A Coder Avatar asked Mar 11 '14 09:03

A Coder


People also ask

What is the difference between project reference and DLL reference?

Well, project references are helpful when you are building and testing in both debug and release mode. If you directly add a DLL then you are locked into whatever that particular DLL was built as. The project reference allows this to be a build time decision. This is correct what you are saying.

Can two projects reference each other C#?

The easy way out: Create a shared library where you put in your shared code of P1 and P2. This shared project can be referenced by both P1 and P2.


1 Answers

NWard is absolutely right. Not sure about the code you're trying to use to add the references but the csproj file in the project root can be edited programmatically to add the following in its rightful place:

<ItemGroup>
    <ProjectReference Include="..\MyOtherProj\MyOtherProj.csproj">
      <Project>{abda85bb-2019-4b80-982e-b90add3fcca0}</Project>
      <Name>MyOtherProj</Name>
    </ProjectReference>
  </ItemGroup>

Call an internally invoked powershell script or some console exe. Hope this helps!

like image 197
Vaibhav Avatar answered Nov 15 '22 23:11

Vaibhav