可能GetObjectsOfType更换 [英] possible GetObjectsOfType replacement

查看:102
本文介绍了可能GetObjectsOfType更换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的小块code的

I have this small piece of code

var idObjects = Spring.Context.Support.ContextRegistry.GetContext()
                      .GetObjectsOfType(typeof (ICustomInterfaceThatDoesSomething));
foreach (ICustomInterfaceThatDoesSomething icitds in idObjects.Values)
      icitds.DoSomething();

有没有一种方法,我可以通过具有spring.net自动注入的单身人士,我宣布一个属性,如 ICustomInterfaceThatDoesSomething

唯一的原因,我想是这样的,因为我要杀死该项目的.dll文件的依赖,这是使用的单点。

The only reason i want something like this is because i want to kill the .dll dependency on the project and this is the single point of usage.

推荐答案

感谢名单,以Marijin的洞察力这已经被钉!

thanx to Marijin's insight this has been nailed!

首先考虑这种多用途的实用工具类

Firstly consider this all-purpose utility class

public class ServiceLocatorImplementer : IMethodReplacer
{
    private readonly Type _forType;
    public ServiceLocatorImplementer(Type forType)
    {
        this._forType = forType;
    }

    protected IEnumerable GetAllImplementers()
    {
        var idObjects = Spring.Context.Support.ContextRegistry.GetContext()
            .GetObjectsOfType(_forType);

        return idObjects.Values;
    }

    public object Implement(object target, MethodInfo method, object[] arguments)
    {
        return GetAllImplementers();
    }
}

此配置示​​例声明它

declare it with this configuration sample

<object id="FindInterfaceUsages" type="ServiceLocatorImplementer, SpringDependendAssembly">
    <constructor-arg name="forType">
        <object type="System.Type" factory-method="GetType">
            <constructor-arg type="string" name="typeName" value="Foo.Bar.IChe, NONSpringDependendAssembly" />
            <!-- i use the strict overload -->
            <constructor-arg type="bool" name="throwOnError" value="true" />
            <constructor-arg type="bool" name="ignoreCase" value="false" />
        </object>
    </constructor-arg>
</object>

<object id="MightyPirate" type="Foo.Pirates, NONSpringDependendAssembly">
    <replaced-method name="GetAllICheImplentations" replacer="FindInterfaceUsages" />
</object>

和最终注入到所需的目标

and finally inject to the required target

这有什么方法真实地伟大,是因为底层GetObjectsOfType是的不可以将被调用,直到你真正执行 GetAllICheImplentations()(除非您尝试它是不会好兆头春天INIT)

What is trully great with this methodology is that the underlying GetObjectsOfType is not going to be called until you actually execute GetAllICheImplentations() (unless you try to execute it during spring init which is not going to bode well)

这篇关于可能GetObjectsOfType更换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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