但想一想,List< MyStruct>无法知道我的结构的大小.我这样做会发生什么?是否有“List`1”的多个副本,每次我使用它与存储大小,它没有它创建一个新的实现? (调整T等的新偏移量).
这可能有意义,因为源将在DLL内部的CIL中.但我完全猜测,它是如何完成的?也许是ECMA标准的参考或页面#?
解决方法
从MSDN开始澄清:
When a generic type or method is
compiled into Microsoft intermediate
language (MSIL),it contains Metadata
that identifies it as having type
parameters. How the MSIL for a generic
type is used differs based on whether
the supplied type parameter is a value
type or reference type.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. 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. 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.