C#重载一个泛型函数
本文关键字:一个 泛型 函数 重载 | 更新日期: 2023-09-27 18:25:28
我正试图用特定方法重载Generic方法。但他不会采用specifiek类型的方法,而是采用不会实现的泛型方法。
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)
{
}
}
方法调用:
result.Add(ConvertEntity(synchronizationContact));
syncrhonizationContact也是一个泛型类型where TC : class, IInternalConexioEntity, IObjectState
。所以实际类型是ConexioContact。
但他没有运行第二个方法,我的值是ConexioContact类型。
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<---
您要做的不是重载。它被称为泛型专门化,在C#中不受支持。有一些脆弱的变通方法,只有在编译时具体类型为时才有效。
如果使用ConexioContact
类型的参数进行调用,编译器会知道第二个方法更具体。
如果您传递一个更通用的参数,例如IConexioContact
或object
,编译器将看到它没有特定的方法,但它可以使用通用方法创建一个。所以ConvertToEntity<IConexioContact>
战胜了ConvertToEntity(ConexioContact)
,得到了一个异常。