简体   繁体   English

Unity运行时解析?

[英]Unity run-time resolving?

I have the following code in a console program. 我在控制台程序中有以下代码。

interface I { ...; string X { get; }; string Y {get; }; string Z {get; } ...}
class A : I {...}
class B : I {...}
class C : I {...}

The program accept command line parameters like test.exe b -x 10 -z 20 . 该程序接受命令行参数,例如test.exe b -x 10 -z 20 And it will create an instant of B and set X to 10, Z to 20. 它将创建B的瞬间并将X设置为10,Z设置为20。

How to implement this using unity? 如何统一使用? This may be a newbie question. 这可能是一个新手问题。

You need to register named mapping against same interface and resolve using the name passed as argument. 您需要针对同一接口注册命名映射,并使用作为参数传递的名称进行解析。

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]);

Read Registering Type Mappings with the Container for explanation 阅读在容器中注册类型映射以获取解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM