【问题标题】:VSCode c# add reference to custom assemblyVSCode c#添加对自定义程序集的引用
【发布时间】:2017-05-13 20:37:14
【问题描述】:

在 Visual Studio Code 中,我只想添加一个引用 到像

这样的自定义 c# 程序集

"../libs/mylib.dll"

如何添加此依赖项?

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

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

"dependencies": {
    "log4net" : { "assembly":"../libs/log4net.dll" }
  },

【问题讨论】:

  • 您将不得不向我们提供更多信息,这对您来说应该是显而易见的。你正在处理什么样的项目?您如何尝试使用代码中的自定义程序集?为什么不能通过添加对项目的引用以常规方式引用它?你做了什么研究?
  • 我认为是一个简单的问题。我有一个带有 program.cs 文件的 HelloWorld 项目。在这个 cs 文件中,我想使用另一个程序集中的类。问题是;我如何告诉 vscode 加载我的自定义程序集。当你写“你为什么不能直接引用它......”时,这就是我尝试做的,但不知道如何
  • 为什么这么生气?当然,我用谷歌搜索了它,但找不到该问题的单一答案。我花了大约 1 小时在谷歌上搜索这个问题......我发现了一些 stackoverflow-threads 但不是我的问题。
  • 仅供参考 - @Tobias 已将其标记为 Visual Studio Code 而不是普通的 VS。
  • @TobiasKoller:请您将您添加的内容添加到 project.json 中。

标签: c# visual-studio-code


【解决方案1】:

我终于找到了一种在 Visual Studio 代码中引用任何 .net 程序集的方法。

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

这是我的解决方案:

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

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

  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>

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

来自

<OutputPath>bin\Debug\</OutputPath>

<OutputPath>libs</OutputPath>

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

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

这是参考 log4net.dll 的完整 *.csproj 文件。

<?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>

【讨论】:

  • 随着 Microsoft 改进 MSBuild(即将在 VS2017 中发布),您很快就会看到更简单的 csproj 格式,其中添加引用(甚至是 NuGet 包)变得更加容易。这应该对 VSCode 用户有很大帮助。目前你必须忍受这些限制。
【解决方案2】:

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

1)修改myproject.csproj文件

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

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

【讨论】:

  • 感谢您的回答,但是根据 stackoverflow 规则,所有帖子都必须是英文。
【解决方案3】:

我用 dotnet 创建了一个项目,然后用 Visual Studio 2019 打开它。然后单击项目,添加引用,浏览到 Dll,然后保存项目(保存一个解决方案,以防你想用文件再次打开它) 现在用 vscode 打开它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 2016-02-26
    • 1970-01-01
    相关资源
    最近更新 更多