有没有新的“动态”变量类型在.NET 4.0中解决CLR的单/多方法分配问题? [英] Does new 'dynamic' variable type in .NET 4.0 solve the single/multiple method dispatch issue in CLR?

查看:260
本文介绍了有没有新的“动态”变量类型在.NET 4.0中解决CLR的单/多方法分配问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单牒的问题主要是熟悉的人从事与像Java和C#静态类型语言编码。其基本思想是:

The problem of single dispatch is mostly familiar to people engaged in coding with statically typed languages like Java and C#. The basic idea is:

虽然运行时多态性使我们能够根据接收的类型(运行时类型)派遣到正确的方法调用,例如:

While the runtime polymorphism allows us to dispatch to the right method call according to the type (runtime type) of receiver, for example:

IAnimal mything = new Cat();
mything.chop();

该方法调用将根据 mything 的运行时类型进行的,即。 这是唯一的调度能力(这是present用Java / C#)。

The method call will be performed according to the runtime type of mything, namely Cat. This is the single dispatch capability (which is present in Java/C#).

现在,如果你需要在接收器的运行时类型不仅出动,但在(多个)参数的类型要么,你面对一个小问题:

Now, if you need to dispatch not only on the runtime type of receiver, but on the types of (multiple) arguments either, you face a little problem:

public class MyAcceptor {  
    public void accept (IVisitor vst) {...}   
    public void accept (EnhancedConcreteVisitor vst) {...}  
}

第二种方法不会被调用,因为在我们的消费者code,我们只是倾向于他们共同的超类型或接口来治疗不同类型的对象(在我的例子观众)的。

The second method never gets called, because in our 'consumer' code we just tend to treat different types of objects (visitors in my example) by their common supertype or interface.

这就是为什么我问 - 因为动态类型允许多分派多态性和C#4.0中有一个动态的关键字;)

That's why I ask - because dynamic typing allows the multiple dispatch polymorphism and C# 4.0 has that dynamic keyword ;)

推荐答案

是的,动态类型允许多个调度 - 也没有,你没有创建自己的动态对象做到这一点

Yes, dynamic typing allows multiple dispatch - and no, you don't have to create your own dynamic object to do it.

假设我们希望实施 Enumerable.Count()自己,我们不希望的负荷如果(来源IList的) 测试在我们的code。我们可以写这样的:

Suppose we wanted to implement Enumerable.Count() ourselves, and we didn't want a load of "if (source is IList)" tests in our code. We could write it like this:

public static class Enumerable
{
    public static int Count<T>(this IEnumerable<T> source)
    {
        dynamic d = source;
        return CountImpl(d);
    }

    private static int CountImpl<T>(ICollection<T> collection)
    {
        return collection.Count;
    }

    private static int CountImpl(ICollection collection)
    {
        return collection.Count;
    }

    private static int CountImpl<T>(string text)
    {
        return text.Length;
    }

    private static int CountImpl<T>(IEnumerable<T> source)
    {
        // Fallback
        int count = 0;
        foreach (T t in source)
        {
            count++;
        }
        return count;
    }
}

我并不是说这将是一个不错的主意,但是这是它如何工作的:)

I'm not saying it would be a good idea, but that's how it would work :)

请注意,你需要小心不要引入在那里你可以有一个模糊的呼叫对于某些类型的最终情况。这种使用类的参数就不会是一个问题,但考虑到一个类可以实现多个接口。

Note that you need to be careful not to introduce situations where you could end up with an ambiguous call for some types. This wouldn't be an issue using classes for parameters, but consider that a single class can implement multiple interfaces.

这篇关于有没有新的“动态”变量类型在.NET 4.0中解决CLR的单/多方法分配问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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