Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Unity(AutoMapper提供的IValueResolver)动态注册接口的多个实现_C#_Automapper_Unity Container - Fatal编程技术网

C# 使用Unity(AutoMapper提供的IValueResolver)动态注册接口的多个实现

C# 使用Unity(AutoMapper提供的IValueResolver)动态注册接口的多个实现,c#,automapper,unity-container,C#,Automapper,Unity Container,我有一些代码,可以动态扫描程序集以实现AutoMapper Profile类,并构建一个映射器以与Unity DI一起使用。最近,我发现需要使用IValueResolver接口。该类需要在其中注入资源依赖项,以便在模型之间执行转换 我可以在automapper配置中添加一行代码,强制它使用unity来解析IValueResolvers cfg.ConstructServicesUsing(type => container.Resolve(type)); 我很难弄清楚如何用Uni

我有一些代码,可以动态扫描程序集以实现AutoMapper Profile类,并构建一个映射器以与Unity DI一起使用。最近,我发现需要使用IValueResolver接口。该类需要在其中注入资源依赖项,以便在模型之间执行转换

我可以在automapper配置中添加一行代码,强制它使用unity来解析IValueResolvers

    cfg.ConstructServicesUsing(type => container.Resolve(type));
我很难弄清楚如何用Unity动态注册通用接口的多个实例

我可以用硬编码

    container.RegisterType<IValueResolver<Class1, Class2, string>,Class1ToClass2ValueResolver>();
container.RegisterType();

但是,我宁愿动态扫描并注册它们。

可能有更好的方法,这种方法可能有点幼稚,可能无法处理所有嵌套类型层次结构或同一泛型接口的多个实现,但这种方法确实适用于发布的案例。也许可以使用一些注册基于惯例的特征

var resolverTypes = .AppDomain.CurrentDomain.GetAssemblies()
    .Where(a => !a.IsDynamic) // Exclude dynamic assemblies
    .Select(a => a.GetTypes())
    .SelectMany(t => t) // flatten Types for each assembly into one
    .Select(t => t.GetTypeInfo())
    .Where(t => 
        t.ImplementedInterfaces.Any(i =>
            i.IsGenericType && 
            i.GetGenericTypeDefinition().IsAssignableFrom(typeof(IValueResolver<,,>))))
    .Select(t => new
    {
        Inteface = t.ImplementedInterfaces.First(i =>
            i.IsGenericType && i.GetGenericTypeDefinition()
                             .IsAssignableFrom(typeof(IValueResolver<,,>))),
        Type = t
    });

var container = new UnityContainer();

foreach (var resolverType in resolverTypes)
{
    container.RegisterType(resolverType.Inteface, resolverType.Type);
    var resolver = container.Resolve(typeof(IValueResolver<Class1, Class2, string>));
    Debug.Assert(resolver.GetType() == typeof(Class1ToClass2ValueResolver));
}

很酷。我想我必须使用MakeGenericType或类似的东西。但是,我看到您通过使用实际的接口定义和类型来进行注册,这使其他人更容易理解。Thx
public class Class1 { }

public class Class2 { }

public interface IValueResolver<U, V, W> { }

public class Class1ToClass2ValueResolver : IValueResolver<Class1, Class2, string> { }