Autofac使用开放泛型类解析开放泛型接口 [英] Autofac Resolve Open Generic Interface with Open Generic Class

查看:378
本文介绍了Autofac使用开放泛型类解析开放泛型接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个接口和类:

So I have an interface and class:

public interface IMyInterface<T> where T : ISomeEntity {}

public class MyClass<T> : IMyInterface<T>
    where T : ISomeEntity {}

我将有一些需要的班级:

I will have some class that calls for it:

public class SomeClass : ISomeClass
{
    public SomeClass (IMyInterface<AuditEntity> myInterface) {}
}

我做了各种各样的事情来让它注册开放的通用接口和类,但是没有运气.

I've done all sorts of things to get it to register the open generic interface and class with no luck.

我只想说些类似的话:

container.RegisterType(typeof(MyClass<>)).As(typeof(IMyInterface<>));

如果我必须经历并明确地执行以下操作,那将很烦人:

It would be annoying if I have to go through and explicitly do something like:

container.RegisterType<MyClass<AuditEntity>>().As<IMyInterface<AuditEntity>>();

这不重要吗?

推荐答案

您必须使用RegisterGeneric方法,请参见

You have to use the RegisterGeneric method see Registration Concepts - Open Generic Components

类似的东西应该可以工作:

Something like this should works :

builder.RegisterGeneric(typeof(MyClass<>)).As(typeof(IMyInterface<>)); 

这篇关于Autofac使用开放泛型类解析开放泛型接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆