VSCode c# add reference to custom assembly

前端 未结 3 2023
长情又很酷
长情又很酷 2021-01-11 15:04

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

相关标签:
3条回答
  • 2021-01-11 15:54

    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.

    0 讨论(0)
  • 2021-01-11 16:00

    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;

    0 讨论(0)
  • 2021-01-11 16:01

    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>
    
    0 讨论(0)
提交回复
热议问题