gpt4 book ai didi

c# - C# 4.0 的 ExpandoObject 是否支持基于原型(prototype)的继承?

转载 作者:可可西里 更新时间:2023-11-01 08:06:02 26 4
gpt4 key购买 nike

C# 4.0 的 ExpandoObject支持Prototype-based inheritance ?如果不是,为什么不(是设计使然?)以及如何实现?如果是,它是如何工作的,与它在 Javascript 中的工作方式相比有什么区别?

最佳答案

Does C# 4.0's ExpandoObject support Prototype-based inheritance?

首先,请注意 ExpandoObject 类与 C# 4.0 没有任何关系。 C# 团队没有设计或实现这个对象。 C# 4.0 和 ExpandoObject 类恰好都附带了最新版本的 .NET。

为了回答您的问题,我建议您引用 documentation对于 ExpandoObject,它明确指出:

The ExpandoObject class is an implementation of the dynamic object concept that enables getting, setting, and invoking members. If you want to define types that have their own dynamic dispatch semantics, use the DynamicObject class.

如文档所述,如果您想要自定义调度语义而不仅仅是调用成员,那么请使用 DynamicObject 类。

If not, why not? was it by design?

有人可能想要一个 expando 对象,但那个人可能既不想也不需要原型(prototype)继承。 Expando 对象在逻辑上不需要任何形式的继承。

how could this be implemented?

使用 DynamicObject 对象。编写自己的原型(prototype)继承机制。

If yes, how does it work and differences are there to the way it works in Javascript?

如果您正在尝试编写与 JScript 完全一样的自己的原型(prototype)继承,我鼓励您非常仔细阅读 ECMAScript 规范。原型(prototype)继承看起来很简单,但其中的微妙之处大多数人都弄错了。例如,即使是 JScript 专家也经常弄错这个小谜题。此 JScript 代码打印什么?

var Animal = new Object(); 
function Reptile() { }
Reptile.prototype = Animal;
var lizard = new Reptile();
print(lizard instanceof Reptile); // this is true
print(lizard.constructor == Reptile); // is this true or false? explain your answer!

原型(prototype)继承并不总是像您想象的那样工作!有关打印内容和原因的解释,请参阅 my article on the subject .

关于c# - C# 4.0 的 ExpandoObject 是否支持基于原型(prototype)的继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2942791/

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