0
我想在Autofac中註冊/解析泛型類型,它接收一個類型參數,它是一個接口並且已經被註冊了。Autofac使用接口作爲參數解析泛型類型
我有以下幾點:
public interface IBaseEntity
{
}
public partial interface IRepository<T>
where T : IBaseEntity
{
}
public class NhRepository<T> : IRepository<T>
where T : IBaseEntity
{
}
public interface IEntity : IBaseEntity
{
}
public class Entity : IEntity
{
}
註冊:
//registering type
builder.RegisterType<Entity>().As<IEntity>();
//registering generic type in Autofac
builder.RegisterGeneric(typeof(NhRepository<>))
.As(typeof(IRepository<>)).InstancePerRequest();
試圖解決:
//the code below returns NhRepository<IEntity>().
_containerManager.Resolve<IRepository<IEntity>>();
我想一個方法來獲得NhRepositoy<Entity>
代替NhRepository<IEntity>()
有什麼想法?
它不起作用,因爲您無法將NhRepository轉換爲IRepository ,無論實現各個接口的類如何。 –
2017-04-27 02:17:24