如何在C#中模拟C++的联合(Union)?[C#, C++] How To Simulate C++ Union In C#?

1 什么是联合?

联合(Union)是一种特殊的类,一个联合中的数据成员在内存中的存储是互相重叠的。每个数据成员都在相同的内存地址开始。分配给联合的存储区数量是“要包含它最大的数据成员”所需的内存数。同一时刻只有一个成员可以被赋给一个值。

下面我们来看看C++中如何表达联合:

如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?// Code #01
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?union TokenValue
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?{
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    char _cval;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    int _ival;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    double _dval;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?};

2 联合的内存布局与内存使用情况。

下面我们来考察一下TokenValue的内存布局。

首先,我们使用sizeof运算符来获取该联合各个成员的内存占用字节数:

如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?// Code #02
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?int _tmain(int argc, _TCHAR* argv[])
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?{
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    cout << "sizeof(char): " << sizeof(char) << endl;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    cout << "sizeof(int): " << sizeof(int) << endl;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    cout << "sizeof(double): " << sizeof(double) << endl;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    return 0;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?}
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?/*
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#? * Output:
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#? * sizeof(char): 1
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#? * sizeof(int): 4
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#? * sizeof(double): 8
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#? *
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#? */

这样,分配给该联合的内存就是8个字节。

接着,我们来看看具体使用该联合时,所分配的内存的字节占用情况如何:

如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?// Code #03
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?int _tmain(int argc, _TCHAR* argv[])
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?{
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    TokenValue tv;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    // [_][_][_][_][_][_][_][_]
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    tv._cval = 'K';
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    // [X][_][_][_][_][_][_][_]
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    tv._ival = 1412;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    // [X][X][X][X][_][_][_][_]
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    tv._dval = 3.14159;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    // [X][X][X][X][X][X][X][X]
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    return 0;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?}

3 第一次尝试:在C#中模拟这种布局方式。

在C#中,要指定成员的内存布局情况,我们需要结合使用StructLayoutAttribute特性、LayoutKind枚举和FieldOffsetAttribute特性,它们都位于System.Runtime.InteropServices命名空间中。

下面我用struct来试着模拟上面的TokenValue联合:

如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?// Code #04
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?[StructLayout(LayoutKind.Explicit, Size=8)]
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?struct TokenValue
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?{
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    [FieldOffset(0)]
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    public char _cval;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    [FieldOffset(0)]
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    public int _ival;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    [FieldOffset(0)]
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    public double _dval;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?}

我们知道,联合的每个数据成员都在相同的内存地址开始,通过把[FieldOffset(0)]应用到TokenValue的每一个成员,我们就指
定了这些成员都处于同一起始位置。当然,我们得事先告诉.NET这些成员的内存布局由我们来作主,把LayoutKind.Explicit枚举传递给
StructLayoutAttribute特性的构造函数,并应用到TokenValue,.NET就不会再干涉该struct的成员在内存中的布局
了。另外,我显式的把TokenValue的大小设置为8字节,当然,这样做是可选的。

4 在实际的C++代码中,我们是如何使用联合的?

在实际的C++代码中,我们应尽量避免让客户端直接使用联合,Code
#03就是一个很好的反面例子了。为什么呢?熟悉C/C++的开发人员都知道,联合提供我们这样一个节省空间的储存方式,是要我们付出一定的代价的。这个
代价就是代码的安全性,不恰当地使用联合可能会导致程序崩溃的。

由于每一次只有一个联合成员处于激活状态,如果我们不小心或者因为其它原因使用处于休眠状态的成员,轻则得到错误的结果,重则整个程序中止。请看下面的代码:

如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?// Code #05
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?union TokenValue
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?{
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    char _cval;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    int _ival;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    double _dval;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    char* _sval;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?};
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?int _tmain(int argc, _TCHAR* argv[])
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?{
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    TokenValue tv;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    tv._cval = 'K';
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    cout << tv._cval << endl;    // Line #01
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    cout << tv._ival << endl;    // Line #02
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    cout << tv._dval << endl;    // Line #03
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    cout << tv._sval << endl;    // Line #04
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    return 0;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?}

这里的TokenValue比起Code #01的仅仅多了一个_sval,它是C风格的字符串,实质上,它是指向字符串的第一个字符的指针,它占用4字节的内存空间。

当程序运行到Line
#04时,就会出现Unhandled
Exception,程序中止,并指出_sval的值非法(即所谓的“野指针”)。程序无法把它的值输出控制台,然而,Line #01 ~ Line
#03都能输出,只是Line #02和Line #03所输出的值是错误的而已。

实际的应用中,我们一般不会看到如此低级且显而易见的错误,但复杂的实际应用中,不恰当地使用联合的确会为我们带来不少的麻烦。

5 第二次尝试:改进型的联合模拟。

一般情况下,联合作为一种内部数据的储存手段,没有必要让客户端对其有所了解,更没必要让客户端直接使用它。为了使我们的联合模拟用起来更安全,我们需要对它进行一番包装:

如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?// Code #06
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?class Program
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?{
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    static void Main(string[] args)
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    {
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        Token t = new Token();
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        Console.WriteLine(t);
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        Console.WriteLine(t.GetTokenValue());
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        t.SetTokenValue('K');
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        Console.WriteLine(t);
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        Console.WriteLine(t.GetTokenValue());
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    }
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?}
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?public struct Token
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?{
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    private TokenValue tv;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    private TokenKind tk;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    public void SetTokenValue(char c)
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    {
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        tk = TokenKind.CharValue;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        tv._cval = c;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    }
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    public void SetTokenValue(int i)
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    {
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        tk = TokenKind.IntValue;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        tv._ival = i;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    }
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    public void SetTokenValue(double d)
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    {
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        tk = TokenKind.DoubleValue;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        tv._dval = d;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    }
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    public object GetTokenValue()
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    {
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        switch (tk)
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        {
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?            case TokenKind.CharValue:
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?                return tv._cval;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?            case TokenKind.IntValue:
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?                return tv._ival;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?            case TokenKind.DoubleValue:
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?                return tv._dval;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?            default:
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?                return "NoValue";
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        }
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    }
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    public override string ToString()
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    {
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        switch (tk)
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        {
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?            case TokenKind.CharValue:
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?                return tv._cval.ToString();
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?            case TokenKind.IntValue:
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?                return tv._ival.ToString();
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?            case TokenKind.DoubleValue:
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?                return tv._dval.ToString();
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?            default:
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?                return "NoValue";
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        }
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    }
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    [StructLayout(LayoutKind.Explicit, Size = 8)]
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    private struct TokenValue
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    {
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        [FieldOffset(0)]public char _cval;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        [FieldOffset(0)]public int _ival;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        [FieldOffset(0)]public double _dval;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    }
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    private enum TokenKind
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    {
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        NoValue,
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        CharValue,
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        IntValue,
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?        DoubleValue
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    }
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?}
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?/* 
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#? * Output:
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#? * NoValue
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#? * NoValue
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#? * K
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#? * K
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#? *
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#? */

由于Token是值类型,实例化时,对应的成员(tv和tk)会自动被赋予与之对应的零值。此时,tv._cval为'\0'、tv._ival和tv._dval均为0(实质上它们是同一个值在不同的类型中的表现)。而tk也被自动赋予0:

tk = 0;

这里,你无需进行强类型转换,0是任何枚举的默认初始值,.NET会负责把0转换成对应的枚举类型。例如,你可以:

如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?// Code #07
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?System.DayOfWeek d = 0;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?Console.WriteLine(d);

该代码能正确输出Sunday——一个星期的第一天(西方习惯),也是该枚举的第一个成员。

一般情况下,0对应着枚举的第一个成员(除非你在定义枚举的时候,把第一个成员指定为别的值,并为别的成员赋予0值)。这样,我们就不难看出代码的输出是合理的,而且代码本身也是安全的。

6 别在模拟的联合中同时使用值类型和引用类型!

到目前为止,我们所模拟的联合中,所有的成员都是值类型,如果我们为它加入一个引用类型,例如String呢?

如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?// Code #08
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?[StructLayout(LayoutKind.Explicit, Size=8)]
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?struct TokenValue
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?{
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    [FieldOffset(0)]
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    public char _cval;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    [FieldOffset(0)]
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    public int _ival;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    [FieldOffset(0)]
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    public double _dval;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    [FieldOffset(0)]
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?    public string _sval;
如何在C#中模拟C++的联合(Union)?[C#, C++]  How To Simulate C++ Union In C#?}

这样,Code #06的代码运行时就会提示出错:

Could
not load type 'TokenValue' from assembly 'UnionLab,
Version=1.0.1820.28531, Culture=neutral, PublicKeyToken=null' because it
contains an object field at offset 0 that is incorrectly aligned or
overlapped by a non-object field.

TokenValue初始化的时候,_cval、_ival和_dval都能正确的被赋予对应的零值,而这些零值也能被统一起来(别的值就不行
了)。但_sval不同,它是引用类型,如果没有显示初始化为某个有意义的值,它将被赋予null值!这个null值跟之前的有意义的零值是不能被统一起
来的!所以,要么你就去掉这个_sval,要么就重新定义它的起始位置(当然,你也得去掉Size=8!),但这样一来,TokenValue就不再称得
上联合的模拟了。

在C++中,我们可以直接使用指针来解决这个问题,如Code #05,但C#中,问题就会变得有点辣手。如果你有兴趣的话,可以使用不安全代码(Unsafe code)来试着解决,但这样一来,你的代码又会引入一些新的问题。

7 为什么要在C#里面模拟这个用处不大的东西?[NEW]

相信很多人都有这样一个疑问:为什么要在C#里面模拟这个
用处不大的东西?就我个人来说,我始终坚信事物的存在必定有它的理由,否则就不会存在。其实,联合在我们平时的编码中的确很少用到,但在某些情况下,我们
必须使用它!.NET为我们提供巨大的便利的同时,也不忘让我们能够与非托管代码交互。你知道,早期的Win32
API使用C来完成的,这里面就有很多函数的参数是以联合的形式表达的,要在C#中跟这些API交互,我们就得“尊重”原函数的用法约束。

8 终点与起点的交界处。

回顾整个探索旅程,我们为了使用联合节省空间的优势,开始了这个模拟的探索,然而,为了弥补联合的不足,我们对这个模拟进行了一番包装,增加了不少
额外的代码,直到后来,又发现了在这个模拟中同时使用值类型的成员和引用类型的成员所引发的问题,我们一直都没有停止过探索和思考。正如马斯洛的需要层次
理论所描述的,人只要低层次的需要被满足,马上就会转向更高的需要层次,一级一级的,直到攀上最高峰为止。

关于在C#中模拟C++的联合这个话题,我并没有在本文中给予你一个完整的展示,相反,我为你展示的仅仅是一个探索的起点,希望为你带来一丝灵感,让你根据自己的实际情况来定制你的探索旅程。Have a good trip!


参考资料:

上一篇:Android测试跑单个包脚本文件


下一篇:iOS 通过tag查找控件