开发者

How to get all possible generic type in StructureMap?

开发者 https://www.devze.com 2022-12-22 13:56 出处:网络
I just used StructureMap few days ago. I use StructureMap for collecting all validator class like the following code.

I just used StructureMap few days ago. I use StructureMap for collecting all validator class like the following code.

public class BaseClassA {}

public class ClassB : BaseClassA {}

public class ClassC : BaseClassB {}

public BaseClassAValidator : IValidator<BaseClassA>() {}开发者_如何学Python

In StructureMap, I only register IValidator<BaseClassA> interface for BaseClassAValidator class. But I want to get the same result when I call IValidator<ClassB> or IValidator<ClassC> that mean StructureMap should return IValidator<T> where T is requested class or parent class of requested class.

Is it possible? Or I need to manually call it.


From the answer to How to use a convention for IRepository<T> with StructureMap mapping

var c = new Container(x =>
{
    x.Scan(scan =>
    {
        // there are other options to expand which assemblies to scan for types
        scan.TheCallingAssembly(); 
        scan.ConnectImplementationsToTypesClosing(typeof (IValidator<>));
    });
});

Now if you request IValidator<ClassC>, you should get BaseClassCValidator.

0

精彩评论

暂无评论...
验证码 换一张
取 消