Adding reference to another project from visual st

2020-05-26 06:07发布

问题:

If a library (eg, on github) doesn't distribute itself via a nuget package, I'd have to manually include it as a reference, correct? I see a lot of reference posts for how to add a reference to a project for Visual Studio, but I can't seem to figure out how to do it on Visual Studio Code.

In this case, I've downloaded the library's zip, and moved the expanded folder into my project, and then tried using <namespace>, which did not work.

EDIT:

I noticed that this downloaded zip contained a .nuspec. Is there something I can do with this file extension to import it in my project?

回答1:

Let's say you have two projects:

1) Project1.Api

2) Project2.Executable


Command line syntax for dotnet add reference:

    cd Project2.Executable
    dotnet add reference ../Project1.Api/Project1.Api.csproj

If you check the Project2.Executable.csproj file, you will see the following entry:

    <ItemGroup>
         <ProjectReference Include = "..\Project1.Api\Project1.Api.csproj" />
    </ItemGroup>


回答2:

You can open .csproj file of the project you want to add reference to and add project reference like this:

<ItemGroup>
    <ProjectReference Include = "<RELATIVE_PATH_TO_REFERENCE_PROJECT>" />
</ItemGroup>

If the ItemGroup for ProjectReference already exist then you can just add to it.

Example:

<ItemGroup>
    <ProjectReference Include = "../MyLibrary.csproj" />
</ItemGroup>


回答3:

In visual studio, in the solution explorer, expand the project that will reference this other library. You will see "References", right click and choose "Add". Then choose browse on the left. Find your dll in your file system. If vs can't find the library you may need to unzip it. I've read where you may need to copy the dll into the bin folders, I recommend trying it without doing that, then copying it in to them if it fails without them.

Btw Googling "visual studio add reference" comes up with A LOT of great results.