StructLayout在继承关系中的使用
2008-01-16 11:17
369 查看
Type Layout in Inheritance chains
A type has a type layout attribute, which specifies how the fields of an instance of a type are arranged. A type can have only one layout attribute.
There are 3 possible kinds of layout:
Auto – the layout is done by the CLI. This is the layout that ILASM uses if no layout attribute is specified by the user.
Explicit – the layout of the fields is explicitly provided. Note, that generic types cannot have explicit layout.
[ StructLayout( LayoutKind.Explicit )]
public class C
{
[ FieldOffset(0)] public int i;
[ FieldOffset(4)] public object o;
}
Sequential – The fields are laid out sequentially as they appear in the definition of the type.
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct S2
{
public int i1;
public char c1;
}
One of the questions I have seen asked with regard to layout is why in some cases using LayoutKind.Sequential or LayoutKind.Explicit causes a TypeLoadException to be thrown when the type is used.
The reason is that layout cannot start part way down the inheritance chain.
If a type is marked Sequential or Explicit, then it has layout. If the type is part of an inheritance chain, then all its parent classes must have layout as well, up to the type that directly extends System.Object or System.ValueType.
If somewhere along the inheritance chain a parent type has no layout, runtime will throw a TypeLoadException.
However, it is valid to stop using layout at any point down the chain.
Examples from ECMA spec:
In the diagrams below, Class A derives from System.Object; Class B derives from A; class C derives from B. System.Object has no layout. But A, B, and C are all defined with layout, and that is valid.
![](http://blogs.msdn.com/photos/dmilirud/images/598510/original.aspx)
The situation with classes E, F, and G is similar - G has no layout, and this too is valid.
In the following two cases the inheritance chain is invalid. On the left, H doesn't have a layout and on the right there is a 'hole' in the inheritance chain, at L.
![](http://blogs.msdn.com/photos/dmilirud/images/598509/original.aspx)
Published Tuesday, May 16, 2006 2:24 AM by dmilirud
A type has a type layout attribute, which specifies how the fields of an instance of a type are arranged. A type can have only one layout attribute.
There are 3 possible kinds of layout:
Auto – the layout is done by the CLI. This is the layout that ILASM uses if no layout attribute is specified by the user.
Explicit – the layout of the fields is explicitly provided. Note, that generic types cannot have explicit layout.
[ StructLayout( LayoutKind.Explicit )]
public class C
{
[ FieldOffset(0)] public int i;
[ FieldOffset(4)] public object o;
}
Sequential – The fields are laid out sequentially as they appear in the definition of the type.
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct S2
{
public int i1;
public char c1;
}
One of the questions I have seen asked with regard to layout is why in some cases using LayoutKind.Sequential or LayoutKind.Explicit causes a TypeLoadException to be thrown when the type is used.
The reason is that layout cannot start part way down the inheritance chain.
If a type is marked Sequential or Explicit, then it has layout. If the type is part of an inheritance chain, then all its parent classes must have layout as well, up to the type that directly extends System.Object or System.ValueType.
If somewhere along the inheritance chain a parent type has no layout, runtime will throw a TypeLoadException.
However, it is valid to stop using layout at any point down the chain.
Examples from ECMA spec:
In the diagrams below, Class A derives from System.Object; Class B derives from A; class C derives from B. System.Object has no layout. But A, B, and C are all defined with layout, and that is valid.
The situation with classes E, F, and G is similar - G has no layout, and this too is valid.
In the following two cases the inheritance chain is invalid. On the left, H doesn't have a layout and on the right there is a 'hole' in the inheritance chain, at L.
Published Tuesday, May 16, 2006 2:24 AM by dmilirud
相关文章推荐
- 在Entity Framework中使用存储过程(二):具有继承关系实体的存储过程如何定义?
- 【Hibernate框架开发之七】Hibernate使用Annotation中各种关系映射的CRUD(增删改查)&&集合映射&&继承映射
- Java继承关系中,父类方法使用实例变量和调用实例方法的探究
- log4j框架logger的继承关系以及使用场景
- 继承关系 在spring的bean注入中的使用
- 再谈C++中的has-a关系(通过复合塑模出has-a、明智而审慎地使用Private继承)
- Smack Connection 的继承关系和读写接口使用说明
- 写几个带继承关系的类,使用直接强转的方式,以及使用as转换, 并且练习is的类型判断
- log4j框架logger的继承关系以及使用场景
- 再谈C++中的has-a关系(通过复合塑模出has-a、明智而审慎地使用Private继承)
- ExtJs--11--Ext定义类时,使用混入的配置项,可实现多继承关系
- 使用新的控件时,要注意他的继承关系
- 使用webwork处理表单字段中的继承关系
- log4j框架logger的继承关系以及使用场景
- Java IO流类继承关系及使用说明
- android studio使用-查看类结构及继承关系
- java之finalize在继承关系中的使用
- Entity Framework -- 使用类别字段的实体继承关系
- 1.3 实体继承关系配置及使用EntityGen和Configurator[发布时间:9/8]
- 使用IntelliJ IDEA查看类的继承关系图形