基于运行时的参数类型的方法动态选择 [英] Dynamic selection of method based on runtime parameter type

查看:212
本文介绍了基于运行时的参数类型的方法动态选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过类似的问题/这张贴在过去的答案,但我从我见过的其他人略有不同。

I've seen similar questions/answers for this posted in the past, but mine differs slightly from the others that I've seen.

从本质上讲,我有一个共同的接口和实现/继承它几类。然后,在一个单独的类,我必须经接口IObject给出对象行为的方法。然而,他们每个人都必须在以不同的方式行事,因此,为什么有所有扩展IObject每个具体类型的方法的一个单独的声明。

Essentially, I have a common interface and several classes that implement/inherit from it. Then, in a separate class, I have methods that must act upon objects given by the interface IObject. However, each of them must be acted upon in different ways, hence why there is a separate declaration of the method for each concrete type that extends IObject.

class IObject
{
    ...
}

class ObjectType1 : IObject
{
    ...
}

class ObjectType2 : IObject
{
    ...
}

class FooBar
{
    void Foo (ObjectType1 obj);
    void Foo (ObjectType2 obj);
}

现在,对我来说,一个明显的解决方案是通过利用动态绑定放置方法foo每个类,它会在运行时正确美孚执行自动选择里面。然而,这是的不可以选项这里,因为我定义了如何对这些对象的行为多个模型,我宁愿封装各个型号在自己的类中处理对象,而不是东西全部的车型引入的对象类。

Now, to me, one obvious solution is to take advantage of dynamic binding by placing the method Foo inside each individual class, which would automatically choose at runtime the correct Foo to execute. However, this is not an option here, because I am defining multiple models for how to act upon these objects, and I would rather encapsulate each individual model for handling objects in its own class, rather than stuff all of the models into the object classes.

我发现的这个帖子里面显示了如何使用字典在运行时动态选择正确的方法实现。我很好,这种方法;然而,假设我必须在每一个模型执行一次这样的调度。如果我只有IObject及其具体实现,有没有什么办法来概括这种方法,这样我可以调用任何名称的方法的根据运行时类型的对象的吗?

I found this post which shows how to use a dictionary to dynamically choose at runtime the correct method implementation. I'm fine with this approach; however, suppose that I have to perform a dispatch like this once in every model. If I only have IObject and its concrete implementations, is there any way to generalize this approach so that I could call methods of any name based on the runtime type of the objects?

我知道这可能是一个不明确的问题,但我将不胜感激任何帮助。

I know this is probably an unclear question, but I would greatly appreciate any help.

推荐答案

动态关键字居然真的擅长此道:

The dynamic keyword is actually really good at this:

void Main()
{
    var foobar = new FooBar();
    foreach(IObject obj in new IObject[]{ new ObjectType1(), new ObjectType2()})
    {
        foobar.Foo((dynamic)obj);
    }   
    // Output:
    //  Type 1
    //  Type 2
}

class IObject
{
}

class ObjectType1 : IObject
{
}

class ObjectType2 : IObject
{
}

class FooBar
{
    public void Foo (ObjectType1 obj) {
        Console.WriteLine("Type 1");
    }
    public void Foo (ObjectType2 obj) {
        Console.WriteLine("Type 2");
    }
}



中的代码是超级简单,它有< A HREF =http://stackoverflow.com/questions/7478387/dynamic-and-performance/7478557#7478557>相当不错的效果。

这篇关于基于运行时的参数类型的方法动态选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆