【问题标题】:The current type is an interface and cannot be constructed. Are you missing a type mapping?当前类型是接口,无法构造。您是否缺少类型映射?
【发布时间】:2012-04-30 14:05:46
【问题描述】:

我有一个控制器类负责双击命令,然后调用一个向用户弹出窗口的方法。类似的东西:

var popup = container.GetService<PopupCommand>();

在上面的行中它抛出一个错误说: 当前类型 PopupCommand.IPopupDataHandler 是一个接口,无法构造。您是否缺少类型映射?

我更新了包含 container.GetService() 方法的 DLL,在此之前它可以正常工作。

我尝试在谷歌搜索,但类似的问题更多与 Unity 相关,我怀疑我的问题是否与 Unity 相关。

【问题讨论】:

  • 阅读editing help
  • container的类型是什么?
  • 另外,PopupCommand 在其构造函数中接受哪些参数。异常是否包含更多信息?
  • 我没有使用 Unity,但我看到其他 IoC 容器存在类似问题,问题实际上在于解析属性或构造函数参数之一的类型。你改变了其中的任何一个吗?远射。
  • Hey Rob,“问题实际上在于解析属性或构造函数参数之一的类型”,您的意思是我使用的属性或者与容器相关的东西......?

标签: c# wpf types mapping


【解决方案1】:

基本上编译器会告诉你你正在尝试实例化一个接口。

container.GetService&lt;PopupCommand&gt;() 可能会带回一个名为PopupCommand.IPopupDataHandler 的接口,您可能需要将其强制转换为您需要的类型或将类型更改为对象,您还应该检查方法的约束 - 它可能会丢失new 约束。

【讨论】:

    【解决方案2】:

    尝试使用 Addin DefaultController Factory 来注册您的控制器。 三步:第一步 1.在你的项目中添加一个类DefaultControllerFactory

    public class ControllerFactory :DefaultControllerFactory
        {
            protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
            {
                try
                {
                    if (controllerType == null)
                        throw new ArgumentNullException("controllerType");
    
                    if (!typeof(IController).IsAssignableFrom(controllerType))
                        throw new ArgumentException(string.Format(
                            "Type requested is not a controller: {0}",
                            controllerType.Name),
                            "controllerType");
    
                    return MvcUnityContainer.Container.Resolve(controllerType) as IController;
                }
                catch
                {
                    return null;
                }
    
            }
            public static class MvcUnityContainer
            {
                public static UnityContainer Container { get; set; }
            }
        }
    

    第二步:在BuildUnityContainer方法的Bootstrap类中注册

    private static IUnityContainer BuildUnityContainer()
        {
          var container = new UnityContainer();
    
          // register all your components with the container here
          // it is NOT necessary to register your controllers
    
          // e.g. container.RegisterType<ITestService, TestService>();    
          //RegisterTypes(container);
          container = new UnityContainer();
          container.RegisterType<IProductRepository, ProductRepository>();
    
    
          UnityInterceptionExample.Models.ControllerFactory.MvcUnityContainer.Container = container;
          return container;
        }
    

    第 3 步:并将其注册到 Global.asax 文件中

    protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
    
                WebApiConfig.Register(GlobalConfiguration.Configuration);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
                AuthConfig.RegisterAuth();
                Bootstrapper.Initialise();
                ControllerBuilder.Current.SetControllerFactory(typeof(ControllerFactory));
            } 
    

    完成了。 可能这对你有用...... 快乐编码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多