首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#重载泛型函数

C#重载泛型函数
EN

Stack Overflow用户
提问于 2014-10-02 17:29:02
回答 1查看 125关注 0票数 0

我正在尝试用一个特定的方法重载一个泛型方法。但他不会使用指定类型的方法,他采用的是不会实现的泛型方法。

代码语言:javascript
复制
public static class Functions<TE, TC, TG>
     where TE : Entity, IExternalConexioEntity
     where TC : class, IInternalConexioEntity, IObjectState
     where TG : ConexioEntity
{

public static TG ConvertToEntity(TC conexioEntity)
{
    throw new Exception("Type not supported(" + conexioEntity.GetType() + ")");
}

public static Contact ConvertToEntity(ConexioContact synccontact)
{

}
}

方法调用:

代码语言:javascript
复制
result.Add(ConvertEntity(synchronizationContact));

而且syncrhonizationContact也是一个泛型类型where TC : class, IInternalConexioEntity, IObjectState。所以实际的类型是ConexioContact。

但是他没有运行到第二个方法,我的值是ConexioContact类型的。

代码语言:javascript
复制
Conexio.Data.Entities.Conexio.ConexioContact
System.AggregateException: One or more errors occurred. ---> System.Exception: Type not supported(Conexio.Data.Entities.Conexio.ConexioContact)
   at Conexio.Core.Orchestration.Functions`3.ConvertToEntity(TC conexioEntity) in 
d:\TeamFoundation\Sources\Conexio\Conexio.Core.Orchestration\Functions.cs:line 187
   at Conexio.Core.Orchestration.Contacts.Doubtfuls.SearchDoubtfuls`3.ConvertEntity(TC entity) in d:\TeamFoundation\Sources\Conexio\Conexio.Core.Orchestration\Contacts\Doubtfuls\SearchDoubtfuls.cs:line 107
   at Conexio.Core.Orchestration.Contacts.Doubtfuls.SearchDoubtfuls`3.<SearchAsync>d__5.MoveNext() in d:\TeamFoundation\Sources\Conexio\Conexio.Core.Orchestration\Contacts\Doubtfuls\SearchDoubtfuls.cs:line 61
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Conexio.Core.Orchestration.Contacts.ConexioEntityBL`3.<SearchDoubtfulsAsync>d__28.MoveNext() in d:\TeamFoundation\Sources\Conexio\Conexio.Core.Orchestration\Contacts\ConexioEntityBL.cs:line 323
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at Conexio.Core.Orchestration.Test.Doubtfuls.SearchContactsTest.TestSearch4() in d:\TeamFoundation\Sources\Conexio\Conexio.Core.Orchestration.Matching.Test\Doubtfuls\SearchContactsTest.cs:line 90
---> (Inner Exception #0) System.Exception: Type not supported(Conexio.Data.Entities.Conexio.ConexioContact)
   at Conexio.Core.Orchestration.Functions`3.ConvertToEntity(TC conexioEntity) in d:\TeamFoundation\Sources\Conexio\Conexio.Core.Orchestration\Functions.cs:line 187
   at Conexio.Core.Orchestration.Contacts.Doubtfuls.SearchDoubtfuls`3.ConvertEntity(TC entity) in d:\TeamFoundation\Sources\Conexio\Conexio.Core.Orchestration\Contacts\Doubtfuls\SearchDoubtfuls.cs:line 107
   at Conexio.Core.Orchestration.Contacts.Doubtfuls.SearchDoubtfuls`3.<SearchAsync>d__5.MoveNext() in d:\TeamFoundation\Sources\Conexio\Conexio.Core.Orchestration\Contacts\Doubtfuls\SearchDoubtfuls.cs:line 61
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Conexio.Core.Orchestration.Contacts.ConexioEntityBL`3.<SearchDoubtfulsAsync>d__28.MoveNext() in d:\TeamFoundation\Sources\Conexio\Conexio.Core.Orchestration\Contacts\ConexioEntityBL.cs:line 323<---
EN

回答 1

Stack Overflow用户

发布于 2014-10-02 17:35:38

你要做的不是重载。它被称为泛型专门化,在C#中不受支持。有一些brittle workarounds只有在编译时知道具体类型时才有效。

如果使用ConexioContact-typed参数进行调用,编译器就会知道第二种方法更具体。

如果你传递一个更泛型的参数,例如IConexioContactobject,编译器会发现它没有特定的方法,但它可以使用泛型方法创建一个。所以ConvertToEntity<IConexioContact>胜过了ConvertToEntity(ConexioContact),你得到了一个例外。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26157979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档