哪种语言可以在运行时动态更改类成员?
我知道在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您正在寻找原型继承。在同一维基百科页面中提到了语言列表。
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.
是的,在 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#.
像 C# 这样的静态类型系统的全部要点是,为特定类型定义的所有功能在编译时都是已知的(并经过检查)。
如果您编写
的编译器验证无论
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
the compiler verifies that whatever type
foo
has, it supports an operation calledjump
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.
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."
在 C# 4 中,您拥有可以在运行时添加/修改的动态对象。
in C# 4 you have dynamic object which you can add/modify at run time.