The Labs \ Source Viewer \ SSCLI \ System.Reflection \ Pointer

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. ////////////////////////////////////////////////////////////////////////////////
  16. //
  17. // This is a wrapper class for Pointers
  18. //
  19. //
  20. //
  21. //
  22. namespace System.Reflection
  23. {
  24.     using System;
  25.     using CultureInfo = System.Globalization.CultureInfo;
  26.     using System.Runtime.Serialization;
  27.    
  28.     [CLSCompliant(false)]
  29.     [Serializable()]
  30.     [System.Runtime.InteropServices.ComVisible(true)]
  31.     public sealed class Pointer : ISerializable
  32.     {
  33.         unsafe private void* _ptr;
  34.         private Type _ptrType;
  35.        
  36.         private Pointer()
  37.         {
  38.         }
  39.        
  40.         unsafe private Pointer(SerializationInfo info, StreamingContext context)
  41.         {
  42.             _ptr = ((IntPtr)(info.GetValue("_ptr", typeof(IntPtr)))).ToPointer();
  43.             _ptrType = (Type)info.GetValue("_ptrType", typeof(Type));
  44.         }
  45.        
  46.         // This method will box an pointer. We save both the
  47.         // value and the type so we can access it from the native code
  48.         // during an Invoke.
  49.         unsafe public static object Box(void* ptr, Type type)
  50.         {
  51.             if (type == null)
  52.                 throw new ArgumentNullException("type");
  53.             if (!type.IsPointer)
  54.                 throw new ArgumentException(Environment.GetResourceString("Arg_MustBePointer"), "ptr");
  55.            
  56.             Pointer x = new Pointer();
  57.             x._ptr = ptr;
  58.             x._ptrType = type;
  59.             return x;
  60.         }
  61.        
  62.         // Returned the stored pointer.
  63.         unsafe public static void* Unbox(object ptr)
  64.         {
  65.             if (!(ptr is Pointer))
  66.                 throw new ArgumentException(Environment.GetResourceString("Arg_MustBePointer"), "ptr");
  67.             return ((Pointer)ptr)._ptr;
  68.         }
  69.        
  70.         internal Type GetPointerType()
  71.         {
  72.             return _ptrType;
  73.         }
  74.        
  75.         unsafe internal object GetPointerValue()
  76.         {
  77.             return (IntPtr)_ptr;
  78.         }
  79.        
  80.        
  81.        
  82.         unsafe void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  83.         {
  84.             info.AddValue("_ptr", new IntPtr(_ptr));
  85.             info.AddValue("_ptrType", _ptrType);
  86.         }
  87.     }
  88. }

Developer Fusion