BuildUp ignores Dependency attributes

Mar 10, 2009 at 5:15 PM
Hi there,

Today our team got bit by what we thought was a strange implementation in the BuildUp(object existing) method.  It looks like it's by design, but when you call BuildUp it only inspects properties on the given variable's declared type as opposed to the object type.  As a result, if you create an object and hold a reference to it as one of it's implementing interfaces, when you pass that reference to BuildUp, none of the Dependency attributes on the class will be found and injected.


public interface IFoo {}
public interface IBar {}

public class Foo : IFoo
   public IBar Bar { get; set; }

This works:

Foo foo = new Foo();

This doesn't:

IFoo foo = new Foo();

I'd be interested to know why that design decision was made.  I am aware of the overload where you can do this:  container.BuildUp(foo.GetType(), foo), but it seems like the intuitive default would be that container.BuildUp(foo) would "just work" (TM) work.

Mar 10, 2009 at 6:25 PM
When you call BuildUp (obj), you are actually calling the generic version BuildUp<T>(T obj). The result is as you saw - the declared type of the object is used instead of the runtime type.

This was reported as a bug a while back. We're not quite sure how much breakage would result if we changing things now, so we're loath to change it.


Mar 10, 2009 at 7:52 PM
I'd be curious to see where folks are using it in situations where it would break things (putting dependency properties on an interface just feels so wrong to me :-).  

Anyways, thanks for the explanation.
Jul 22, 2010 at 4:55 AM

I'm using the following workaround

public static T BuildUpRuntime<T>(this IUnityContainer container, T existing, params ResolverOverride[] resolverOverrides)
    return (T)container.BuildUp(existing.GetType(), existing, resolverOverrides);

What do you think about it?