本文介绍了对方法的类型参数不能从使用推断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我是劳累过度,但这不是编译(CS0411)。为什么

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?

推荐答案

获取LT; S,T> 需要两式参数。当你调用 service.Get(新签字:()); 编译器如何知道什么 T 是?你必须明确地传递或改变你的类型层次别的东西。传递它明确地将如下所示:

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());

这篇关于对方法的类型参数不能从使用推断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:12