The type arguments for method cannot be inferred from the usage

前端 未结 6 1715
盖世英雄少女心
盖世英雄少女心 2020-12-05 04:16

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

interface ISignatur
{
    Type Type { get; }
}

interface IAccess where          


        
6条回答
  •  离开以前
    2020-12-05 04:42

    Now my aim was to have one pair with an base type and a type definition (Requirement A). For the type definition I want to use inheritance (Requirement B). The use should be possible, without explicite knowledge over the base type (Requirement C).

    After I know now that the gernic constraints are not used for solving the generic return type, I experimented a little bit:

    Ok let's introducte Get2:

    class ServiceGate
    {
        public IAccess Get1(C control) where C : ISignatur
        {
            throw new NotImplementedException();
        }
    
        public IAccess, T> Get2(ISignatur control)
        {
            throw new NotImplementedException();
        }
    }
    
    class Test
    {
        static void Main()
        {
            ServiceGate service = new ServiceGate();
            //var bla1 = service.Get1(new Signatur()); // CS0411
            var bla = service.Get2(new Signatur()); // Works
        }
    }
    

    Fine, but this solution reaches not requriement B.

    Next try:

    class ServiceGate
    {
        public IAccess Get3(C control, ISignatur iControl) where C : ISignatur
        {
            throw new NotImplementedException();
        }
    
    }
    
    class Test
    {
        static void Main()
        {
            ServiceGate service = new ServiceGate();
            //var bla1 = service.Get1(new Signatur()); // CS0411
            var bla = service.Get2(new Signatur()); // Works
            var c = new Signatur();
            var bla3 = service.Get3(c, c); // Works!! 
        }
    }
    

    Nice! Now the compiler can infer the generic return types. But i don't like it. Other try:

    class IC
    {
        public IC(A a, B b)
        {
            Value1 = a;
            Value2 = b;
        }
    
        public A Value1 { get; set; }
    
        public B Value2 { get; set; }
    }
    
    class Signatur : ISignatur
    {
        public string Test { get; set; }
    
        public IC> Get()
        {
            return new IC>(this, this);
        }
    }
    
    class ServiceGate
    {
        public IAccess Get4(IC> control) where C : ISignatur
        {
            throw new NotImplementedException();
        }
    }
    
    class Test
    {
        static void Main()
        {
            ServiceGate service = new ServiceGate();
            //var bla1 = service.Get1(new Signatur()); // CS0411
            var bla = service.Get2(new Signatur()); // Works
            var c = new Signatur();
            var bla3 = service.Get3(c, c); // Works!!
            var bla4 = service.Get4((new Signatur()).Get()); // Better...
        }
    }
    

    My final solution is to have something like ISignature, where B ist the base type and C the definition...

提交回复
热议问题