load assembly and use buildup

Sep 24, 2008 at 1:09 PM

 

Hi,

the block of code is an example of loading an assembly into memory create and instance and then apply a buildup on it, the propertis using the attribute [Depenency] aren't call by the container, why ?

Assembly
LoadAssembly;
IBlock Block;
LoadAssembly =
Assembly.LoadFrom(AssemblyPath);
Type AssemblyType = LoadAssembly.GetType("AppBlock", true, true);
Block = (IBlock)Activator.CreateInstance(AssemblyType);

 

 


IMod1
Mod1 = new Module1();
IMod2 Mod2 = new Module2();
ChildContainer =
Container.CreateChildContainer();
ChildContainer.RegisterInstance<
IMod1>(Mod1);
ChildContainer.RegisterInstance<
IMod2>(Mod2);

// doesn't call's the properties setter injection
ModuleBuildUp = ChildContainer.BuildUp(Block, "Block");

 

Sep 24, 2008 at 2:16 PM
Hi,

after a couple of tests I come up with the conclusion that Unity check the attributes of the interface and not the class, since my dependency attributes aren't part of the interface Unity don't call then.

Sep 24, 2008 at 8:23 PM
Basically, the BuildUp method uses the type of the value you pass it to look up the configuration. Since you're asking for an IBlock, it uses IBlock. You could try ChildContainer.BuildUp(Block.GetType(), Block, "Block") instead, but you'll have to cast the resulting value back to IBlock afterwards.