如果ActivatorUtilities.CreateInstance参数是ArgumentNullException,这两provider
方法现在会引发null
异常。
已引入的版本
.NET 8 预览版 1
以前的行为
允许为null
参数设定provider
值。 在某些情况下,仍正确创建了指定的类型。
新行为
当provider
为null
时,会抛出ArgumentNullException异常。
破坏性变更的类型
此更改为行为更改。
更改原因
我们修复了参数验证以及 构造函数匹配问题 ,以符合预期目的 CreateInstance。 这些 CreateInstance()
方法具有不可为 null 的 provider
参数,因此通常预期不允许使用 null
提供者。
建议的措施
为 IServiceProvider 参数传递一个非 null provider
。 如果提供程序还实现 IServiceProviderIsService,可以通过该提供程序获取构造函数参数。
或者,如果您的方案不需要依赖项注入,并且IServiceProvider是null
,则改为使用Activator.CreateInstance。
受影响的 API
- Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance<T>(IServiceProvider, Object[])
- Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider, Type, Object[])