2012-04-19 26 views
8

私は、ダブルクリックコマンドを気にして、ウィンドウにユーザをポップアップするメソッドを呼び出すコントローラクラスを持っています。何かのように:それはというエラーがスローされます上記の行で現在のタイプはインタフェースであり、構築できません。型マッピングがありませんか?

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

: 現在のタイプ、PopupCommand.IPopupDataHandler、インタフェースであり、構築することができません。型マッピングがありませんか?

container.GetService()のメソッドを含むDLLを更新してから正常に動作していました。

私はGoogleで検索しましたが、類似の問題はUnityに関連しています。私の問題がどこにUnityに関連するのか疑問に思っています。

+0

[編集ヘルプ](http://stackoverflow.com/editing-help#code)をお読みください。 –

+1

'コンテナ'のタイプは? –

+0

また、 'PopupCommand'はそのコンストラクタでどのような引数をとりますか?例外にはさらに情報が含まれていますか? –

答えて

1

基本的に、コンパイラはインターフェイスをインスタンス化しようとしていることを伝えます。

container.GetService<PopupCommand>()は、おそらくあなたはおそらく、あなたもこの方法の制約をチェックする必要がありますあなたが必要な型にキャストまたはオブジェクトにタイプを変更する必要があり、PopupCommand.IPopupDataHandlerという名前のインターフェイスを、あなたを思い出させる - それはnew制約が失われる可能性が。

0

コントローラを登録するためのAddin DefaultController Factoryを試してください。 スリーステップ:ステップ1 1.Addプロジェクト

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; } 
     } 
    } 

ステップ2のクラスDefaultControllerFactory:ステップ3

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; 
    } 

BuildUnityContainer方法でブートストラップクラスでそれを登録しますし、グローバルに登録.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)); 
     } 

そしてfinisihed。 これはあなたのために働くかもしれません... ハッピーコーディング。

関連する問題