简体   繁体   English

通用类型autofac更改类型

[英]Generic type autofac change type

i work with autofac with generic type injection, I want to call a function in base DI with another generic type injection. 我使用带有泛型注入的autofac,我想用另一个泛型注入在基本DI中调用一个函数。 how i do it? 我该怎么做? it's possible? 这是可能的?

for example: 例如:

public class FooRepository : BaseRepository<Foo>
{
    public FooRepository(IUser user) : base(user)
    {
    }

     public override int Add(Foo entity)
    {
        var d =base.Add(entity)

        //how declare BaseRepository<Bar> 
        var c = ?????.Add(entity.Bar);

        return d;

    }
 }

Somewhat alike typeof(BaseRepository<>).MakeGenericType(typeof(Bar)) may or may not work depending upon context, which you did not actually specify. 有点像typeof(BaseRepository<>).MakeGenericType(typeof(Bar))可能会或可能不会运行,具体取决于您未实际指定的上下文。 Be aware that I provided a reflection-driven solution, certainly slower than a normal subclassing. 请注意,我提供了一个反射驱动的解决方案,肯定比普通的子类要慢。

i found simple solution 我找到了简单的解决方案

public class FooRepository : BaseRepository<Foo>
{
   IBaseRepository<Bar> _barep;

  public FooRepository(IUser user, IBaseRepository<Bar> barep) : base(user)
   {
    _barep=barep;
   }

 public override int Add(Foo entity)
{
    var d =base.Add(entity)

    //how declare BaseRepository<Bar> 
    var c = barep.Add(entity.Bar);

    return d;

}

} }

thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM