编辑:让我们假设这些类实际上不共享接口(interface)!我的重大失误...
由于我不知道的原因,例如有两个 WPF 类,它们都具有具有相同签名的相同方法:
所以我一直想知道(到底)我应该如何构造一个最终调用上述方法的方法(例如类型检查和异常)。
(我将发布我想做的事情,但我正在寻找更有经验的人会推荐的内容。)
最佳答案
我的方法:
public static void Animate(object target)
{
target.ThrowIfNull(); //Extension
if (!(target is Animatable || target is UIElement))
throw new ArgumentException("The target is not animatable");
//Construct animation
(target as dynamic).BeginAnimation(property, animation);
}
关于c# - 在不同类型上调用相同的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7052952/