How to inject a Type instead of an instance into a constructor using Unity

Viewed 753

Is it possible to inject a type into a constructor instead of an instance? If so how is this achieved? I want to avoid explicitly registering a factory method or resolving an instance in situ if possible.

public interface IJob { }
public class TheJob : IJob { }

public interface IService
{
    string GetTypeDesc();
}

public class Service : IService
{
    private Type _jobType;

    public Service(Type jobType)
    {
        _jobType = jobType;
    }

    public string GetTypeDesc()
    {
        return _jobType.ToString();
    }
}

It seems even when registering the type with explicit constructor definition, Unity wants to inject an instance into the type place holder.

Type jobType = typeof(TheJob);    
(new UnityContainer()).RegisterType<IService, Service>(new InjectionConstructor(jobType));
2 Answers
Related