通过泛型在构造函数中使用参数创建类的实例

时间:2020-04-17 15:39:39

标签: c# asp.net-core asp.net-core-3.0

使用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); 

我该怎么做?

0 个答案:

没有答案