1

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. 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.

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

... and then move on and set the X,Y,Z properties on instance to the other args...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.