无法从Foo< F>到F? [英] Cannot convert from Foo<F> to F?
本文介绍了无法从Foo< F>到F?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在C#中使用了 CRTP风格的泛型构造,并且有这些类:
公共抽象类Foo< F>其中F:Foo< F>
{
public abstract Bar< F>酒吧{get; }
public void Baz()
{
return this.Bar.Qux(this); //错误在这里:参数1:无法从'Foo< F>'转换为'F'
}
}
公共抽象类Bar< F>其中F :Foo< F>
{
public abstract void Qux(F foo);
}
这对我来说很奇怪,考虑到这个
应该总是一个Foo,对吗?
这个是一个
Foo< F>
。 Qux的参数被声明为 F
,它需要改为 Foo
: public abstract class Foo 其中F:Foo {
public abstract Bar< F> bar {get;}
public void Baz(){
Bar.Qux(this);
}
}
公共抽象类Bar< F>其中F:Foo {F> {
public abstract void Qux(Foo< F> foo);
}
I'm using a CRTP-style generic construct in C#, and have these classes:
public abstract class Foo<F> where F : Foo<F>
{
public abstract Bar<F> Bar { get; }
public void Baz()
{
return this.Bar.Qux(this); //Error is here: "Argument 1: Cannot convert from 'Foo<F>' to 'F'
}
}
public abstract class Bar<F> where F : Foo<F>
{
public abstract void Qux(F foo);
}
This seems very strange to me, considering this
should always be a Foo, right?
解决方案
In that context, this
is a Foo<F>
. The argument of Qux is declared as F
. It needs to be changed to Foo<F>
:
public abstract class Foo<F> where F : Foo<F> {
public abstract Bar<F> Bar { get; }
public void Baz() {
Bar.Qux(this);
}
}
public abstract class Bar<F> where F : Foo<F> {
public abstract void Qux(Foo<F> foo);
}
这篇关于无法从Foo< F>到F?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文