Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type arguments for method cannot be inferred from the usage

Maybe I'm overworked, but this isn't compiling (CS0411). Why?

interface ISignatur<T> {     Type Type { get; } }  interface IAccess<S, T> where S : ISignatur<T> {     S Signature { get; }         T Value { get; set; } }  class Signatur : ISignatur<bool> {     public Type Type     {         get { return typeof(bool); }     } }  class ServiceGate {     public IAccess<S, T> Get<S, T>(S sig) where S : ISignatur<T>     {         throw new NotImplementedException();     } }  static class Test {     static void Main()     {         ServiceGate service = new ServiceGate();         var access = service.Get(new Signatur()); // CS4011 error     } } 

Anyone an idea why not? Or how to solve?

like image 644
Ben Avatar asked Oct 12 '10 17:10

Ben


1 Answers

Get<S, T> takes two type arguments. When you call service.Get(new Signatur()); how does the compiler know what T is? You'll have to pass it explicitly or change something else about your type hierarchies. Passing it explicitly would look like:

service.Get<Signatur, bool>(new Signatur()); 
like image 164
Kirk Woll Avatar answered Oct 02 '22 11:10

Kirk Woll