C# 重载方法中的StackOverflowException
我试图在如下代码中调用重载方法:C# 重载方法中的StackOverflowException,c#,exception-handling,overloading,stack-overflow,overload-resolution,C#,Exception Handling,Overloading,Stack Overflow,Overload Resolution,我试图在如下代码中调用重载方法: public abstract class BaseClass<T> { public abstract bool Method(T other); } public class ChildClass : BaseClass<ChildClass> { public bool Method(BaseClass<ChildClass> other) { return this.Metho
public abstract class BaseClass<T>
{
public abstract bool Method(T other);
}
public class ChildClass : BaseClass<ChildClass>
{
public bool Method(BaseClass<ChildClass> other)
{
return this.Method(other as ChildClass);
}
public override bool Method(ChildClass other)
{
return this == other;
}
}
class Program
{
static void Main(string[] args)
{
BaseClass<ChildClass> baseObject = new ChildClass();
ChildClass childObject = new ChildClass();
bool result = childObject.Method(baseObject);
Console.WriteLine(result.ToString());
Console.Read();
}
}
一切正常。
我错过什么了吗?或者这是.NET中的一个bug?
在.NET 2.0,3.5,4.0中测试表明:
首先,设置所有可访问的
(第3.5节)声明名为N的成员
在T和基本类型中(第
7.3.1)建造一个T形结构包含覆盖的声明
修饰符从集合中排除。如果
不存在名为N的成员,并且该成员为
可访问,然后查找生成
不匹配,以下步骤无效
未评估
由于这两种方法都适用,但其中一种方法被标记为覆盖,因此在确定调用哪个方法时会忽略该方法。因此,将调用当前方法,从而导致递归。进行强制转换时,重写版本是唯一适用的方法,因此您可以获得所需的行为
return ((BaseClass<ChildClass>)this).Method(other as ChildClass);