如果 provider
参数为 null
,两种 ActivatorUtilities.CreateInstance 方法现在会引发 ArgumentNullException 异常。
引入的版本
.NET 8 预览版 1
旧行为
参数 provider
允许使用值 null
。 在某些情况下,仍然正确创建了指定的类型。
新行为
当 provider
为 null
时,会引发 ArgumentNullException 异常。
中断性变更的类型
此更改为行为更改。
更改原因
我们修复了参数验证以及构造函数匹配问题,以符合 CreateInstance 的预期用途。 CreateInstance()
方法具有一个不可为 null 的 provider
参数,因此通常认为不允许有 null
提供程序。
建议的操作
为 provider
参数传递一个非 null IServiceProvider。 如果提供程序还实现了 IServiceProviderIsService,那么构造函数参数可以通过它获得。
或者,如果你的方案不需要依赖关系注入(因为 IServiceProvider 是 null
),请改用 Activator.CreateInstance。
受影响的 API
- Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance<T>(IServiceProvider, Object[])
- Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider, Type, Object[])