The type arguments for method cannot be inferred from the usage

前端 未结 6 1726
盖世英雄少女心
盖世英雄少女心 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:46

    As I mentioned in my comment, I think the reason why this doesn't work is because the compiler can't infer types based on generic constraints.

    Below is an alternative implementation that will compile. I've revised the IAccess interface to only have the T generic type parameter.

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

提交回复
热议问题