Resolving classes without registering them using Castle Windsor

后端 未结 3 785
温柔的废话
温柔的废话 2020-12-01 16:13

Take the following useless program:

class Program
{
    static void Main(string[] args)
    {
        IUnityContainer unityContainer = new UnityContainer();
         


        
3条回答
  •  隐瞒了意图╮
    2020-12-01 16:47

    Krzysztof don't be afraid to link to your own blog here :) http://devlicious.com/blogs/krzysztof_kozmic/archive/2009/11/16/castle-windsor-lazy-loading.aspx

    Also, I found this simple implementation useful in my WPF app, remove the string contraint and you are close to the general case

    public class ViewModelLoader : Castle.MicroKernel.Resolvers.ILazyComponentLoader {
        public IRegistration Load(string key, Type service)
        {
            if (service == null)
                return null;
            if (service.Name.EndsWith("ViewModel", StringComparison.CurrentCultureIgnoreCase))
                return Component.For(service).Named(key);
            else
                return null;
        }
    }
    

提交回复
热议问题