IUnityContainer.Resolve <T>抛出错误声称它不能与类型参数一起使用

Bud*_*dda 74 .net c# generics compilation unity-container

昨天我已经实现了代码:

CustomerProductManager productsManager = container.Resolve<CustomerProductManager>();
Run Code Online (Sandbox Code Playgroud)

它是可编辑和工作的.

今天(可能是我修改了一些东西)我经常收到错误:

非泛型方法'Microsoft.Practices.Unity.IUnityContainer.Resolve(System.Type,string,params Microsoft.Practices.Unity.ResolverOverride [])'不能与类型参数一起使用

我的同事有相同的源代码,没有相同的错误.为什么?如何解决问题?

PS

行"使用Microsoft.Practices.Unity;" 存在于使用部分.

我试图用非泛型版替换泛型版:

CustomerProductManager productsManager = (CustomerProductManager)container.Resolve(typeof(CustomerProductManager));
Run Code Online (Sandbox Code Playgroud)

并得到另一个错误:

方法'Resolve'没有重载需要'1'参数

It seems like one of the assemblies is not referenced.. but which one? I have 2 of them referenced: 1. Microsoft.Practices.Unity.dll 2. Microsoft.Practices.ServiceLocation.dll

P.P.S. I've saw similar problem http://unity.codeplex.com/WorkItem/View.aspx?WorkItemId=8205 but it is resolved as "not a bug"

Any thought will be helpful

小智 173

我有同样的问题,发现"修复"看Prism示例代码文件.看起来,即使它不是Unity V2中的DLL,您也必须在类中添加一个引用: Microsoft.Practices.Unity

我完整的"使用"部分如下

using System;
using System.Windows;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.UnityExtensions;
Run Code Online (Sandbox Code Playgroud)

我不知道如果你正在使用Silverlight,但Container.Resolve通用版 IS在Microsoft.Practices.Unity.

  • 而FWIW,在新的和高度兼容的Unity 5中,命名空间现在是`Unity`而不是`Microsoft.Practices.Unity`. (10认同)
  • 究竟.前段时间我发现添加'using Microsoft.Practices.Unity;' 解决了一个问题. (9认同)
  • 原因是Resolve with type参数是Microsoft.Practices.Unity中的扩展方法,而没有type参数的Resolve()只是接口(或类)上的方法. (5认同)

Ran*_*s1r 33

微软不再拥有Unity,它在版本5中,命名空间现在是:

using Unity;
Run Code Online (Sandbox Code Playgroud)

使用时确保在您的使用部分:

container.Resolve<T>();
Run Code Online (Sandbox Code Playgroud)