为什么这个泛型代码不会编译?

时间:2009-10-18 09:44:26

标签: c# generics unity-container

//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

2 个答案:

答案 0 :(得分: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 <did_bfg@.(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

答案 1 :(得分: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());
        }