2011-08-13 58 views
2

编辑:让我们假设实际上的类别不是共享一个接口!在我的部分主要滑...调用不同类型的相同方法


因我的原因不明有例如两个WPF类这两个具有相同签名同样的方法:

所以我一直想知道(为什么它)我怎么样应该构建方法(例如,类型检查&例外),最终调用上述方法。

我会发布什么,我会倾向于做的,但我在寻找什么更多的有经验的人会推荐

+0

您是否知道要调用哪种方法的实例的类型?或者它是对象? – hazzik

+0

@hazzik:只要考虑一个方法来获取某个对象,它就想调用一个特定的方法,唯一知道的就是方法名和它的签名,并且该方法存在于某些类型中(名字已知),所以在里面所输入的设计方法或者需要铸造,或者您可以尝试反射/动态。 –

回答

3

我的方法:

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); 
} 
1

在这种情况下,你已经提出,两个类共享相同的接口IAnimatable

((IAnimatable)target).BeginAnimation(property, animation); 

应足以

Here是文档

+0

哦,什么,我以为我真的检查了一个共同的界面 - 这是有点不幸。 –

+0

欣赏你的答案,但我编辑了我的问题,关注不存在接口的一个相当糟糕的架构的不太可能的情况。抱歉。 –

+0

我很高兴你做到了,这是我经常问自己的一个问题。必须有一个这个我现在无法想到的常见例子。 –

1
public static void Animate(this object target, DependencyProperty property, AnimationTimeline animation) 
{ 
    target.ThrowIfNull(); 
    DoAnimate(target as dynamic); 
} 

private static void DoAnimate(object target, DependencyProperty property, AnimationTimeline animation) 
{ 
    throw new ArgumentException("The target is not animatable") 
} 

private static void DoAnimate(Animatable target, DependencyProperty property, AnimationTimeline animation) 
{ 
    target.BeginAnimation(property, animation); 
} 

private static void DoAnimate(UIElement target, DependencyProperty property, AnimationTimeline animation) 
{ 
    target.BeginAnimation(property, animation); 
} 

在我的角度来看,这是清洁。

修订与AnimationContext

class AnimationContext 
{ 
    private readonly DependencyProperty property; 
    private readonly AnimationTimeline animation; 

    public AnimationContext(DependencyProperty property, AnimationTimeline animation) 
    { 
     this.property = property; 
     this.animation = animation; 
    } 

    public void Animate(UIElement target) 
    { 
     target.BeginAnimation(property, animation); 
    } 

    public void Animate(Animatable target) 
    { 
     target.BeginAnimation(property, animation); 
    } 

    public void Animate(object target) 
    { 
     throw new ArgumentException("The target is not animatable"); 
    } 
} 

static class AnimateExtensions 
{ 
    public static void Animate(this object target, DependencyProperty property, AnimationTimeline animation) 
    { 
     target.ThrowIfNull(); 
     new AnimationContext(property, animation).Animate(target as dynamic); 
    } 
} 
+0

超载 - 很好。 –

+0

嗯,这些方法需要'property'和'animation'的参数,这会导致相当长的签名。 –

+0

您可以将其他参数封装到单个参数对象(http://www.refactoring.com/catalog/introduceParameterObject.html)中以减少签名 – hazzik

1

上两个班不是来自同一个接口继承您的编辑后例子,答案是使用下列之一:

  1. 思考
  2. Dynamic

这些都不会检查该方法是否存在直到运行时,但我认为这是问题中的暗示。

考虑到以下两个类A和B,其中包含具有相同签名的方法,但不实现相同的接口:

class A 
    public void Handle(string s) { 
     Console.WriteLine("Hello from A: " + s); 
    } 
} 

和:

class B 
    public void Handle(string s) { 
     Console.WriteLine("Hello from B: " + s); 
    } 
} 

您可以创建一个方法来处理任何具有该签名方法的对象,如下所示:

static void HandleObject(dynamic anything) { 
    anything.Handle("It works!"); 
} 

HandleObject基本上将任何对象作为输入,并且在运行时尝试盲目地调用一个名为Handle的方法。如果对象没有Handle方法,则调用在运行时将失败。

编译器没有帮助(或停止)动态。故障将推迟到运行时间:)

+0

似乎与[我的回答](http:// stackoverflow。 com/questions/7052952/calling-the-same-method-on-different-types/7052954#7052954),只是在方法参数中进行了强制转换,并且没有针对错误的输入类型引发特殊的异常。 –

+0

确实如此。看起来我没有很好地阅读你的答案。 –

相关问题