Unity IoC for resolving assemblies dynamically

前端 未结 2 734
灰色年华
灰色年华 2020-12-30 16:55

We are using unity as IoC. We came across unique problem. We have created interface called IPlugin. This interface is shared across various third party vendors to develop th

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 17:46

        var assemblies = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "Worker*.dll").Select(f => Assembly.LoadFile(f)).ToArray();
    
        (from asm in assemblies
            from t in asm.GetExportedTypes()
            where typeof(ICommonWorker).IsAssignableFrom(t) && t.IsClass
            select t).ForEach(x =>
        {
            unityContainer.RegisterType(typeof(ICommonWorker), x, x.FullName, new ContainerControlledLifetimeManager());
        });
    

    If anyone still cares, this is what I did to load DLL's dynamically which implemented a specific interface (ICommonWorker).

提交回复
热议问题