开发者

Unity run-time resolving?

开发者 https://www.devze.com 2023-04-09 06:43 出处:网络
I have the following code in a console program. interface I { ...; string X { get; }; string Y {get; }; string Z {get; } ...}

I have the following code in a console program.

interface I { ...; string X { get; }; string Y {get; }; string Z {get; } ...}
class A : I {...}
clas开发者_StackOverflows B : I {...}
class C : I {...}

The program accept command line parameters like test.exe b -x 10 -z 20. And it will create an instant of B and set X to 10, Z to 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号