3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 ////////////////////////////////////////////////////////////////////////////////
8 // This is a wrapper class for Pointers
10 // <OWNER>Microsoft</OWNER>
15 namespace System.Reflection {
17 using CultureInfo = System.Globalization.CultureInfo;
18 using System.Runtime.Serialization;
19 using System.Security;
20 using System.Diagnostics.Contracts;
24 [System.Runtime.InteropServices.ComVisible(true)]
25 public sealed class Pointer: ISerializable {
27 unsafe private void* _ptr;
28 private RuntimeType _ptrType;
32 [System.Security.SecurityCritical] // auto-generated
33 private unsafe Pointer(SerializationInfo info, StreamingContext context)
35 _ptr = ((IntPtr)(info.GetValue("_ptr", typeof(IntPtr)))).ToPointer();
36 _ptrType = (RuntimeType)info.GetValue("_ptrType", typeof(RuntimeType));
39 // This method will box an pointer. We save both the
40 // value and the type so we can access it from the native code
42 [System.Security.SecurityCritical] // auto-generated
43 public static unsafe Object Box(void *ptr,Type type) {
45 throw new ArgumentNullException("type");
47 throw new ArgumentException(Environment.GetResourceString("Arg_MustBePointer"),"ptr");
48 Contract.EndContractBlock();
50 RuntimeType rt = type as RuntimeType;
52 throw new ArgumentException(Environment.GetResourceString("Arg_MustBePointer"), "ptr");
54 Pointer x = new Pointer();
60 // Returned the stored pointer.
61 [System.Security.SecurityCritical] // auto-generated
62 public static unsafe void* Unbox(Object ptr) {
63 if (!(ptr is Pointer))
64 throw new ArgumentException(Environment.GetResourceString("Arg_MustBePointer"),"ptr");
65 return ((Pointer)ptr)._ptr;
68 internal RuntimeType GetPointerType() {
72 [System.Security.SecurityCritical] // auto-generated
73 internal unsafe Object GetPointerValue() {
77 #if FEATURE_SERIALIZATION
78 [System.Security.SecurityCritical]
79 unsafe void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) {
80 info.AddValue("_ptr", new IntPtr(_ptr));
81 info.AddValue("_ptrType", _ptrType);