简体   繁体   中英

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. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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