22

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" }
  },
Tobias Koller
  • 2,116
  • 4
  • 26
  • 46
  • 1
    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? – rory.ap Dec 29 '16 at 13:57
  • 1
    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 how – Tobias Koller Dec 29 '16 at 14:06
  • 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. – Tobias Koller Dec 29 '16 at 14:22
  • FYI everyone - @Tobias has tagged this as Visual Studio Code not the normal VS. – garfbradaz Dec 29 '16 at 14:23
  • @TobiasKoller: Please can you add what you added to the project.json. – garfbradaz Dec 29 '16 at 14:24
  • @rory.ap: Can you remove that Google Link please as thats linking to Visual Studio and not Visual Studio Code :) – garfbradaz Dec 29 '16 at 14:25
  • Are these .net core assemblies? VSCode only supports .NET Core – garfbradaz Dec 29 '16 at 14:43
  • no they are regular .net assemblies. If I would switch them to .net core, how would I have to add them to my project? – Tobias Koller Dec 29 '16 at 15:01
  • Possible duplicate of [How to reference assemblies using Visual Studio Code?](http://stackoverflow.com/questions/31888274/how-to-reference-assemblies-using-visual-studio-code) – Siva Gopal Dec 29 '16 at 16:28

4 Answers4

39

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;

user5226582
  • 1,946
  • 1
  • 22
  • 37
7

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 an regular .net class library with Visual studio (not code) This will create an myproject.csproj-file (wich can be read by vscode). Or use my test.csproj-file at the bottom of the post.

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

  3. close vs and open the folder with vscode.

  4. modify the *.csproj-file as followed:

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 referencing .net assembly (external or custom) in the libs-directory and references them like:

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

this is the complete *.csproj file with reference to an 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>
Tobias Koller
  • 2,116
  • 4
  • 26
  • 46
  • 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. – Lex Li Jan 07 '17 at 09:48
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.

David Morrow
  • 262
  • 4
  • 9
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

Andy
  • 339
  • 3
  • 17