RuntimeHelpers.GetObjectValue(Object) 方法    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将值类型装箱。
public:
 static System::Object ^ GetObjectValue(System::Object ^ obj);public static object GetObjectValue(object obj);public static object? GetObjectValue(object? obj);static member GetObjectValue : obj -> objPublic Shared Function GetObjectValue (obj As Object) As Object参数
- obj
- Object
要装箱的值类型。
返回
如果 obj 是一个值类,则返回其装箱的副本;否则返回 obj 本身。
示例
下面的示例演示如何使用 GetObjectValue 方法将值类装箱。
using System;
using System.Runtime.CompilerServices;
// Declare a value type.
struct Point2I
{
    public int x;
    public int y;
}
class Program
{
    static void Main(string[] args)
    {
        // Allocate an unboxed Point2I (not on the heap).
        Point2I pnt;
        pnt.x = 0;
        pnt.y = 0;
        // Box the value.  (Put it in the heap.)
        object objPntr = RuntimeHelpers.GetObjectValue(pnt);
    }
}
Imports System.Runtime.CompilerServices
' Declare a value type.
Structure Point2I
    Dim x As Integer
    Dim y As Integer
End Structure
Module Program
    Sub Main(ByVal args() As String)
        ' Allocate an unboxed Point2I (not on the heap).
        Dim pnt As Point2I
        pnt.x = 0
        pnt.y = 0
        ' Box the value.  (Put it in the heap.)
        Dim objPntr As Object = RuntimeHelpers.GetObjectValue(pnt)
    End Sub
End Module
注解
将值类型装箱会创建一个 对象,并将指定值类型的字段浅表复制到新对象中。
此方法允许将值类作为对象进行操作,同时保留值类的别名行为。
返回值取决于值类是可变的还是不可变的:
- 如果赋值是可变值类,该方法将返回类的浅表副本,因为值类具有复制语义。 
- 如果要分配的值是一个不可变值类,该方法将返回对象本身,而不是类的副本。 
动态类型化语言的编译器可以使用此方法来确保装箱值类型的工作方式与未装箱值类型相同。 也就是说,当你传递值类型时,会克隆这些值类型,并且它们始终按值传递。 编译器可以调用 GetObjectValue 以将值类型分配给对象,或将值类型作为类型对象的参数传递。
编译器使用此方法。