Move Pointer.cs to the shared partition. (dotnet/coreclr#10499)
authorAtsushi Kanamori <AtsushiKan@users.noreply.github.com>
Mon, 27 Mar 2017 18:09:21 +0000 (11:09 -0700)
committerGitHub <noreply@github.com>
Mon, 27 Mar 2017 18:09:21 +0000 (11:09 -0700)
* Move Pointer.cs to the shared partition.

(The two internal helpers are not yet used
on CoreRT but that's only because CoreRT
doesn't yet implement Pointer support
for Reflection invoke. Exposing them
unconditionally now to save us the trouble
of removing the #ifdef's later.)

Commit migrated from https://github.com/dotnet/coreclr/commit/47d333c856e567884594e1efd3497a254b6a04fb

src/coreclr/src/mscorlib/System.Private.CoreLib.csproj
src/coreclr/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems
src/coreclr/src/mscorlib/shared/System/Reflection/Pointer.cs [new file with mode: 0644]
src/coreclr/src/mscorlib/src/System/Reflection/Pointer.cs [deleted file]
src/coreclr/src/mscorlib/src/System/RtType.cs

index 928a344..dba0fbd 100644 (file)
     <Compile Include="$(BclSourcesRoot)\System\Reflection\MemberSerializationStringGenerator.cs" />
     <Compile Include="$(BclSourcesRoot)\System\Reflection\MethodBase.CoreCLR.cs" />
     <Compile Include="$(BclSourcesRoot)\System\Reflection\MethodBody.cs" />
-    <Compile Include="$(BclSourcesRoot)\System\Reflection\Pointer.cs" />
     <Compile Include="$(BclSourcesRoot)\System\Reflection\RtFieldInfo.cs" />
     <Compile Include="$(BclSourcesRoot)\System\Reflection\RuntimeAssembly.cs" />
     <Compile Include="$(BclSourcesRoot)\System\Reflection\RuntimeConstructorInfo.cs" />
index e8d180d..98a5827 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)System\Reflection\ParameterAttributes.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Reflection\ParameterInfo.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Reflection\ParameterModifier.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Pointer.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Reflection\PortableExecutableKinds.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Reflection\ProcessorArchitecture.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Reflection\PropertyAttributes.cs" />
diff --git a/src/coreclr/src/mscorlib/shared/System/Reflection/Pointer.cs b/src/coreclr/src/mscorlib/shared/System/Reflection/Pointer.cs
new file mode 100644 (file)
index 0000000..13a5eff
--- /dev/null
@@ -0,0 +1,61 @@
+// 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.
+
+using System.Diagnostics;
+using System.Runtime.Serialization;
+
+namespace System.Reflection
+{
+    [Serializable]
+    [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 Type _ptrType;
+
+        private Pointer(void* ptr, Type ptrType)
+        {
+            Debug.Assert(ptrType.IsRuntimeImplemented()); // CoreCLR: For CoreRT's sake, _ptrType has to be declared as "Type", but in fact, it is always a RuntimeType. Code on CoreCLR expects this.
+            _ptr = ptr;
+            _ptrType = ptrType;
+        }
+
+        private Pointer(SerializationInfo info, StreamingContext context)
+        {
+            _ptr = ((IntPtr)(info.GetValue("_ptr", typeof(IntPtr)))).ToPointer();
+            _ptrType = (Type)info.GetValue("_ptrType", typeof(Type));
+            if (!_ptrType.IsRuntimeImplemented())
+                throw new SerializationException(SR.Arg_MustBeType);
+        }
+
+        public static object Box(void* ptr, Type type)
+        {
+            if (type == null)
+                throw new ArgumentNullException(nameof(type));
+            if (!type.IsPointer)
+                throw new ArgumentException(SR.Arg_MustBePointer, nameof(ptr));
+            if (!type.IsRuntimeImplemented())
+                throw new ArgumentException(SR.Arg_MustBeType, nameof(ptr));
+
+            return new Pointer(ptr, type);
+        }
+
+        public static void* Unbox(object ptr)
+        {
+            if (!(ptr is Pointer))
+                throw new ArgumentException(SR.Arg_MustBePointer, nameof(ptr));
+            return ((Pointer)ptr)._ptr;
+        }
+
+        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
+        {
+            info.AddValue("_ptr", new IntPtr(_ptr));
+            info.AddValue("_ptrType", _ptrType);
+        }
+
+        internal Type GetPointerType() => _ptrType;
+        internal object GetPointerValue() => (IntPtr)_ptr;
+    }
+}
diff --git a/src/coreclr/src/mscorlib/src/System/Reflection/Pointer.cs b/src/coreclr/src/mscorlib/src/System/Reflection/Pointer.cs
deleted file mode 100644 (file)
index 600676a..0000000
+++ /dev/null
@@ -1,82 +0,0 @@
-// 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)]
-    [Serializable]
-    public sealed class Pointer : ISerializable
-    {
-        unsafe private void* _ptr;
-        private RuntimeType _ptrType;
-
-        private Pointer() { }
-
-        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.
-        public static unsafe Object Box(void* ptr, Type type)
-        {
-            if (type == null)
-                throw new ArgumentNullException(nameof(type));
-            if (!type.IsPointer)
-                throw new ArgumentException(SR.Arg_MustBePointer, nameof(ptr));
-            Contract.EndContractBlock();
-
-            RuntimeType rt = type as RuntimeType;
-            if (rt == null)
-                throw new ArgumentException(SR.Arg_MustBePointer, nameof(ptr));
-
-            Pointer x = new Pointer();
-            x._ptr = ptr;
-            x._ptrType = rt;
-            return x;
-        }
-
-        // Returned the stored pointer.
-        public static unsafe void* Unbox(Object ptr)
-        {
-            if (!(ptr is Pointer))
-                throw new ArgumentException(SR.Arg_MustBePointer, nameof(ptr));
-            return ((Pointer)ptr)._ptr;
-        }
-
-        internal RuntimeType GetPointerType()
-        {
-            return _ptrType;
-        }
-
-        internal unsafe Object GetPointerValue()
-        {
-            return (IntPtr)_ptr;
-        }
-
-        unsafe void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
-        {
-            info.AddValue("_ptr", new IntPtr(_ptr));
-            info.AddValue("_ptrType", _ptrType);
-        }
-    }
-}
index 826bbea..678e46f 100644 (file)
@@ -3881,7 +3881,7 @@ namespace System
                 RuntimeType valueType;
                 Pointer pointer = value as Pointer;
                 if (pointer != null)
-                    valueType = pointer.GetPointerType();
+                    valueType = (RuntimeType)pointer.GetPointerType();
                 else
                     valueType = (RuntimeType)value.GetType();
 
@@ -3922,7 +3922,7 @@ namespace System
                     RuntimeType valueType;
                     Pointer pointer = value as Pointer;
                     if (pointer != null)
-                        valueType = pointer.GetPointerType();
+                        valueType = (RuntimeType)pointer.GetPointerType();
                     else
                         valueType = (RuntimeType)value.GetType();