有没有委托这不是一个MulticastDelegate在C#中? [英] Is there a Delegate which isn't a MulticastDelegate in C#?

查看:431
本文介绍了有没有委托这不是一个MulticastDelegate在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想答案是否定?如果没有,为什么我们已经分居代理 MulticastDelegate 班?也许是因为一些其他.NET语言?

I think the answer is NO? If there isn't, why do we have separated Delegate and MulticastDelegate classes? Maybe it's again because of "some other .NET languages"?

推荐答案

编辑:我的认为的,这是ECMA 335的一部分,但我看不出它有任何地方

I thought this was part of ECMA 335, but I can't see it in there anywhere.

您不能创建在C#这样的委托类型,但你可以在IL:

You can't create such a delegate type in C#, but you can in IL:

.class public auto ansi sealed Foo
       extends [mscorlib]System.Delegate
{
    // Body as normal
}

C#编译器具有使用这样的代表没有问题:

The C# compiler has no problems using such a delegate:

using System;

class Test
{
    static void Main()
    {
        Foo f = x => Console.WriteLine(x);
        f("hello");
    }
}

但CLR,当它试图加载它:

But the CLR does when it tries to load it:

未处理的异常:System.TypeLoadException:未能从程序集富,版本= 0.0.0.0,文化=中性公钥=空'加载类型'富',因为它不能直接从委托类继承。      在Test.Main()

Unhandled Exception: System.TypeLoadException: Could not load type 'Foo' from assembly 'Foo, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' because it cannot inherit directly from the delegate class. at Test.Main()

基本上代表/ MulticastDelegate分离是一种历史的偶然。我相信,早期的alpha / beta版本并进行区分,但事实证明这太混乱,一般没有什么用处 - 所以现在的每次的代表从MulticastDelegate派生

Basically the Delegate/MulticastDelegate separation is an historical accident. I believe that early alpha/beta versions did make the distinction, but it proved too confusing and generally not useful - so now every delegate derives from MulticastDelegate.

(有趣的是,C#的规范只提到MulticastDelegate一次,在不能被用作通用的约束的类型列表。)

(Interestingly, the C# specification only mentions MulticastDelegate once, in the list of types which can't be used as generic constraints.)

这篇关于有没有委托这不是一个MulticastDelegate在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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