System.Type 参数上的 ParameterOverride 失败

发布于 2024-10-31 06:38:11 字数 2659 浏览 2 评论 0原文

我尝试在 Unity 2.0 中使用 ParameterOverride。只要我提供的参数不是 System.Type,它就可以正常工作。

在下面的测试程序中,当泛型参数 T 是 int 时,我将构造函数注入到 MyClass 中,并且当泛型参数 T 是 int 时,我将使用整数参数;当 T 是 System.Type 时,我将使用 Type 参数注入构造函数。在接下来的输出中,您会看到它很好地处理了整数,但似乎认为 typeof(MyClassForParam) 是一个实际的 MyClassForParam 对象,而不是类型。有什么想法吗?

namespace UnityParameterOverride
{
    interface IMyClass
    {
    }

    public class MyClass<T> : IMyClass
    {
        public MyClass(T myParam)
        {
            Console.WriteLine("{0}", myParam);
        }
    }

    public class MyClassForParam
    {
        public MyClassForParam() { }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer unity = new UnityContainer();
            try
            {
                // Works fine: prints 12345
                Console.WriteLine("Injecting an integer parameter.");
                unity.RegisterType<IMyClass, MyClass<int>>();
                unity.Resolve<IMyClass>(new ParameterOverride("myParam", 12345));

                // Fails: Seems to think that the parameter is an actual MyClassForParam instead of the type of that class.
                Console.WriteLine();
                Console.WriteLine("Injecting a Type parameter.");
                unity.RegisterType<IMyClass, MyClass<Type>>();
                unity.Resolve<IMyClass>(new ParameterOverride("myParam", typeof(MyClassForParam)));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}

输出如下:

Injecting an integer parameter.
12345

Injecting a Type parameter. ----
Resolution of the dependency failed, type = "UnityParameterOverride.IMyClass", name = "(none)".

异常发生时: 发生

Exception occurred while: Resolving parameter "myParam" of constructor UnityParameterOverride.MyClass`1[[System.Type, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Type myParam).
Exception is: InvalidCastException - Unable to cast object of type 'UnityParameterOverride.MyClassForParam' to type 'System.Type'.

异常时,容器为:

Resolving UnityParameterOverride.MyClass1[System.Type],(none) (mapped from Un ityParameterOverride.IMyClass, (none)) Resolving parameter "myParam" of constructor UnityParameterOverride.MyClass1[ [System.Type, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5 61934e089]](System.Type myParam)

I'm attempting to use a ParameterOverride with Unity 2.0. It works fine as long as the parameter I'm supplying is not a System.Type.

In the following test program, I am injecting the constructor to MyClass<T> with an integer parameter when the generic parameter T is an int, and with a Type parameter when T is System.Type. In the output that follows, you'll see that it handles the integer just fine, but seems to think that typeof(MyClassForParam) is an actual MyClassForParam object, not the type. Any ideas?

namespace UnityParameterOverride
{
    interface IMyClass
    {
    }

    public class MyClass<T> : IMyClass
    {
        public MyClass(T myParam)
        {
            Console.WriteLine("{0}", myParam);
        }
    }

    public class MyClassForParam
    {
        public MyClassForParam() { }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer unity = new UnityContainer();
            try
            {
                // Works fine: prints 12345
                Console.WriteLine("Injecting an integer parameter.");
                unity.RegisterType<IMyClass, MyClass<int>>();
                unity.Resolve<IMyClass>(new ParameterOverride("myParam", 12345));

                // Fails: Seems to think that the parameter is an actual MyClassForParam instead of the type of that class.
                Console.WriteLine();
                Console.WriteLine("Injecting a Type parameter.");
                unity.RegisterType<IMyClass, MyClass<Type>>();
                unity.Resolve<IMyClass>(new ParameterOverride("myParam", typeof(MyClassForParam)));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}

Output follows:

Injecting an integer parameter.
12345

Injecting a Type parameter. ----
Resolution of the dependency failed, type = "UnityParameterOverride.IMyClass", name = "(none)".

Exception occurred while:

Exception occurred while: Resolving parameter "myParam" of constructor UnityParameterOverride.MyClass`1[[System.Type, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Type myParam).
Exception is: InvalidCastException - Unable to cast object of type 'UnityParameterOverride.MyClassForParam' to type 'System.Type'.

At the time of the exception, the container was:

Resolving UnityParameterOverride.MyClass1[System.Type],(none) (mapped from Un ityParameterOverride.IMyClass, (none)) Resolving parameter "myParam" of constructor UnityParameterOverride.MyClass1[ [System.Type, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5 61934e089]](System.Type myParam)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧时浪漫 2024-11-07 06:38:11

Unity 对类型参数定义进行特殊处理。有关更多详细信息,请参阅 Krzysztof Kozmic 的这篇文章: http://kozmic.pl/2008/12/03/unity-framework-and-the-principle-of-the-least-surprise/

Unity gives special treatment to Type parameter definitions. See this post by Krzysztof Kozmic for more details: http://kozmic.pl/2008/12/03/unity-framework-and-the-principle-of-the-least-surprise/

玻璃人 2024-11-07 06:38:11

在 Unity 中进行一些探索后,我遇到了同样的问题,我发现您可以通过用 InjectionParameter 包装裸类型参数来覆盖默认类型解析行为:

new ParameterOverride("typeParameterArgument",new InjectionParameter(typeof(Type),typeof(MyNamespace.MyClassUsedAsInjectedTypeArgument)))

ParameterOverride 的构造函数使用 InjectionParameterValue.ToParameter() ,它将尊重任何预定义的子类注射参数值。当使用在构造期间使用相同方法的 PropertyOverride 时,此方法也应该有效。

I ran into the same issue after some spelunking in Unity I found that you can override the default type resolution behavior by wrapping the naked type argument with an InjectionParameter:

new ParameterOverride("typeParameterArgument",new InjectionParameter(typeof(Type),typeof(MyNamespace.MyClassUsedAsInjectedTypeArgument)))

The constructor for ParameterOverride uses InjectionParameterValue.ToParameter() which will respect any predefined sub-class of InjectionParameterValue. This method should also work when using a PropertyOverride which uses the same method during construction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文