how to reslove a class with a constructor take primitive type argument

Oct 3, 2009 at 5:07 AM

for example:

Class Man{

  public Man(String name) {}

}

can i pass the container a string instance. so the container will use this string to reslove the class at runtime ?

thank you in advance for you help

Oct 3, 2009 at 5:46 AM

In Unity 1.2, you can specify the value in container configuration:

container.RegisterType<Man>( new InjectionConstructor("Fred") );

var man = container.Resolve<Man>();

Assert.AreEqual("Fred", man.Name);

 

In Unity 2.0 we've added the ability to pass values in the resolve call itself:

container.Resolve<Man>(new ParameterOverride("name", "Fred"));

That's currently available in Codeplex source control.

 

 

Oct 3, 2009 at 7:22 PM

cool! thank you ctavares

that Help me a lot, otherwise i have to read through the source.

Oct 5, 2009 at 5:13 AM

Umm, Unity includes fairly comprehensive documentation, you shouldn't have to go to the source for basic capabilities like this. If the docs aren't sufficient to explain this, please let us know so we can fix them.

 

-Chris

 

Oct 5, 2009 at 2:31 PM

I get one more question. how to do this. thank you in advance.

If i can use the one parameter constructor to initiate some children and one parent( no recursive), then use the three parameter constructor to create a Man instance.

Class Man{

  public Man(String name){}

  public Man(String name, List<Man> children, Man parent) {}

}