简体   繁体   English

无法将程序集引用添加到控制台应用程序

[英]Unable to add assembly references to a Console Application

Currently I'm unable to add an assembly reference to my C# project in Visual Studio 2019.目前我无法在 Visual Studio 2019 中添加对我的 C# 项目的程序集引用。

I don't know if this is an error with Visual Studio itself or it's my fault.我不知道这是 Visual Studio 本身的错误还是我的错。

Currently my References page looks like this:目前我的参考页面如下所示: 添加项目参考

If I try to add a COM Reference then I get the following error of:如果我尝试添加 COM 引用,则会出现以下错误:

Severity    Code    Description Project File    Line    Suppression State
Warning MSB3290 Failed to create the wrapper assembly for type library "{215d64d2-031c-33c7-96e3- 
61794cd1ee61}". Type library 'System_Windows_Forms' was exported from a CLR assembly and cannot be 
re-imported as a CLR assembly.  MyProgram   C:\Program Files (x86)\Microsoft Visual 
Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets   2705    

All I want is to add a reference to Windows Forms.我只想添加对 Windows Forms 的引用。

Reference to a type library would indcate you are adding a COM reference.对类型库的引用将表明您正在添加 COM 引用。

But System.Windows.Forms is a .NET assembly, so you should be selecting it from the Assemblies list on the references manager (shown when you add reference).但是System.Windows.Forms是一个 .NET 程序集,因此您应该从引用管理器的程序集列表中选择它(添加引用时显示)。

If this is a .NET Core project then add the appropriate reference via NuGet (in project's context menu in solution explorer).如果这是一个 .NET Core 项目,则通过 NuGet 添加适当的引用(在解决方案资源管理器的项目上下文菜单中)。 But it also needs the right SDK version.但它还需要正确的 SDK 版本。

Probably easier to copy from a fresh WinForms project created from the VS New Project and see what NuGet references it included.从 VS 新项目创建的新 WinForms 项目复制并查看它包含哪些 NuGet 引用可能更容易。

Correction更正

For .NET Core 3.1 you don't need any references for the WinForms assemblies.对于 .NET Core 3.1,您不需要对 WinForms 程序集的任何引用。 Instead you need the right settings in your .csproj :相反,您需要在.csproj中进行正确的设置:

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>

There could possibly because of you are trying to refer a COM library build on different architecture than the project you want to get the COM reference.可能是因为您正在尝试引用一个 COM 库构建在与您想要获得 COM 引用的项目不同的体系结构上。 For example COM lib may be built on x64 and your project may be built upon x32.例如 COM 库可能构建在 x64 上,而您的项目可能构建在 x32 上。

Or will you please elaborate your issue or help me on how to reproduce it.或者请您详细说明您的问题或帮助我如何重现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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