Base InjectionProperties applied to derived classes

Mar 25, 2009 at 8:53 AM
Is there way to get base InjectionProperties applied to derived classes without registering the derived classes?

This is what I mean in code:

    public class Program
    {
        public class IDependency
        {
        }

        public class Dependency : IDependency
        {
        }

        public class Base
        {
            public IDependency Dependency { get; set; }
        }

        public class Derived : Base
        {
        }

        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();
            container.RegisterType<IDependency, Dependency>();
            container.RegisterType<Base>(new InjectionProperty("Dependency"));

            //Works.
            var @base = container.Resolve<Base>();
            Debug.Assert(@base.Dependency is Dependency);

            //Doesn't work.
            var derived = container.Resolve<Derived>();
            Debug.Assert(derived.Dependency is Dependency);

        }
    }
Mar 25, 2009 at 11:00 AM

I don’t think it’s possible, you may want to decorate  the Dependency property of your Base class with a [Dependecy] attribute instead. This is assuming that attributes are OK with you.

Example.
public class Base

        {

            [Dependency]

            public IDependency Dependency { get; set; }

        }

Mar 25, 2009 at 12:58 PM
That works but unfortunately base class is in a standard library and I can't change it.