统一运行时解析?
问题描述:
我在控制台程序中有以下代码。统一运行时解析?
interface I { ...; string X { get; }; string Y {get; }; string Z {get; } ...}
class A : I {...}
class B : I {...}
class C : I {...}
程序接受命令行参数,如test.exe b -x 10 -z 20
。它会创建一个B的瞬间,并将X设置为10,Z为20.
如何使用统一来实现此操作?这可能是一个新手问题。
答
您需要在相同接口上注册命名映射,并使用作为参数传递的名称进行解析。
var container = new UnityContainer();
container.RegisterType<I, A>("a");
container.RegisterType<I, B>("b");
container.RegisterType<I, C>("c");
I instance = container.Resolve<I>(args[0]);
......然后继续前进和X,Y,Z的属性上的实例设置为其他ARGS ... – Carsten