简体   繁体   中英

How to reference System.Windows.Forms from a net462 targeting SDK project

Is there any possibility of referencing System.Windows.Forms from an Sdk project (VS 2017) that targets net462 ? Something like:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net462</TargetFrameworks>
    ...    
  </PropertyGroup>
  <PackageReference Include="System.Windows.Forms" Version="4.0.0" />
...

Or to generalize the question: how can system libraries residing in the GAC be used from Sdk projects? According to this discussion it seems it is not something supported out-of-the-box.

The part that confuses me is that targeting net462 gives the impression that one has access to the full framework. However, if GAC assemblies are an issue, the framework is not so "full" anymore. Thus, my question.

The following compiles for me:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net462</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="System.Windows.Forms" />
  </ItemGroup>

</Project>
using System.Windows.Forms;

namespace ClassLibrary1
{
    public class Class1
    {
        public Class1()
        {
            var form = new Form();
        }
    }
}

Note : Instead of editing the project file directly, you can add these references the same way you would in a Framework project, by right clicking on Dependencies in the Solution Explorer and choosing Add Reference. Click on Assemblies and then Framework, and select the assemblies you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM