类本身在运行时存储在哪里以用作引用 [英] Where is the class itself stored at runtime to be used as a reference

查看:119
本文介绍了类本身在运行时存储在哪里以用作引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#或Java中,我们发现对象存储在堆上,它们的引用变量存储在堆栈中。但是在运行时,存储的类定义是否用作创建对象的模板?

In C# or Java we find that the objects are stored on heap and their reference vars are stored on stack. But at run time where is the class definition stored to be used as a template for creating objects?

推荐答案

它不存储在任何地方。编译器使用类定义在最终程序中生成可执行代码。如果你的意思是可以通过反射找到的项目,那么该信息是固定内存中生成的代码的一部分。
It is not stored anywhere. The class definition is used by the compiler to generate the executable code in the final program. If you mean the items that can be found via reflection then that information is part of the generated code in fixed memory.


类本身存储在程序集的代码中并且其元数据的。 .NET是所有类型的所有信息都存储在相应程序集中的平台之一。 .NET元数据是.NET的核心概念之一;它使反射成为可能。你真的需要学习它:

类型类(系统) [ ^ ],

反思(计算机)编程) - 维基百科,免费的百科全书 [ ^ ],

元数据和自我 - 描述组件 [ ^ ],

.NET Framework中的反思 [ ^ ]。



中心类是班级 System.Type (上面引用)。该类的实例包含所有可能需要使用的类型,包括对每个方法的IL代码的引用。所有这些都可以使用反射来检索。



术语引用与类的实例有关,而不是类。尽管如此,实例始终保持对其类型的引用,可以通过方法检索 System.Object.GetType()

Object.GetType Method(System) [ ^ ]。



-SA
The class itself is stored in the code of your assembly and its metadata. .NET is one of the platform where all the information on all types is stored in the corresponding assemblies. .NET metadata is one of the central concepts of .NET; it makes reflection possible. You really need to learn it:
Type Class (System)[^],
Reflection (computer programming) - Wikipedia, the free encyclopedia[^],
Metadata and Self-Describing Components[^],
Reflection in the .NET Framework[^].

The central class is the class System.Type (referenced above). The instance of this class holds all one may need to use the type, including the references to the IL code of each method. All this can be retrieved using reflection.

The term "reference" is related to the instance of the class, not class. Nevertheless, the instance always keep the reference to its type, it can be retrieved via the method System.Object.GetType():
Object.GetType Method (System)[^].

—SA


在Java中,类本身只能存储在堆上。

它必须存在,因为静态字段应该在VM的生命周期内可访问。
In Java, the class itself can only be stored on the heap.
It has to be there because the static fields should be accessible during the lifetime of the VM.


这篇关于类本身在运行时存储在哪里以用作引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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