How to get the type of the object that is being injected in Unity?

眉间皱痕 提交于 2019-12-11 00:34:46

问题


I have a type that receives another type in its constructor, which usually is the type of the object that creates it, e.g.:

public class Logger {
    public Logger(Type parent) { ... }
}

I would like to instruct Unity to resolve Logger passing as the argument to its constructor the type of the object that requires it. Something like:

// ... would be some directive to tell Unity to use the type that
/// depends on Logger
container.RegisterType<Logger>(new InjectionConstructor(...));

So that when I try to resolve MyService:

public MyService {
    public MyService(Logger logger) { ... }
}

it will return:

var logger = new Logger(typeof(MyService));
return new MyService(logger);

Is it possible? Is there another way of doing it?


回答1:


Actually, you can do this:

internal class Program
{
    static void Main( string[] args )
    {
        var container = new UnityContainer();
        container.RegisterType<IInterface, Implementation>( new MyInjectionConstructor() );

        // this instance will get a logger constructed with loggedType == typeof( Implementation )
        var instance = container.Resolve<IInterface>();
    }
}

internal class MyInjectionConstructor : InjectionMember
{
    public override void AddPolicies( Type serviceType, Type implementationType, string name, IPolicyList policies )
    {
        policies.Set<IConstructorSelectorPolicy>( new MyConstructorSelectorPolicy(), new NamedTypeBuildKey( implementationType, name ) );
    }
}

internal class MyConstructorSelectorPolicy : DefaultUnityConstructorSelectorPolicy
{
    protected override IDependencyResolverPolicy CreateResolver( ParameterInfo parameter )
    {
        if( parameter.ParameterType == typeof( ILogger ) )
        {
            return new LiteralValueDependencyResolverPolicy( new Logger( parameter.Member.DeclaringType ) );
        }
        return base.CreateResolver( parameter );
    }
}

internal interface ILogger
{
}

internal class Logger : ILogger
{
    public Logger( Type loggedType )
    {
    }
}

internal interface IInterface
{
}

internal class Implementation : IInterface
{
    public Implementation( ILogger logger )
    {
    }
}

This is proof of concept code only, and might need to be refined a bit before production use...



来源:https://stackoverflow.com/questions/43056082/how-to-get-the-type-of-the-object-that-is-being-injected-in-unity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!