c# - 使用泛型的构造函数中的参数创建类的实例
问题描述
使用 Asp.Net Core 3 有以下方法:
public static void AddLiteContext<TContext, TContextMapper>(this IServiceCollection services, String connection)
where TContext : ILiteContext
where TContextMapper : LiteContextMapper {
}
在此方法中,我需要以下内容:
services.AddTransient<Context>(x => new Context(connection, new ContextMapper()));
但我需要基于泛型创建 Context 和 ContextMapper 的实例:
ILiteContext
LiteContextMapper
请注意,类 Context 和类 ContextMapper 是这些泛型的实现:
public class Context : ILiteContext { }
public class ContextMapper : LiteContextMapper { }
我使用的方法如下:
services.AddLiteContext<Context, ContextMapper>(myConnection);
我怎样才能做到这一点?
解决方案
推荐阅读
- haskell - 如何定义一个简单的 RIO LogFunc 而不用括号执行
- apache - 如何通过反向代理将 IP 作为远程主机名转发到 Apache?
- tensorflow - 如何从 Tensorflow Model Zoo 迁移学习预训练模型
- python - 使用 python setuptools 库时的导入问题
- regex - 隔离以各种方式编写的大小,同时将其保持在第 1 组中
- node.js - 在 cassandra 中更新具有大量数据(8000 万+行)的表中的列
- java - 创建项目酒店管理系统时出错
- r - 当 Wilcoxon 检验返回一些 0 p 值时该怎么办?
- laravel-7 - Laravel Dusk 能够打开浏览器但无法控制它
- sql-server - 我可以像在 Azure SQL Database for SQL Server 本地获得 SQL 性能建议吗?