0

I am trying to overload a Generic method with a specific method. But he is not going to the specifiek typed method, he takes the generic method that will not be implemented.

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)
{

}
}

The method call:

result.Add(ConvertEntity(synchronizationContact));

And the syncrhonizationContact is also a generic type where TC : class, IInternalConexioEntity, IObjectState. So the actual type is ConexioContact.

But he is not running to the second method and my value is of type 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<---
kevingoos
  • 3,785
  • 4
  • 36
  • 63

1 Answers1

4

What you are trying to do isn't overloading. It's called generic specialization and is not supported in C#. There are some brittle workarounds that work only if the concrete type is known at compile time.

If you make the call using a ConexioContact-typed parameter the compiler knows that the second method is the more specific.

If you pass a more generic parameter, eg IConexioContact or object, the compiler will see that it has no specific method but it can create one using the generic method. So ConvertToEntity<IConexioContact> wins over ConvertToEntity(ConexioContact) and you get an exception.

Community
  • 1
  • 1
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236