Ejemplo n.º 1
0
        public static unsafe object?ToObject(TypedReference value)
        {
            TypeHandle typeHandle = new((void *)value._type);

            if (typeHandle.IsNull)
            {
                ThrowHelper.ThrowArgumentException_ArgumentNull_TypedRefType();
            }

            // The only case where a type handle here might be a type desc is when the type is either a
            // pointer or a function pointer. In those cases, just always return the method table pointer
            // for System.UIntPtr without inspecting the type desc any further. Otherwise, the type handle
            // is just wrapping a method table pointer, so return that directly with a reinterpret cast.
            MethodTable *pMethodTable = typeHandle.IsTypeDesc
                ? TypeHandle.TypeHandleOf <UIntPtr>().AsMethodTable()
                : typeHandle.AsMethodTable();

            Debug.Assert(pMethodTable is not null);

            object?result;

            if (pMethodTable->IsValueType)
            {
                result = RuntimeHelpers.Box(pMethodTable, ref value._value);
            }
            else
            {
                result = Unsafe.As <byte, object>(ref value._value);
            }

            return(result);
        }