English 中文(简体)
不同类型采用相同的方法
原标题:Calling the same method on different types

<><>Edit>: 让我们承担实际的do not分成一个接口! 我方说......


出于我所不知的原因,例如,两个妇女论坛的班级都有相同的签名方法:

因此,我很想知道(for the heck of it)ishould如何构建一种方法(例如,打字和安插;例外情况),最终称之为上述方法。

(I”将介绍哪些方面愿意做,但我想到哪些经验丰富的人会建议做些什么。

最佳回答

我的方法:

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);
}
问题回答

如你提议,这两个班级都共用相同的接口<条码>。

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

应足够

Here。

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

我认为,这是更清洁的。

<>蓬贝> 缩略语

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

在你对不继承同一接口的两门课进行编辑后,答案是使用以下一类:

  1. Reflection
  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方法,则该呼吁在操作时将失效。

汇编者不帮助(或停止)具有动态。 故障推迟到运行时间:





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...