如何是"原始"类型定义的非递归? [英] How are the "primitive" types defined non-recursively?

查看:250
本文介绍了如何是"原始"类型定义的非递归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于用C 结构#由其成员位的,你不能有一个值类型 T 这包括任何 T 字段:

Since a struct in C# consists of the bits of its members, you cannot have a value type T which includes any T fields:

// Struct member 'T.m_field' of type 'T' causes a cycle in the struct layout
struct T { T m_field; }



我的理解是,上述类型的实例永远不能被实例化* - 任何尝试这样做这样会导致实例/分配的无限循环(我猜会导致堆栈溢出 ** ?) - 或者,交替,看它的另一种方式可能是,定义本身只是没有按T意义;也许这是一个弄巧成拙的实体,有点像。这种说法是错误的

My understanding is that an instance of the above type could never be instantiated*—any attempt to do so would result in an infinite loop of instantiation/allocation (which I guess would cause a stack overflow?**)—or, alternately, another way of looking at it might be that the definition itself just doesn't make sense; perhaps it's a self-defeating entity, sort of like "This statement is false."

然而奇怪的是,如果你运行这段代码:

Curiously, though, if you run this code:

BindingFlags privateInstance = BindingFlags.NonPublic | BindingFlags.Instance;

// Give me all the private instance fields of the int type.
FieldInfo[] int32Fields = typeof(int).GetFields(privateInstance);

foreach (FieldInfo field in int32Fields)
{
    Console.WriteLine("{0} ({1})", field.Name, field.FieldType);
}



...你会得到下面的输出:

...you will get the following output:


m_value (System.Int32)

我们似乎正在谎称到这里***。显然,我的理解是原始类型,如 INT 双击等,必须在一些特殊的方式来定义深跌中C#的肠子(你不能在系统中每一个可能的单元在系统方面可以定义......可以 - 不同的话题,不管?!);我只是想知道的什么是怎么回事

It seems we are being "lied" to here***. Obviously I understand that the primitive types like int, double, etc. must be defined in some special way deep down in the bowels of C# (you cannot define every possible unit within a system in terms of that system... can you?—different topic, regardless!); I'm just interested to know what's going on here.

如何在 System.Int32 类型(例如)实际上占了32位整数的存储?更一般地,值类型(作为一种价值的定义)如何可以包括类型的字段是本身的?这似乎只是龟一路下跌

How does the System.Int32 type (for example) actually account for the storage of a 32-bit integer? More generally, how can a value type (as a definition of a kind of value) include a field whose type is itself? It just seems like turtles all the way down.

黑魔法

<子> *在一个单独的说明:这是一个值的正确的字类型(实例)?我觉得它携带的参考样的内涵;但也许这只是我。另外,我觉得我的可能的问这个问题,所以如果之前-,我忘了的人回答什么。

*On a separate note: is this the right word for a value type ("instantiated")? I feel like it carries "reference-like" connotations; but maybe that's just me. Also, I feel like I may have asked this question before—if so, I forget what people answered.

<子> **两个马丁诉Löwis埃里克利珀人士指出,这既不是完全准确,也没有在这个问题上一个适当的角度。见他们的答案获得更多信息。

**Both Martin v. Löwis and Eric Lippert have pointed out that this is neither entirely accurate nor an appropriate perspective on the issue. See their answers for more info.

<子> ***好了,我知道没有人真正说谎。我并不是在暗示我认为这是的的;我怀疑过,这在某种程度上过于简单化。来了解(我的认为的)的 thecoop的回答,这让很多我更有意义。

***OK, I realize nobody's actually lying. I didn't mean to imply that I thought this was false; my suspicion had been that it was somehow an oversimplification. After coming to understand (I think) thecoop's answer, it makes a lot more sense to me.

推荐答案

据因为我知道,存储在装配现场签名中,有代表核心的基本类型一定的硬编码的字节模式 - 签名/无符号整数和浮点数(以及字符串,这是引用类型和特殊案件)。在CLR知道本身如何处理这些。查看分区II的CLR规范了签名的位模式的节12年2月23日。

As far as I know, within a field signature that is stored in an assembly, there are certain hardcoded byte patterns representing the 'core' primitive types - the signed/unsigned integers, and floats (as well as strings, which are reference types and a special case). The CLR knows natively how to deal with those. Check out Partition II, section 23.2.12 of the CLR spec for the bit patterns of the signatures.

在每个原始的结构( [mscorlib程序] System.Int32 [mscorlib程序] System.Single 等)中的BCL是原生类型的单一领域,因为一个结构是完全的大小作为其组成字段相同,每个基元结构是相同的位组合格式作为存储器中其自身的类型,因此可以被解释为任一,由CLR,C#编译器,或使用这些类型的库。

Within each primitive struct ([mscorlib]System.Int32, [mscorlib]System.Single etc) in the BCL is a single field of that native type, and because a struct is exactly the same size as its constituent fields, each primitive struct is the same bit pattern as its native type in memory, and so can be interpreted as either, by the CLR, C# compiler, or libraries using those types.

这是C#, INT 双击等是mscorlib程序结构的同义词,这每个人都有自己的是本机由CLR认可的类型的原始现场。

From C#, int, double etc are synonyms of the mscorlib structs, which each have their primitive field of a type that is natively recognised by the CLR.

(有一个额外的复杂在这里,在该CLR规范规定,具有短表任何类型(本地CLR类型),总是要被编码为短表( INT32 ),而不是值类型[mscorlib程序] System.Int32 ,所以C#编译器知道的原始类型为好,但我不知道,在C#编译器和CLR,比如说那张精确语义和特殊套管,方法调用原始的结构)

(There's an extra complication here, in that the CLR spec specifies that any types that have a 'short form' (the native CLR types) always have to be encoded as that short form (int32), rather than valuetype [mscorlib]System.Int32. So the C# compiler knows about the primitive types as well, but I'm not sure of the exact semantics and special-casing that goes on in the C# compiler and CLR for, say, method calls on primitive structs)

因此,由于哥德尔不完全性定理,必须有一些外部通过它可以被定义的系统。这是一个让CLR解释4个字节作为一个本地的魔术 INT32 [mscorlib程序] System.Int32 ,这是从C#别名

So, due to Godel's Incompleteness Theorem, there has to be something 'outside' the system by which it can be defined. This is the Magic that lets the CLR interpret 4 bytes as a native int32 or an instance of [mscorlib]System.Int32, which is aliased from C#.

这篇关于如何是&QUOT;原始&QUOT;类型定义的非递归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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