VSCode c#添加对自定义程序集的引用

Tob*_*ler 9 c# visual-studio-code

在Visual Studio代码中我只想添加对自定义c#程序集的引用

"../libs/mylib.dll"

我该如何添加这种依赖?

我试图添加路径到依赖,但无法编译,因为它只是错误的:)

"dependencies": {
    "myassembly" : "../libs/Common.dll"
  },
Run Code Online (Sandbox Code Playgroud)

要么

"dependencies": {
    "log4net" : { "assembly":"../libs/log4net.dll" }
  },
Run Code Online (Sandbox Code Playgroud)

小智 13

更简单,只需添加以下内容:

1)修改myproject.csproj文件

    <ItemGroup>
     <Reference Include="DllComunVb2008">
       <HintPath>..\Dlls\DllComunVb2008.dll</HintPath>
     </Reference>
    </ItemGroup>
Run Code Online (Sandbox Code Playgroud)

2)添加using您要使用的库.例:using Dllcomun;


Tob*_*ler 5

我终于找到了一种方法来引用visual studio代码中的任何.net程序集.

首先要注意:我只需要智能感知的vscode.我不会在vscode /.netcore中编译程序集.当我完成编码时,我将使用命令行工具来生成我的程序集.

这是我的解决方案:

  1. 使用Visual Studio(而不是代码)创建一个常规的.net类库这将创建一个myproject.csproj文件(可以通过vscode读取). 或者使用帖子底部的test.csproj文件.

  2. 为引用的程序集创建一个文件夹.我刚刚在顶级目录中创建了一个libs -directory.

  3. 关闭vs并使用vscode打开文件夹.

  4. 修改*.csproj文件如下:

注意:我们已经在调试模式下创建了项目,因此我们可以删除release-property-group:

4.2.删除Release-PropertyGroup(你不必,但你不需要它)

<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>
Run Code Online (Sandbox Code Playgroud)

4.3.修改bin-output-path到libs-directory

<OutputPath>bin\Debug\</OutputPath>
Run Code Online (Sandbox Code Playgroud)

<OutputPath>libs</OutputPath>
Run Code Online (Sandbox Code Playgroud)

4.4.将您的引用.net程序集(外部或自定义)放在libs目录中并引用它们,如:

...
</PropertyGroup>
  <ItemGroup>
    <Reference Include="log4net">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>log4net.dll</HintPath>
    </Reference>
    ...
  </ItemGroup>
...
Run Code Online (Sandbox Code Playgroud)

这是一个完整的*.csproj文件,引用了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>
Run Code Online (Sandbox Code Playgroud)