• .NET的基元类型包括哪些?Unmanaged和Blittable类型又是什么?


    在讨论.NET的类型系统的时候,我们经常提到“基元类型(Primitive Type)”的概念,我发现很多人并没有真正理解基元类型就究竟包含哪些(比如很多人觉得字符串是基元类型)。除了明确界定基元类型外,本篇文章还会简单介绍额外两种关于类型的概念——Unmanaged类型和Blittable类型。

    一、Primitive Type
    二、Unmanaged Type
    三、Blittable Type

    一、Primitive Type

    .NET下的基元类型(Primitive Type)如下14个。我们可以这样来记:长度(字节数)分别为1、2、4、8的有/无符号的整数;外加两个基于指针宽度(下x86=4; x64=8)的整数,计10个。长度(字节数)分别为4和8的单精度和双精度浮点数,计2个。外加布尔类型和字符类型, 计2个。所以我们熟悉的String(string)和Decimal(decimal)并不是基元类型。

    • 整数(10):Byte(byte)/SByte(sbyte), Int16(short)/UInt16(ushort), Int32(int)/UInt32(uint), Int64(long)/UInt64(ulong), IntPtr(nint)/UIntPtr(nuint)
    • 浮点(2):Float(float), Double(double)
    • 布尔(1):Boolean(bool)
    • 字符(1):Char(char)

    对于某个指定的Type对象,我们可以利用它的IsPrimitive属性确定它是否为基元类型。

    public abstract class Type
    {
        public bool IsPrimitive { get; }
    }
    

    Type对象的IsPrimitive属性值最终来源于RuntimeTypeHandle类型如下这个内部静态方法IsPrimitive。从该方法的实现和CorElementType的枚举成员也可以看出,枚举值2-13,外加CorElementType.I(IntPtr)和CorElementType.U(UIntPtr)这14个类型属于基元类型的范畴,这与上面的列表是一致的。

    复制代码
    public struct RuntimeTypeHandle
    {
        [SecuritySafeCritical]
        internal static bool IsPrimitive(RuntimeType type)
        {
            CorElementType corElementType = GetCorElementType(type);
            if (((int)corElementType < 2 || (int)corElementType > 13) && corElementType != CorElementType.I)
            {
                return corElementType == CorElementType.U;
            }
            return true;
        }
    }
    
    [Serializable]
    internal enum CorElementType : byte
    {
        End = 0,
        Void = 1,
        Boolean = 2,
        Char = 3,
        I1 = 4,
        U1 = 5,
        I2 = 6,
        U2 = 7,
        I4 = 8,
        U4 = 9,
        I8 = 10,
        U8 = 11,
        R4 = 12,
        R8 = 13,
        String = 14,
        Ptr = 15,
        ByRef = 16,
        ValueType = 17,
        Class = 18,
        Var = 19,
        Array = 20,
        GenericInst = 21,
        TypedByRef = 22,
        I = 24,
        U = 25,
        FnPtr = 27,
        Object = 28,
        SzArray = 29,
        MVar = 30,
        CModReqd = 31,
        CModOpt = 32,
        Internal = 33,
        Max = 34,
        Modifier = 64,
        Sentinel = 65,
        Pinned = 69
    }
    
    复制代码

    二、Unmanaged Type

    顾名思义,Unmanaged类型可以理解不涉及托管对象引用的值类型。如下的类型属于Unmanaged 类型的范畴:

    • 14种基元类型+Decimal(decimal)

    • 枚举类型

    • 指针类型(比如int*, long*)

    • 只包含Unmanaged类型字段的结构体

    如果要求泛型类型是一个Unmananged类型,我们可以按照如下的方式使用unmanaged泛型约束。我在《如何计算一个实例占用多少内存?》提到过,只有Unmananged类型采用使用sizeof操作符计算大小。

    public static unsafe int SizeOf() where T : unmanaged
    {
        return sizeof(T);
    }
    

    三、Blittable Type

    Blittable是站在基于P/Invoke的互操作(InterOp)角度对传递的值是否需要进行转换(Marshaling)而作的分类。Blittable类型要求在托管内存和非托管内存具有完全一致的表示。如果某个参数为Blittable类型,在一个P/Invoke方法调用非托管方法的时候,该参数就无需要作任何的转换。与之类似,如果调用方法的返回值是Blittable类型,在回到托管世界后也无需转换。如下的类型属于Blittable类型范畴:

    • Boolean(bool)Char(char)之外的12种基元类型,因为布尔值True在不同的平台可能会表示成1或者-1,对应的字节数可能是1、2或者4,字符涉及不同的编码(Unicode和ANSI),所以这两种类型并非Blittable类型;
    • Blittable基元类型的一维数组;
    • 采用SequentialExplicitly布局的且只包含Blittable类型成员的结构或者类,因为采用这两种布局的对象最终会按照一种确定的格式转换成对应的C风格的结构体。如果采用Auto布局,CLR会按照少占用内存的原则对字段成员重新排序,意味着其内存结构是不确定的。

    顺便强调一下,DateTime/DateTimeOffset都采用Auto布局(如下所示),Guid虽然是一个默认采用Sequential布局的结构体,但是最终映射在内存种的字节依赖于字节序(Endianness),所以具有这三种类型字段的结构体或者类都不是Blittable类型。

    [Serializable]
    [StructLayout(LayoutKind.Auto)]
    public struct DateTime
    { }
    
    [Serializable]
    [StructLayout(LayoutKind.Auto)]
    public struct DateTimeOffset
    { }
    

    只有Blittable类型的实例才能调用GCHandle的静态方法Alloc为其创建一个Pinned类型的GC句柄。以如下的代码为例,类Foobar的两个属性都是Blittable类型,我们通过标注在类型上的StructLayoutAttribute将布局类型显式设置成Sequential使其称为了一个Blittable类型。

    GCHandle.Alloc(new Foobar(), GCHandleType.Pinned);
    
    [StructLayout(LayoutKind.Sequential)]
    public class Foobar
    {
        public int Foo { get; set; }
        public double Bar { get; set; }
    }
    

    如果Foobar类定义成如下的形式,都不能使其称为一个Blittable类型。前者默认采用Auto布局,后者的Bar属性并不是Blittable类型。如果将这样Foobar对象作为参数按照上面的方式调用GCHandle. Alloc方法,会直接抛出ArgumentException异常,并提示“Object contains non-primitive or non-blittable data. (Parameter 'value')”。

    复制代码
    public class Foobar
    {
        public int Foo { get; set; }
        public double Bar { get; set; }
    }
    
    [StructLayout(LayoutKind.Sequential)]
    public class Foobar
    {
        public int Foo { get; set; }
        public DateTime Bar { get; set; }
    }
    
    复制代码
  • 相关阅读:
    从零开始将图片信息和空间信息绑定,并在前端展示到地图
    如何对 ABAP 数据库表通过 ABAP 代码进行更新和删除操作试读版
    nRF5340(入门篇)之1.0 window下开发环境搭建
    接口的安全设计要素:ticket,签名,时间戳
    TYWZOJ 礼品配对包装 题解
    【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
    排序——归并排序
    SQL Server教程 - SQL Server介绍
    k8s-Kuboard国产页面管理
    你应该知道的Redis事务
  • 原文地址:https://www.cnblogs.com/artech/p/basic-types.html