哪种语言可以在运行时动态更改类成员?

发布于 2024-09-16 14:15:21 字数 81 浏览 3 评论 0原文

我知道在 Ruby 中可以在运行时动态添加和修改类的方法。那么其他语言呢?什么是 C#,可以在这种语言中修改或添加一些方法以及...在运行时动态吗?

I know in Ruby can add and modify method of class dynamically in run time. what about other language? what is C# ,can in this language modify or add some method and ... in run time and dynamically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一片旧的回忆 2024-09-23 14:15:22

我认为您正在寻找原型继承。在同一维基百科页面中提到了语言列表

SO上有一个类似问题,您可以查找一下。

I think you are looking for prototype inheritance. A list of languages is mentioned in the same wikipedia page.

There is a similar question on SO which you can look up.

铁憨憨 2024-09-23 14:15:22

是的,在 C# 中,您可以通过反射和 发射器对象

在 C# 4.0 中,您甚至可以使用纯 C# 代码使用 Expando 对象。这可以说更接近 Ruby 方式(如果我没记错的话,它实际上是一个抄本)并且更容易使用。

编辑:所有这些都适用于所有 .NET 语言,包括 VB.Net 和 F#。

Yes, in C# you can add methods at runtime through reflection and the Emitter object.

In C# 4.0 you can even do it in plain C# code with the Expando object. This is arguably closer to the Ruby way (it's practically a carbon copy if I remember correctly) and a lot easier to use.

Edit: All of this applies to all .NET languages, including VB.Net and F#.

素罗衫 2024-09-23 14:15:22

像 C# 这样的静态类型系统的全部要点是,为特定类型定义的所有功能在编译时都是已知的(并经过检查)。

如果您编写

foo.jump(42);

的编译器验证无论 foo 具有什么类型,它都支持名为 jump 的操作,该操作采用整数参数。

最近,C# 获得了通过动态类型动态检查对象的可能性,这基本上允许您在非常有限的上下文中描述内容,但尽管如此,整个语言都是静态类型的。

所以剩下的是像 Ruby 这样的动态语言,其中方法的存在只是在运行时(或调用时)检查。

我认为 JavaScript 可以改变所谓的原型来向对象添加方法,基本上可以实现与 Ruby 相同的效果。

The whole point of static type systems like C#'s is that all functionality defined for a particular type is known (and checked) at compile-time.

If you write

foo.jump(42);

the compiler verifies that whatever type foo has, it supports an operation called jump taking an integer parameter.

Recently, C# got the possibility of having dynamically checked objects through the dynamic type, which basically allows what you described in a very limited context, but nevertheless, the overall language is statically typed.

So what's left are dynamic languages like Ruby, where method existence is just checked at run-time (or call-time).

I think JavaScript can change so called prototypes to add methods to objects and basically achive the same thing as Ruby.

薔薇婲 2024-09-23 14:15:22

Python 擅长此操作 - 这里有很多示例: Python:更改方法和属性在运行时

Lisp 的对象系统也是相当动态的:
http://en.wikipedia.org/wiki/Common_Lisp_Object_System
“CLOS 是动态的,这意味着不仅其对象的内容,而且其对象的结构都可以在运行时修改。CLOS 支持动态更改类定义(即使相关类的实例已经存在)以及通过 CLOS 更改给定实例的类成员身份还允许在运行时添加、重新定义和删除方法。”

Python excels at this operation - here are bunch of examples: Python: changing methods and attributes at runtime

Lisp's object system is also quite dynamic:
http://en.wikipedia.org/wiki/Common_Lisp_Object_System
"CLOS is dynamic, meaning that not only the contents, but also the structure of its objects can be modified at runtime. CLOS supports changing class definitions on-the-fly (even when instances of the class in question already exist) as well as changing the class membership of a given instance through the change-class operator. CLOS also allows one to add, redefine and remove methods at runtime."

梦言归人 2024-09-23 14:15:22

在 C# 4 中,您拥有可以在运行时添加/修改的动态对象。

in C# 4 you have dynamic object which you can add/modify at run time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文