VSCode c# add reference to custom assembly
Asked Answered
M

5

23

in Visual Studio Code I simply want to add a reference to an custom c# assembly like

"../libs/mylib.dll"

how can I add this dependency?

I tried to add the path to the dependency but was not able to compile because its just wrong :)

"dependencies": {
    "myassembly" : "../libs/Common.dll"
  },

or

"dependencies": {
    "log4net" : { "assembly":"../libs/log4net.dll" }
  },
Mcclain answered 29/12, 2016 at 13:50 Comment(9)
You're going to have to give us more information, which should have been obvious to you. What kind of project are you working with? How are you trying to use the custom assembly from your code? Why can't you just reference it the regular way, by adding a reference to your project? What research have you done?Dogmatize
I thought is a simple question. I have a HelloWorld project with an program.cs-file. In this cs-file I want to use a Class from another assembly. And the question is; how can I tell vscode to load my custom assembly. When you write "why can't you just reference it..." this is what I try to do but don't know howMcclain
why so angry? of course i googled it but could not found a single answer to that question. I spend about 1h just googling this question...I found some stackoverflow-threads but non to my question.Mcclain
FYI everyone - @Tobias has tagged this as Visual Studio Code not the normal VS.Calvo
@TobiasKoller: Please can you add what you added to the project.json.Calvo
@rory.ap: Can you remove that Google Link please as thats linking to Visual Studio and not Visual Studio Code :)Calvo
Are these .net core assemblies? VSCode only supports .NET CoreCalvo
no they are regular .net assemblies. If I would switch them to .net core, how would I have to add them to my project?Mcclain
Possible duplicate of How to reference assemblies using Visual Studio Code?Dissenter
O
44

simpler, just add the following:

1) modify the myproject.csproj file

    <ItemGroup>
     <Reference Include="DllComunVb2008">
       <HintPath>..\Dlls\DllComunVb2008.dll</HintPath>
     </Reference>
    </ItemGroup>

2) Add the using of the library you are going to use. Example: using Dllcomun;

Orate answered 20/9, 2017 at 6:2 Comment(2)
Thanks for your answer, however per stackoverflow rules all posts have to be in English.Czech
Where in my .csproj does this go? Putting it directly under <Project> results it it being silently ignored, but putting it under <PropertyGroup> results in the error error MSB4004: The "ItemGroup" property is reserved, and cannot be modified.Haematite
M
8

I finally found a way to reference any .net assembly within visual studio code.

First to note: I only need the vscode for the intellisense. I will not compile the assembly in vscode / .netcore. When I'm done with coding, I will use commandline-tools to generate my assembly.

And this is my solution:

  1. Create a regular .net class library with Visual studio (not code) This will create an myproject.csproj file (which can be read by vscode). Or use my test.csproj file at the bottom of the post.

  2. create a folder for the referenced assemblies. I've just created a libs directory inside the top directory.

  3. close vs and open the folder with vscode.

  4. modify the *.csproj file as follows:

note: we've created the project in debug-mode, so we can remove the release-property-group:

4.2. remove the Release-PropertyGroup (you don't have to, but you don't need it)

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>

4.3. Modify the bin-output-path to the libs directory

from

<OutputPath>bin\Debug\</OutputPath>

to

<OutputPath>libs</OutputPath>

4.4. put your referenced .net assembly (external or custom) in the libs directory and reference them like so:

...
</PropertyGroup>
  <ItemGroup>
    <Reference Include="log4net">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>log4net.dll</HintPath>
    </Reference>
    ...
  </ItemGroup>
...

this is the complete *.csproj file with a reference to a log4net.dll:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{75278D05-4850-4282-8AB4-3643A9E799FF}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Test</RootNamespace>
    <AssemblyName>Test</AssemblyName>
    <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>libs</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="log4net">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>log4net.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Runtime.Serialization" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="myassembly.cs" />
  </ItemGroup>
  <ItemGroup>
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>
Mcclain answered 7/1, 2017 at 8:43 Comment(1)
As Microsoft revamps MSBuild (the upcoming release in VS2017), you will soon see a simpler csproj format, where adding references (even NuGet packages) becomes much easier. That should help VSCode users a lot. Currently you have to live with the limitations.Butterworth
A
1

I create a project with dotnet and then open it with Visual Studio 2019. Then click project, Add reference, browse to Dll, then save project (save a solution incase you want to open it again with file) Now open it with vscode.

Agist answered 21/11, 2020 at 21:47 Comment(1)
visual studio doesn't work on linux (as of 2023) so this is not possible for everyoneEricaericaceous
A
1

I just came across this question, so I thought I would add a couple of helpful links for generally using the CLI. It still looks like you have to manually add a dll yourself...

https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-add-reference

If you are building the library and added it as part of the solution, then you can use the CLI to add references as and when required, see the following link

https://learn.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio-code?pivots=dotnet-7-0

You could also probably pack it into a package yourself, but unless you have your own feed, then you will need to do some similar custom .csproj changes

Archenteron answered 9/5, 2023 at 21:3 Comment(0)
H
0

Had the same problem when trying to reference a local project (rather than a nuget package), here are the steps I took.

  1. Be sure to install the C# DevKit extension. This enables you to see the solution explorer panel view.
  2. Create a solution file via the VSCode terminal by cd'ing to the directory you want your solution folder to live, and typing the following at the prompt

    dotnet new sln -o MyApiApp

  3. Restart VSCode after the install to see the new solution explorer side panel.
  4. [Optional]To Create a project hit ctrl+shift+p and type new project, selecting the .NET: New Project option.
  5. Right-click the your .csproj in solution explorer.
  6. Select "Add Project Reference"
  7. Choose the project/dll you want to reference.
Holotype answered 20/12, 2023 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.