[英]How to Downcast to an Overloaded Method
我的代码中有一个小问题,我认为这很有趣:
foreach(ISceneNode node in (root as IGroupNode))
{
PreVisit(node);
if (notFound == false)
{
return node;
}
else
PostVisit(node);
}
我试图在ISceneNode对象节点上调用方法PreVisit和PostVisit,该节点是其他类型节点的父类。 但是,由于这是对象关系的“太笼统”,因此不允许调用这些方法:
//methods
void PreVisit(IDrawableNode drawable)
{
string targetName = drawable.Name;
Boolean notFound = true;
CompareToTarget(targetName, notFound, drawable);
}
void PreVisit(ITransformNode transform)
{
string targetName = transform.Name;
Boolean notFound = true;
CompareToTarget(targetName, notFound, transform);
}
void PreVisit(IStateNode state)
{
string targetName = state.Name;
Boolean notFound = true;
CompareToTarget(targetName, notFound, state);
}
void PreVisit(IGroupNode group)
{
string targetName = group.Name;
Boolean notFound = true;
CompareToTarget(targetName, notFound, group);
}
IGroupNode,IStateNode等是从ISceneNode派生的...为什么我不能仅使用ISceneNode来调用此重载方法? 是否因为不知道选择哪种方法? 如何在我的代码中说明此问题并解决该问题?
当您调用方法时,对象是ISceneNode,因为您没有定义PreVisit(ISceneNode),所以它将找不到合适的方法。
编译器将无法理解您已经为每个子类型定义了一个子案例。 一种解决方案是将其强制转换以检查您的对象是否实现了子接口之一,并在强制转换的对象上调用方法。
当然,仅在其余代码中间编写此代码并不是一个很好的解决方案。 作为SLaks何况你应该使用派遣,像这里 ,或使用C#4.0关键字dynamic
如图所示这里
这是第二个链接的示例:
class Animal
{
}
class Cat : Animal
{
}
class Dog : Animal
{
}
以下是专长:
void ReactSpecialization(Animal me, Animal other)
{
Console.WriteLine("{0} is not interested in {1}.", me, other);
}
void ReactSpecialization(Cat me, Dog other)
{
Console.WriteLine("Cat runs away from dog.");
}
void ReactSpecialization(Dog me, Cat other)
{
Console.WriteLine("Dog chases cat.");
}
现在,这就是您使用dynamic
在C#4.0中定义双重分发的方式:
void React(Animal me, Animal other)
{
ReactSpecialization(me as dynamic, other as dynamic);
}
然后跑
void Test()
{
Animal cat = new Cat();
Animal dog = new Dog();
React(cat, dog);
React(dog, cat);
}
C# dynamic
很好:
PreVisit((dynamic)node);
那将在运行时使用C#语义选择适当的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.