Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / reflection / pointer.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 ////////////////////////////////////////////////////////////////////////////////
7 //
8 // This is a wrapper class for Pointers
9 // 
10 // <OWNER>Microsoft</OWNER>
11 //
12 // 
13 //  
14 //
15 namespace System.Reflection {
16     using System;
17     using CultureInfo = System.Globalization.CultureInfo;
18     using System.Runtime.Serialization;
19     using System.Security;
20     using System.Diagnostics.Contracts;
21
22     [CLSCompliant(false)]
23     [Serializable]
24     [System.Runtime.InteropServices.ComVisible(true)]
25     public sealed class Pointer: ISerializable {
26         [SecurityCritical]
27         unsafe private void* _ptr;
28         private RuntimeType _ptrType;
29
30         private Pointer() {}
31
32         [System.Security.SecurityCritical]  // auto-generated
33         private unsafe Pointer(SerializationInfo info, StreamingContext context)
34         {
35             _ptr = ((IntPtr)(info.GetValue("_ptr", typeof(IntPtr)))).ToPointer();
36             _ptrType = (RuntimeType)info.GetValue("_ptrType", typeof(RuntimeType));
37         }
38
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
41         //    during an Invoke.
42         [System.Security.SecurityCritical]  // auto-generated
43         public static unsafe Object Box(void *ptr,Type type) {
44             if (type == null)
45                 throw new ArgumentNullException("type");
46             if (!type.IsPointer)
47                 throw new ArgumentException(Environment.GetResourceString("Arg_MustBePointer"),"ptr");
48             Contract.EndContractBlock();
49
50             RuntimeType rt = type as RuntimeType;
51             if (rt == null)
52                 throw new ArgumentException(Environment.GetResourceString("Arg_MustBePointer"), "ptr");
53
54             Pointer x = new Pointer();
55             x._ptr = ptr;
56             x._ptrType = rt;
57             return x;
58         }
59
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;
66         }
67     
68         internal RuntimeType GetPointerType() {
69             return _ptrType;
70         }
71     
72         [System.Security.SecurityCritical]  // auto-generated
73         internal unsafe Object GetPointerValue() {
74             return (IntPtr)_ptr;
75         }
76
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);
82         }
83 #endif
84     }
85 }