gpt4 book ai didi

delegates - 是否有可能扩展 System.Delegate?

转载 作者:行者123 更新时间:2023-12-04 05:04:14 27 4
gpt4 key购买 nike

在 C# 中不能从 System.Delegate 或 System.MulticastDelegate 继承。只要您声明标准的“运行时托管”方法,就完全可以在 MSIL 中执行此操作。但是,每次我向类型添加“cil 管理”方法时,我都会得到:

System.TypeLoadException:运行时实现的委托(delegate)方法的非法定义。

是否可以扩展 Delegate/MulticastDelegate?

最佳答案

不是使用您自己的自定义代码,在 C# 中。从 C# 3.0 规范的第 10.1.4 节:

The direct base class of a class type must not be any of the following types: System.Array, System.Delegate, System.MulticastDelegate, System.Enum, or System.ValueType. Furthermore, a generic class declaration cannot use System.Attribute as a direct or indirect base class.



但是,每次创建委托(delegate)类型时,它都会自动派生自 MulticastDelegate .

来自 ECMA-335 ,第 8.9.3 节:

While, for the most part, delegates appear to be simply another kind of user-defined class, they are tightly controlled. The implementations of the methods are provided by the VES, not user code. The only additional members that can be defined on delegate types are static or instance methods.



这听起来像是在禁止构造函数。我个人会使用普通类型的静态方法。

关于delegates - 是否有可能扩展 System.Delegate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/752466/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com