简体   繁体   English

泛型如何实现结构?

[英]How do generics implement structs?

I was thinking about this. 我在想这个。 classes are obviously passed around by ptr. 类显然是由ptr传递的。 I suspect structs are passed around by copying it but i don't know for sure. 我怀疑结构是通过复制传递但我不确定。 (it seems like a waste for an int array to have every element a ptr. and passing ptrs for ints) (对于一个int数组而言,将每个元素都设置为ptr并将ptrs传递给int)似乎是浪费

But thinking about it, List<MyStruct> can not know the size of my struct. 但考虑到它, List<MyStruct>无法知道我的结构的大小。 What happens when i do this? 我这样做会发生什么? Are there multiple copies of "List`1" and every time i use it with a storage size it does not have it creates a new implementation? 是否有“List`1”的多个副本,每次我使用它与存储大小,它没有它创建一个新的实现? (adjusting for the new offsets of T and such). (调整T等的新偏移量)。

That could make sense since the source would be in the CIL inside of a DLL. 这可能有意义,因为源将位于DLL内部的CIL中。 But i am completely guessing, how is it done? 但我完全猜测,它是如何完成的? Perhaps a reference or page # to the ECMA standards? 也许是ECMA标准的参考或页面#?

Generics use the concept of open and closed generic types: A parametrized generic class definition (ie List<T> ) is an open generic type of which the runtime generates a closed generic type for each different use you have in your code, ie a different type is created for List<int> and for List<MyStruct> - for each closed generic type the size and type of T is known at run-time. 泛型使用开放和封闭泛型类型的概念:参数化泛型类定义(即List<T> )是一种开放的泛型类型,运行时为代码中的每个不同用途生成一个封闭的泛型类型,即不同的类型为List<int>List<MyStruct>创建了类型 - 对于每个封闭的泛型类型, T的大小和类型在运行时是已知的。

Clarification from MSDN : 来自MSDN的澄清:

When a generic type or method is compiled into Microsoft intermediate language (MSIL), it contains metadata that identifies it as having type parameters. 将泛型类型或方法编译为Microsoft中间语言(MSIL)时,它包含将其标识为具有类型参数的元数据。 How the MSIL for a generic type is used differs based on whether the supplied type parameter is a value type or reference type. 如何使用泛型类型的MSIL根据提供的类型参数是值类型还是引用类型而不同。

When a generic type is first constructed with a value type as a parameter, the runtime creates a specialized generic type with the supplied parameter or parameters substituted in the appropriate locations in the MSIL. 当首次使用值类型作为参数构造泛型类型时,运行时会创建一个专用泛型类型,其中提供的参数或参数将替换为MSIL中的相应位置。 Specialized generic types are created one time for each unique value type that is used as a parameter. 为每个用作参数的唯一值类型创建一次专用泛型类型。

Generics work somewhat differently for reference types. 泛型对参考类型的工作方式有所不同。 The first time a generic type is constructed with any reference type, the runtime creates a specialized generic type with object references substituted for the parameters in the MSIL. 第一次使用任何引用类型构造泛型类型时,运行时会创建一个专用泛型类型,其中对象引用将替换MSIL中的参数。 Then, every time that a constructed type is instantiated with a reference type as its parameter, regardless of what type it is, the runtime reuses the previously created specialized version of the generic type. 然后,每次构造类型以引用类型作为其参数进行实例化时,无论它是什么类型,运行时都会重用先前创建的泛型类型的专用版本。 This is possible because all references are the same size. 这是可能的,因为所有引用都是相同的大小。

The CLR compiles 1 version of the generic class and uses it for all reference types. CLR编译通用类的1个版本,并将其用于所有引用类型。 It also compiles 1 version for every value type usage to optimize for performance. 它还为每种值类型使用编译1个版本以优化性能。

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

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