possible GetObjectsOfType replacement

前端 未结 4 600
-上瘾入骨i
-上瘾入骨i 2020-12-12 03:08

I have this small piece of code

var idObjects = Spring.Context.Support.ContextRegistry.GetContext()
                      .GetObjectsOfType(typeof (ICustomIn         


        
4条回答
  •  死守一世寂寞
    2020-12-12 03:50

    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

    
        
            
                
                
                
                
            
        
    
    
    
        
    
    

    and finally inject to the required target

    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)

提交回复
热议问题