团结 - ResolveAll与条件名称 [英] Unity - ResolveAll by name with condition
本文介绍了团结 - ResolveAll与条件名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道是否有可能通过在他们被注册的名称一些条件来解决统一所有的依赖关系。
I was wondering if it is possible to resolve all dependancies in Unity by some condition on the name that they were registered.
例如:
Resloving注册的所有接口所在的名字注册了ProcessA启动。
For example: Resloving all interfaces registered where the name registered starts with "ProcessA".
和如果没有办法做到这一点,那么也许我可以如何扩展统一,让这一点。
And if there is no way to do this then perhaps how can i extend Unity to allow this.
推荐答案
您应该能够使用注册
来做到这一点,我会建议扩展方法而不是扩展统一直接:
You should be able to use Registrations
to do this and I would recommend an extension method rather than extending Unity directly:
var matches = c.Resolve<IMyService>(name => name.StartsWith("ProcessA"));
使用这个扩展方法:
Using this extension method:
public static class MyUnityExtensions
{
public static IEnumerable<T> Resolve<T>(this IUnityContainer c, Func<string, bool> match)
{
var matches = c.Registrations.Where(r => match(r.Name));
foreach (var registration in matches)
{
yield return c.Resolve<T>(registration.Name);
}
}
}
这篇关于团结 - ResolveAll与条件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文