简体   繁体   English

.Net 中的自描述类型到底是什么意思?

[英]What exactly does a self-describing type in .Net mean?

Given this MSDN article, we learn that the Common Type System in .Net has this classification of reference types :鉴于这篇MSDN 文章,我们了解到 .Net 中的通用类型系统具有以下引用类型分类:

"Reference types can be self-describing types , pointer types , or interface types . The type of a reference type can be determined from values of self-describing types. Self-describing types are further split into arrays and class types ." “引用类型可以是自描述类型指针类型接口类型。引用类型的类型可以从自描述类型的值中确定。自描述类型进一步分为数组类类型。”

  1. So an array, for instance, is a self-describing type because we can determine its type from its values?例如,一个数组是一个自描述类型,因为我们可以从它的值来确定它的类型?
  2. How?如何?
  3. Is that it, or is there more to this definition?是这样,还是这个定义还有更多?

A self-describing type is a data type that provide information about themselves for the benefit of the garbage collector.自描述类型是一种为垃圾收集器提供有关自身信息的数据类型。 Basically any type that has some form of Metadata eg an assembly, would be considered a self-describing type.基本上,任何具有某种形式的元数据的类型(例如程序集)都将被视为自描述类型。

A self-describing type is type that is described by metadata available about itself.自描述类型是由关于其自身的可用元数据描述的类型。 The most common form are class types.最常见的形式是类类型。 There it's quite easy to show what self-describing means:在那里很容易展示自我描述的含义:

The type itself is described by the class definition.类型本身由类定义描述。 eg A customer class with a name, age and customerid.例如,具有名称、年龄和 customerid 的客户类。 The pure data for an instance of this class would be something like:此类实例的纯数据类似于:

8%3|*1C U S T O M E R

Only because the environment has the class description containing the metadata you really know that some of this data forms the id, age and name.仅因为环境具有包含元数据的类描述,您才真正知道其中一些数据构成了 id、age 和 name。 And to identify the metadata the object content data is merged with a class id so the environment can match the class description with the metadata.为了识别元数据,对象内容数据与类 id 合并,因此环境可以将类描述与元数据匹配。

|Class metadata reference: Metadata for the customer class
| |Customer ID: Field 
| |  |Customer Age: Field
| |  ||Customer Name : Field
8%3|*1C U S T O M E R

For arrays it is similar: Array classes contain information about the number of entries as well as type information (see above) about the stored entries.对于数组,它是类似的:数组类包含有关条目数量的信息以及有关存储条目的类型信息(见上文)。

Maybe the best way to show how pointer types and interface types are not self-describing is with an example:也许展示指针类型和接口类型如何不是自描述的最好方法是举个例子:

using System;

interface ISample { }
class CSample : ISample { }

class Program {
    static unsafe void Main(string[] args) {
        ISample itf = new CSample();
        var it = itf.GetType();
        Console.WriteLine(it.FullName);
        int value = 42;
        int* p = &value;
        var pt = p->GetType();
        Console.WriteLine(pt.FullName);
        Console.ReadLine();
    }
}

Output:输出:

CSample 
System.Int32

In other words, objects declared as an interface type can only describe the class that implements them.换句话说,声明为接口类型的对象只能描述实现它们的类。 Pointers can only describe the type of the object they point to.指针只能描述它们所指向的对象的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM