【问题标题】:"Cannot implicitly convert type 'thisMethod<T>' to 'T'"“无法将类型 'thisMethod<T>' 隐式转换为 'T'”
【发布时间】:2012-01-17 11:16:23
【问题描述】:

我在使用下面的代码时遇到了问题,希望有人能告诉我它有什么问题。

我得到的错误是:

无法将类型 ThisThing&lt;T&gt; 隐式转换为 T

我的代码:

class ThisThing<T>
{
    public string A { get; set; }
    public string B { get; set; }
}

class OtherThing
{
    public T DoSomething<T>(string str)
    {
        T foo = DoSomethingElse<T>(str);
        return foo;
    }

    private T DoSomethingElse<T>(string str)
    {
        ThisThing<T> thing = new ThisThing<T>();
        thing.A = "yes";
        thing.B = "no";

        return thing; // This is the line I'm given the error about
    }
}

想法?感谢您的帮助!

【问题讨论】:

  • 请以大写字母开头类型名称,这样更容易阅读。并且,请不要使用下划线开头的局部变量。或者,如果您这样做,请以下划线开头。这将帮助我们(更重要的是您)阅读代码并防止错误。

标签: c# .net generics compiler-errors


【解决方案1】:

方法doSomethingdoSomethingElse 的返回类型为T,而您实际上在这些方法的主体中返回thisThing&lt;T&gt;。这些不一样。

举个简单的例子,这相当于返回 List&lt;T&gt;,而您只期望 T - 它们是完全不同的类。

【讨论】:

    【解决方案2】:

    好吧,编译器这么说:

    private T doSomethingElse<T>(string thisString)
    

    需要:

    private thisThing<T> doSomethingElse<T>(string thisString)
    

    编译。

    现在修复取决于您要做什么(不使用其参数的方法已被怀疑)。

    【讨论】:

      【解决方案3】:

      您要做的是将类型thisThing 转换为类型T,这是不可能的。相反,您需要将 doSomethingElse&lt;T&gt;(...) 的返回类型更改为:

      private thisThing<T> doSomethingElse<T>(...) { ... }
      

      【讨论】:

        【解决方案4】:

        thisThing&lt;T&gt; 是一个泛型类型,T 作为类型参数。无法转换为T

        这就像是说:

        List<string> stringList = new List<string>();
        string someString = stringList;   // Makes no sense.
        

        【讨论】:

          【解决方案5】:

          您已经告诉代码该方法的返回类型是T,并且您正试图返回thisThing&lt;T&gt;。编译器不知道如何相互转换,所以它会向你抱怨。

          您需要更改方法的返回类型或更改方法中返回的内容。

          【讨论】:

            【解决方案6】:

            对我来说很清楚

            _thisThingthisThing&lt;T&gt; 类型但返回类型是 T 类型

            【讨论】:

              【解决方案7】:
              private thisThing<T> doSomethingElse<T>(string thisString) { }
              

              【讨论】:

                【解决方案8】:

                您可以像下面的代码一样更改您的otherThing 类代码并尝试

                class otherThing 
                    {
                        public otherThing() 
                        {
                        }
                        public thisThing<T> doSomething<T>(string thisString)
                        {
                            thisThing<T> foo = doSomethingElse<T>(thisString); 
                            return foo; 
                        }
                        private thisThing<T> doSomethingElse<T>(string thisString)
                        {
                            thisThing<T> _thisThing = new thisThing<T>(); 
                            _thisThing.A = "yes";
                            _thisThing.B = "no"; 
                            return _thisThing; //This is the line I'm given the error about 
                        }
                    } 
                

                【讨论】:

                  猜你喜欢
                  • 2019-12-02
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-12-31
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多