.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<T>() where T : unmanaged{ return sizeof(T);}三、Blittable TypeBlittable是站在基于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基元類型的一維數組;采用Sequential和Explicitly布局的且只包含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; }}到此這篇關于.NET的基元類型包括什么及Unmanaged和Blittable類型詳解的文章就介紹到這了,更多相關.NET的基元類型內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
