English 中文(简体)
如何确定结构类型?
原标题:
  • 时间:2009-05-29 14:44:27
  •  标签:

在参考类别中,物体的记忆布局是

| Type Object pointer|
|    Sync Block      |
|  Instance fields...|

对价值类型而言,标的布局似乎是

|  Instance fields...|

就参考类型而言,GetType是指从类型目标点发现物体。 特定参比物体的所有物体均指同一类型物体(也附有方法表)

就价值类型而言,该点码是可用的。 因此,GetType()的工作如何?

我与谷歌进行了检查,我发现这片ni。 是否有人阐述?

The solution is that the location in which a value is stored may only store values of a certain type. This is guaranteed by the verifier. Source

最佳回答
问题回答

Maybe Andrew H.将这一点视为显而易见的,并试图使我理解+1,我的灯布时来自Joon Skeet(这是通过我所阅读的书本,也是在答案所在的确切地区周围)。

  • C# is a statically typed. Each variable has a type and it is known at compile time.
  • Value types can t be inherited. As a result, the VT objects don t need to carry around extra type information (as opposed to Ref Type objects, each of which have an object type header since the variable type and value/object type may differ.)

考虑如下文。 虽然变型类型是“基地”,但还是指一种较为专业化的物体。 对于价值类型而言,由于遗产被宣布为非法,因此物体类型为

BaseRefType r = new DerivedRefType(); 
ValueType v = new ValueType();

My missing piece was bullet#1.
<Snipped after J.Skeet s comment since it seems to be wrong>. There seems to be some magic that lets the compiler/runtime know the type of the variable given any arbitrary variable. So the runtime somehow knows that ob is of MyStruct type, even though the VT object itself has no type information.

MyStruct ob = new MyStruct();
ob.WhoAmI();                          // no box ; defined in MyStruct
Console.WriteLine(ob.GetHashCode());  // no box ; overridden in ValueType
Console.WriteLine( ob.GetType() );    // box ; implemented in Object

因此,我能够援引MyStruct(和价值Type(出于某种原因)所定义的方法,而没有插手RefType。





相关问题