本文记录 dotnet 10 新引入的问题。如果 TargetFramework 是 .NET 9 版本,而引用的 System.Drawing.Common 包是 10.0 版本,那么运行程序时,可能抛出找不到类型异常

此问题我已经在 WinForms 仓库反馈: https://github.com/dotnet/winforms/issues/14145

最简复现步骤如下:

先创建一个空的 .NET 项目,编辑 csproj 文件,替换为以下代码

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="System.Drawing.Common" Version="10.0.1" />
  </ItemGroup>
</Project>

创建 Program.cs 文件,添加如下代码

System.Windows.Forms.SendKeys.SendWait("Hello, World!");

将项目里面的其余文件全删掉。然后构建运行项目

此时可见抛出如下错误

System.TypeInitializationException: The type initializer for 'System.Windows.Forms.SendKeys' threw an exception.
   ---> System.TypeInitializationException: The type initializer for 'System.Windows.Forms.ScaleHelper' threw an exception.
   ---> System.TypeLoadException: Could not load type 'System.Private.Windows.Core.OsVersion' from assembly 'System.Private.Windows.Core, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
     at System.Windows.Forms.ScaleHelper.<InitializeStatics>g__GetPerMonitorAware|8_1()
     at System.Windows.Forms.ScaleHelper.InitializeStatics()
     --- End of inner exception stack trace ---
     at System.Windows.Forms.ScaleHelper.get_InitialSystemDpi()
     at System.Windows.Forms.Control..ctor(Boolean autoInstallSyncContext)
     at System.Windows.Forms.SendKeys.SKWindow..ctor()
     at System.Windows.Forms.SendKeys..cctor()
     --- End of inner exception stack trace ---
     at System.Windows.Forms.SendKeys.SendWait(String keys)
     at sendkey_error.MainWindow.Button_Click(Object sender, RoutedEventArgs e)

问题的原因是在 https://github.com/dotnet/winforms/pull/12839 里面修改了命名空间

这就导致了 OsVersion 类型从 System.Private.Windows.Core 命名空间改到 System.Private.Windows 命名空间,这里的不匹配就导致了类型找不到异常

本次问题属于情有可原,毕竟更新的是主版本号,主版本更新发生不兼容是合理的

此问题一开始是在 WPF 仓库报告的,但经过我调查,和 WPF 毫无关系,详细请看: https://github.com/dotnet/wpf/issues/11313

解决方法十分简单,保持 System.Drawing.Common 跟随主入口项目的 TargetFramework 版本,如修改 csproj 为如下代码

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="System.Drawing.Common" Version="9.0.11" />
  </ItemGroup>
</Project>

本文代码放在 githubgitee 上,可以使用如下命令行拉取代码。我整个代码仓库比较庞大,使用以下命令行可以进行部分拉取,拉取速度比较快

先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码

git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin 1534db9e5f807d2c958cc8f7509f9f45229651e5

以上使用的是国内的 gitee 的源,如果 gitee 不能访问,请替换为 github 的源。请在命令行继续输入以下代码,将 gitee 源换成 github 源进行拉取代码。如果依然拉取不到代码,可以发邮件向我要代码

git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
git pull origin 1534db9e5f807d2c958cc8f7509f9f45229651e5

获取代码之后,进入 WPFDemo/LaiwerelawkewaFeereajemle 文件夹,即可获取到源代码

更多技术博客,请参阅 博客导航


本文会经常更新,请阅读原文: https://blog.lindexi.com/post/dotnet-10-%E5%B7%B2%E7%9F%A5%E9%97%AE%E9%A2%98-WinForms-%E7%9A%84-TargetFramework-%E4%B8%8E-System.Drawing.Common-%E4%B8%8D%E5%8C%B9%E9%85%8D%E5%B0%86%E6%8A%9B%E5%87%BA%E6%89%BE%E4%B8%8D%E5%88%B0%E7%B1%BB%E5%9E%8B%E5%BC%82%E5%B8%B8.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者收藏我的博客导航

知识共享许可协议 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

微软最具价值专家


无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号

欢迎进入 Eleven 老师组建的 .NET 社区

以上广告全是友情推广,无盈利