“无法隐式转换类型“thisMethod””到“T”

发布于 12-27 13:29 字数 697 浏览 4 评论 0原文

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

我给出的错误是:

无法将类型 ThisThing 隐式转换为 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
    }
}

想法?我感谢你的帮助!

I'm having trouble with the below code and was hoping someone out there could tell me what's wrong with it.

The error I'm given is:

Cannot implicitly convert type ThisThing<T> to T

My code:

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
    }
}

Thoughts? I appreciate your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

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

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

The methods doSomething and doSomethingElse have a return type of T, whereas you're actually returning a thisThing<T> in the body of those methods. These are not the same.

For an easy example, this would be equivalent of returning List<T> where you expect just T - they're completely different classes.

时间你老了2025-01-03 13:29:24

好吧,编译器说:

private T doSomethingElse<T>(string thisString)

需要:

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

才能编译。

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

Well, the compiler says it:

private T doSomethingElse<T>(string thisString)

would need to be:

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

for it to compile.

Now the fix depends on what are you trying to do (a method which doesn't use its parameter is suspect already).

慢慢从新开始2025-01-03 13:29:24

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

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

What you are trying to do is convert the type thisThing to the type T, which is not possible. Instead, you need to change your return type of doSomethingElse<T>(...) to:

private thisThing<T> doSomethingElse<T>(...) { ... }
我的鱼塘能养鲲2025-01-03 13:29:24

thisThing 是一个泛型类型,以 T 作为类型参数。它无法转换为 T

这就像说:

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

thisThing<T> is a generic type with T as the type parameter. It can't be converted to a T.

That would be like saying:

List<string> stringList = new List<string>();
string someString = stringList;   // Makes no sense.
‖放下2025-01-03 13:29:24

您已告诉代码该方法的返回类型是 T 并且您正在尝试返回 thisThing。编译器不知道如何将其中一个转换为另一个,因此它会向您抱怨这一点。

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

You have told the code that the return type for that method is T and you are trying to return a thisThing<T>. The compiler has no idea how to convert one from the other so it is complaining to you about it.

You need to either change the return type for your method or change what you are returning in the method.

寻找我们的幸福2025-01-03 13:29:24

对我来说很清楚

_thisThing 的类型为 thisThing 但返回类型的类型为 T

It's clear for me

_thisThing is of type thisThing<T> but the return type is of type T

枉心2025-01-03 13:29:24
private thisThing<T> doSomethingElse<T>(string thisString) { }
private thisThing<T> doSomethingElse<T>(string thisString) { }
独闯女儿国2025-01-03 13:29:24

您可以更改您的 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 
        }
    } 

You can change your otherThing class code like the below code and try

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 
        }
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文