5 // Miguel de Icaza (miguel@ximian.com)
8 // Michael Lambert, michaellambert@email.com
10 // (C) Ximian, Inc. http://www.ximian.com
12 // Remarks: Requires '/unsafe' compiler option. This class uses void*,
13 // in overloaded constructors, conversion, and cast members in
14 // the public interface. Using pointers is not valid CLS and
15 // the methods in question have been marked with the
16 // CLSCompliant attribute that avoid compiler warnings.
18 // FIXME: How do you specify a native int in C#? I am going to have to do some figuring out
22 using System.Runtime.Serialization;
27 assembly: System.CLSCompliant(true)
34 public unsafe struct IntPtr : ISerializable {
38 public static readonly IntPtr Zero;
42 Zero.value = (void *) 0;
45 public IntPtr (int i32)
50 public IntPtr (long i64)
58 unsafe public IntPtr (void *ptr)
63 private IntPtr (SerializationInfo info, StreamingContext context)
65 long savedValue = info.GetInt64 ("value");
66 value = (void *) savedValue;
69 public static int Size {
71 return sizeof (void *);
75 void ISerializable.GetObjectData (SerializationInfo si, StreamingContext sc)
78 throw new ArgumentNullException( "si" );
80 si.AddValue("value", (long) value);
83 public override bool Equals (object o)
85 if (!(o is System.IntPtr))
88 return ((IntPtr) o).value == value;
91 public override int GetHashCode ()
101 public long ToInt64 ()
109 unsafe public void *ToPointer ()
114 override public string ToString ()
117 return ((int) value).ToString ();
119 return ((long) value).ToString ();
122 public static bool operator == (IntPtr a, IntPtr b)
124 return (a.value == b.value);
127 public static bool operator != (IntPtr a, IntPtr b)
129 return (a.value != b.value);
132 public static explicit operator IntPtr (int value)
134 return new IntPtr (value);
137 public static explicit operator IntPtr (long value)
139 return new IntPtr (value);
145 unsafe public static explicit operator IntPtr (void *value)
147 return new IntPtr (value);
150 public static explicit operator int (IntPtr value)
152 return (int) value.value;
155 public static explicit operator long (IntPtr value)
157 return (long) value.value;
163 unsafe public static explicit operator void * (IntPtr value)