2009-10-18 78 views
0
//The class is defined like so.... 
public class CreateNewAccountHandler : ICommandHandler<CreateNewAccountCommand, CreateNewAccountResponse> 
{ 
     public CreateNewAccountResponse ExecuteCommand(CreateNewAccountCommand command) 
     { 
       throw new NotImplementedException(); 
     } 
} 

//And here it the code which won't compile 
static void RegisterHandlers_Account(IUnityContainer unityContainer) 
{ 
     unityContainer.RegisterType 
       < 
         ICommandHandler 
           < 
             TaskSmart.AppLayer.Api.Commands.Account.CreateNewAccountCommand, 
             TaskSmart.AppLayer.Api.Commands.Account.CreateNewAccountResponse 
           >, 
         TaskSmart.AppLayer.RequestHandlers.Account.CreateNewAccountHandler 
       >(new TransientLifetimeManager()); 
} 

錯誤1型 「TaskSmart.AppLayer.RequestHandlers.Account.CreateNewAccountHandler」 不能被用作類型參數「TTO」 在通用類型或方法 「Microsoft.Practices.Unity.IUnityContainer。 RegisterType(Microsoft.Practices.Unity.LifetimeManager, params Microsoft.Practices.Unity.InjectionMember [])'。 有從 沒有隱式引用 轉化「TaskSmart.AppLayer.RequestHandlers.Account.CreateNewAccountHandler」 到 「TaskSmart.AppLayer.Api.RequestHandlers.ICommandHandler」。 C:\ DATA \ TaskSmart \ TaskSmart.AppLayer \ UnityBootStrapper.cs 50 6 TaskSmart.AppLayer爲什麼不能編譯此泛型代碼?

我都檢查過很多次,但我只是不明白爲什麼這個拒絕編譯!我甚至完全限定了類/接口名稱,以確保它不是名稱空間問題,並且我得到相同的錯誤。

任何想法?

PS:SVN是在這裏:https://tasksmart.svn.sourceforge.net/svnroot/tasksmart/trunk

+0

你確定你沒有命名空間問題嗎?類似於具有相同名稱但名稱空間不同的類? – 2009-10-18 09:49:24

+0

更新了提到的問題,我試圖完全限定命名空間而沒有成功。 給SVN一個嘗試,它不需要安裝 - 這是完全奇怪的,我只是看不到問題! – 2009-10-18 09:52:14

回答

2

你已經在你的代碼庫有兩個ICommandHandler(在TaskSmart.AppLayer.Api.RequestHandlers和TaskSmart.AppLayer.RequestHandlers),以及您的通用註冊的第一部分沒有使用完全合格的名稱

正確的代碼是

static void RegisterHandlers_Account(IUnityContainer unityContainer) 
     { 
      unityContainer.RegisterType 
       < 
        TaskSmart.AppLayer.RequestHandlers.ICommandHandler 
         < 
          TaskSmart.AppLayer.Api.Commands.Account.CreateNewAccountCommand, 
          TaskSmart.AppLayer.Api.Commands.Account.CreateNewAccountResponse 
         >, 
        TaskSmart.AppLayer.RequestHandlers.Account.CreateNewAccountHandler 
       >(new TransientLifetimeManager()); 
     } 
+0

我之前曾將AppLayer分成兩個項目,並沒有意識到我已經「複製」了文件而不是「移動」文件。 非常感謝! – 2009-10-18 10:20:59

+0

不客氣; o) – 2009-10-18 10:24:01

+0

它一直讓我發瘋。我並不認爲這是因爲我不那麼愚蠢;-) – 2009-10-18 10:25:05

2

嘗試完全限定ICommandHandler接口:

unityContainer.RegisterType<TaskSmart.AppLayer.RequestHandlers.ICommandHandler 
    <CreateNewAccountCommand, CreateNewAccountResponse>, 
    CreateNewAccountHandler>(); 

必須由CreateNewAccountHandler執行完全相同的接口:

public class CreateNewAccountHandler : 
    TaskSmart.AppLayer.RequestHandlers.ICommandHandler<CreateNewAccountCommand, CreateNewAccountResponse> 
{ } 

而這裏的補丁文件應用於您的SVN回購:

From f5541188298b40515728c1ad51f645408876999c Mon Sep 17 00:00:00 2001 
From: unknown <[email protected](none)> 
Date: Sun, 18 Oct 2009 12:14:26 +0200 
Subject: [PATCH] fixed namespace 

--- 
TaskSmart.AppLayer/UnityBootStrapper.cs | 2 +- 
1 files changed, 1 insertions(+), 1 deletions(-) 

diff --git a/TaskSmart.AppLayer/UnityBootStrapper.cs b/TaskSmart.AppLayer/UnityBootStrapper.cs 
index c3ed0fe..d9748a9 100644 
--- a/TaskSmart.AppLayer/UnityBootStrapper.cs 
+++ b/TaskSmart.AppLayer/UnityBootStrapper.cs 
@@ -41,7 +41,7 @@ namespace TaskSmart.AppLayer 
       { 
         unityContainer.RegisterType 
           < 
-          ICommandHandler 
+     TaskSmart.AppLayer.RequestHandlers.ICommandHandler 
               < 
                 TaskSmart.AppLayer.Api.Commands.Account.CreateNewAccountCommand, 
                 TaskSmart.AppLayer.Api.Commands.Account.CreateNewAccountResponse 
-- 
1.6.4.msysgit.0