/
Pointer.cs
62 lines (50 loc) · 1.89 KB
/
Pointer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Diagnostics.CodeAnalysis;
namespace System.Reflection
{
[CLSCompliant(false)]
public sealed unsafe class Pointer : ISerializable
{
// CoreCLR: Do not add or remove fields without updating the ReflectionPointer class in runtimehandles.h
private readonly void* _ptr;
private readonly RuntimeType _ptrType;
private Pointer(void* ptr, RuntimeType ptrType)
{
_ptr = ptr;
_ptrType = ptrType;
}
public static object Box(void* ptr, Type type)
{
ArgumentNullException.ThrowIfNull(type);
if (!type.IsPointer)
throw new ArgumentException(SR.Arg_MustBePointer, nameof(ptr));
if (type is not RuntimeType rtType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(type));
return new Pointer(ptr, rtType);
}
public static void* Unbox(object ptr)
{
if (!(ptr is Pointer))
throw new ArgumentException(SR.Arg_MustBePointer, nameof(ptr));
return ((Pointer)ptr)._ptr;
}
public override unsafe bool Equals([NotNullWhen(true)] object? obj)
{
if (obj is Pointer pointer)
{
return _ptr == pointer._ptr;
}
return false;
}
public override unsafe int GetHashCode() => ((nuint)_ptr).GetHashCode();
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
throw new PlatformNotSupportedException();
}
internal RuntimeType GetPointerType() => _ptrType;
internal IntPtr GetPointerValue() => (IntPtr)_ptr;
}
}