我正在尝试创建一个使用 IOptions<T>
的通用方法作为参数,但代码似乎无效。
Visual Studio 显示以下消息:
TConfig must be a non-abstract type with a non-parameterless constructor in order to use it as a parameter 'TOption' in the generic type or method
'IOptions<TOptions>'
如有任何关于如何解决此问题的建议,我们将不胜感激。谢谢!!
public static IServiceCollection AddConfigurations<TConfig>(
this IServiceCollection services, IConfiguration configuration,
IOptions<TConfig> options)
where TConfig : class
{
}
最佳答案
问题是,IOptions
具有以下约束:
where TOptions : class, new()
所以你也需要这个约束(new()
):
public static IServiceCollection AddConfigurations<TConfig>(
this IServiceCollection services, IConfiguration configuration,
IOptions<TConfig> options)
where TConfig : class, new()
{
}
关于c# - 如何在泛型方法中使用 IOptions<T> 作为泛型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56677936/