Unity.BuildUp无法消除歧义

时间:2010-11-03 15:59:16

标签: unity-container

我有一个带有两个构造函数的类,两个构造函数都有一个参数。由于限制不值得解释,我不能改变构造函数或使用后代类。

我不能使用unity来创建这个类的实例,因为Unity看到2个构造函数具有相同数量的参数并且抱怨它不知道使用哪个,这是公平的。因此,我自己创建实例,然后尝试使用UnityContainer.BuildUp()

var result = constructorInfo.Invoke(new object[] { content });
UnitContainer.BuildUp(result);

上面的代码没有设置我的[Dependency]属性,也没有调用[InjectionMethod],如果我改用它。

var result = constructorInfo.Invoke(new object[] { content });
UnitContainer.BuildUp(typeOfObject, result);

这引发了关于模糊构造函数的另一个异常,即使我没有要求它构造实例。

有没有人有任何想法?

以下是一个示例应用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.Reflection;

namespace ConsoleApplication7
{
    public interface IConstructorType1 { }
    public interface IConstructorType2 { }
    public interface INeedThisDependency { }
    public class NeedThisDependency : INeedThisDependency { }

    public class MyDomainObject
    {
        public MyDomainObject(IConstructorType1 constructorType1) { }
        public MyDomainObject(IConstructorType2 constructorType2) { }

        [Dependency]
        public INeedThisDependency Needed { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer unityContainer = new UnityContainer();
            unityContainer.RegisterType<INeedThisDependency, NeedThisDependency>();
            //Try with type 1 constructor
            ConstructorInfo constructorInfo1 = typeof(MyDomainObject).GetConstructor(new Type[] { typeof(IConstructorType1) });
            MyDomainObject instance1 = CreateTheInstance(unityContainer, typeof(MyDomainObject), constructorInfo1, null);
            //Try with type 2 constructor
            ConstructorInfo constructorInfo2 = typeof(MyDomainObject).GetConstructor(new Type[] { typeof(IConstructorType2) });
            MyDomainObject instance2 = CreateTheInstance(unityContainer, typeof(MyDomainObject), constructorInfo2, null);
        }

        //This is the only point I have any influence over what happens
        //So this is the only place I get to change the code.
        static MyDomainObject CreateTheInstance(IUnityContainer unityContainer, Type type, ConstructorInfo constructorInfo, object parameters)
        {
            var result = (MyDomainObject)constructorInfo.Invoke(new object[] { parameters });
            //This will throw an ambiguous constructor exception,
            //even though I am not asking it to construct anything
            unityContainer.BuildUp(type, result);
            //This will not build up dependencies
            unityContainer.BuildUp(result); 
            if (result.Needed == null)
                throw new NullReferenceException("Needed");
            return result;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

遗憾的是,这是BuildUp中的一个错误。

答案 1 :(得分:0)

而不是调用BuildUp来调用这个CallInjectionMethod帮助器。

public static class UnityContainerHelper
{
    public static void CallInjectionMethod(this IUnityContainer unityContainer, object instance, params ResolverOverride[] overrides)
    {
        if (instance == null)
            throw new ArgumentNullException("Instance");

        var injectionMethodInfo = instance.GetType().GetMethods().Where(x => x.GetCustomAttributes(typeof(InjectionMethodAttribute), true).Any()).SingleOrDefault();
        if (injectionMethodInfo == null)
            return;
        var parameters = injectionMethodInfo.GetParameters();
        if (parameters.Length == 0)
            return;

        var dependencies = new object[parameters.Length];
        int index = 0;
        foreach (Type parameterType in parameters.Select(x => x.ParameterType))
        {
            dependencies[index] = unityContainer.Resolve(parameterType, overrides);
            index++;
        }
        injectionMethodInfo.Invoke(instance, dependencies);
    }
}