请参阅以下代码:
public interface IUserRepository:IRepository<User>
{
}
public abstract class GenericRepository<TEntity,Tkey>:IRepository<TEntity>,IDisposable
where TEntity:BaseEntity<Tkey>
where Tkey:class
public class UserRepository : GenericRepository<User, string>, IUserRepository
{
public UserRepository(SecurityDb dbContext) : base(dbContext)
{
}
}
我的问题是:我想找到UserRepository
到GenericRepository<>
和IUserRepository
到IRepository<>
。所以我做了这个功能:
public static IServiceCollection AddIgidiaService(this IServiceCollection service,IEnumerable<Assembly> Assemblies)
{
var types= Assemblies.SelectMany(a=>a.ExportedTypes).ToArray();
var repositories= types.Where(t=>typeof(GenericRepository<,>).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo()))
.Where(t => !t.GetTypeInfo().IsAbstract);
var irepositories=types.Where(t=>typeof(IRepository<>).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo()))
.Where(t=>t.GetTypeInfo().IsInterface);
foreach (var repository in repositories)
{
foreach (var irepositoriy in irepositories)
{
if (repository.GetTypeInfo().IsInstanceOfType(irepositoriy))
{
service.AddScoped(repository,irepositoriy);
}
}
}
return service;
}
此代码无法正常工作。