Allow interface interception with no target instance


It would be very useful to provide a dynamic implementation of the interface at runtime.

A common scenario it would enable is automatic typed factories. Consider the class and interface below:
public class Foo
    public ISomething Something { get; set; }

    public Foo(int id, string name)
        Id = id;
        Name = name;

    public int Id { get; private set; }
    public string Name { get; private set; }


public interface IFooFactory
    Foo Create(int id, string name);
The implementation for IFooFactory is obvious (just use the container to create instances of Foo by passing the parameters to the constructor and injecting the dependencies), and could easily be provided at runtime. It could easily be done by an interceptor, except that at the moment you must have a target to intercept.

Castle Core already supports this, but I'd rather not have to use Castle Core in addition to Unity...


Enyra wrote Jun 26, 2015 at 2:22 PM

I also had this needs already and was forced to use Castle Core. It would be nice if Unity supports this case.