summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Reflection/Pointer.cs
blob: fd3b2dac64e3e719cd8c65aecf2aed605a260a10 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

////////////////////////////////////////////////////////////////////////////////
//
// This is a wrapper class for Pointers
// 
//
// 
//  
//
namespace System.Reflection {
    using System;
    using CultureInfo = System.Globalization.CultureInfo;
    using System.Runtime.Serialization;
    using System.Security;
    using System.Diagnostics.Contracts;

    [CLSCompliant(false)]
#if FEATURE_SERIALIZATION
    [Serializable]
#endif
    [System.Runtime.InteropServices.ComVisible(true)]
    public sealed class Pointer: ISerializable {
        [SecurityCritical]
        unsafe private void* _ptr;
        private RuntimeType _ptrType;

        private Pointer() {}

        [System.Security.SecurityCritical]  // auto-generated
        private unsafe Pointer(SerializationInfo info, StreamingContext context)
        {
            _ptr = ((IntPtr)(info.GetValue("_ptr", typeof(IntPtr)))).ToPointer();
            _ptrType = (RuntimeType)info.GetValue("_ptrType", typeof(RuntimeType));
        }

        // This method will box an pointer.  We save both the
        //    value and the type so we can access it from the native code
        //    during an Invoke.
        [System.Security.SecurityCritical]  // auto-generated
        public static unsafe Object Box(void *ptr,Type type) {
            if (type == null)
                throw new ArgumentNullException("type");
            if (!type.IsPointer)
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBePointer"),"ptr");
            Contract.EndContractBlock();

            RuntimeType rt = type as RuntimeType;
            if (rt == null)
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBePointer"), "ptr");

            Pointer x = new Pointer();
            x._ptr = ptr;
            x._ptrType = rt;
            return x;
        }

        // Returned the stored pointer.
        [System.Security.SecurityCritical]  // auto-generated
        public static unsafe void* Unbox(Object ptr) {
            if (!(ptr is Pointer))
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBePointer"),"ptr");
            return ((Pointer)ptr)._ptr;
        }
    
        internal RuntimeType GetPointerType() {
            return _ptrType;
        }
    
        [System.Security.SecurityCritical]  // auto-generated
        internal unsafe Object GetPointerValue() {
            return (IntPtr)_ptr;
        }

#if FEATURE_SERIALIZATION
        [System.Security.SecurityCritical]
        unsafe void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) {
            info.AddValue("_ptr", new IntPtr(_ptr));
            info.AddValue("_ptrType", _ptrType);
        }
#endif
    }
}