Fix warnings in mscorlib's Test suite + bring a couple more tests + fix thread rename...
[mono.git] / mcs / class / corlib / System.Runtime.InteropServices / Marshal.cs
index 1f7530a6d0426386835e512b9bd0ba6da1963a02..e339f94955ad4463ee6de55a20d018c6b0157cd2 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using Mono.Interop;
 using System.Collections;
+using System.Collections.Generic;
 using System.Runtime.CompilerServices;
 using System;
 using System.Security;
 using System.Reflection;
 using System.Threading;
 
-#if NET_2_0
 using System.Runtime.ConstrainedExecution;
+#if !FULL_AOT_RUNTIME
 using System.Runtime.InteropServices.ComTypes;
+using Mono.Interop;
 #endif
 
 namespace System.Runtime.InteropServices
 {
-       [SuppressUnmanagedCodeSecurity ()]
-       public
-#if NET_2_0
-       static
-#else
-       sealed
-#endif
-       class Marshal
+       public static class Marshal
        {
                /* fields */
                public static readonly int SystemMaxDBCSCharSize = 2; // don't know what this is
-               public static readonly int SystemDefaultCharSize;
-
-               static Marshal ()
-               {
-                       SystemDefaultCharSize = Environment.OSVersion.Platform == PlatformID.Win32NT ? 2 : 1;
-               }
-
-#if !NET_2_0
-               private Marshal () {}
-#endif
+               public static readonly int SystemDefaultCharSize = Environment.IsRunningOnWindows ? 2 : 1;
 
+#if !MOBILE
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern static int AddRefInternal (IntPtr pUnk);
+#endif
 
                public static int AddRef (IntPtr pUnk)
                {
+#if !MOBILE
                        if (pUnk == IntPtr.Zero)
                                throw new ArgumentException ("Value cannot be null.", "pUnk");
                        return AddRefInternal (pUnk);
+#else
+                       throw new NotImplementedException ();
+#endif
                }
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr AllocCoTaskMem (int cb);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
-#endif
                public extern static IntPtr AllocHGlobal (IntPtr cb);
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
-#endif
                public static IntPtr AllocHGlobal (int cb)
                {
                        return AllocHGlobal ((IntPtr)cb);
@@ -107,12 +94,12 @@ namespace System.Runtime.InteropServices
                }
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               extern static void copy_to_unmanaged (Array source, int startIndex,
-                                                     IntPtr destination, int length);
+               internal extern static void copy_to_unmanaged (Array source, int startIndex,
+                                                              IntPtr destination, int length);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               extern static void copy_from_unmanaged (IntPtr source, int startIndex,
-                                                       Array destination, int length);
+               internal extern static void copy_from_unmanaged (IntPtr source, int startIndex,
+                                                                Array destination, int length);
 
                public static void Copy (byte[] source, int startIndex, IntPtr destination, int length)
                {
@@ -149,12 +136,10 @@ namespace System.Runtime.InteropServices
                        copy_to_unmanaged (source, startIndex, destination, length);
                }
 
-#if NET_2_0
                public static void Copy (IntPtr[] source, int startIndex, IntPtr destination, int length)
                {
                        copy_to_unmanaged (source, startIndex, destination, length);
                }
-#endif
 
                public static void Copy (IntPtr source, byte[] destination, int startIndex, int length)
                {
@@ -191,7 +176,6 @@ namespace System.Runtime.InteropServices
                        copy_from_unmanaged (source, startIndex, destination, length);
                }
 
-#if NET_2_0
                public static void Copy (IntPtr source, IntPtr[] destination, int startIndex, int length)
                {
                        copy_from_unmanaged (source, startIndex, destination, length);
@@ -202,8 +186,14 @@ namespace System.Runtime.InteropServices
                {
                        throw new NotImplementedException ();
                }
+
+#if NET_4_5
+               public static IntPtr CreateAggregatedObject<T> (IntPtr pOuter, T o) {
+                       return CreateAggregatedObject (pOuter, (object)o);
+               }
 #endif
-               
+
+#if !FULL_AOT_RUNTIME
                public static object CreateWrapperOfType (object o, Type t)
                {
                        __ComObject co = o as __ComObject;
@@ -221,12 +211,23 @@ namespace System.Runtime.InteropServices
                        return ComInteropProxy.GetProxy (co.IUnknown, t).GetTransparentProxy ();
                }
 
+#if NET_4_5
+               public static TWrapper CreateWrapperOfType<T, TWrapper> (T o) {
+                       return (TWrapper)CreateWrapperOfType ((object)o, typeof (TWrapper));
+               }
+#endif
+#endif
+
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-#if NET_2_0
                [ComVisible (true)]
-#endif
                public extern static void DestroyStructure (IntPtr ptr, Type structuretype);
 
+#if NET_4_5
+               public static void DestroyStructure<T> (IntPtr ptr) {
+                       DestroyStructure (ptr, typeof (T));
+               }
+#endif                 
+
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static void FreeBSTR (IntPtr ptr);
 
@@ -234,13 +235,9 @@ namespace System.Runtime.InteropServices
                public extern static void FreeCoTaskMem (IntPtr ptr);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                public extern static void FreeHGlobal (IntPtr hglobal);
 
-#if NET_2_0
-
                static void ClearBSTR (IntPtr ptr)
                {
                        int len = ReadInt32 (ptr, -4);
@@ -290,17 +287,34 @@ namespace System.Runtime.InteropServices
                        ClearUnicode (s);
                        FreeHGlobal (s);
                }
-#endif
 
+#if !FULL_AOT_RUNTIME
                public static Guid GenerateGuidForType (Type type)
                {
                        return type.GUID;
                }
 
-               [MonoTODO]
                public static string GenerateProgIdForType (Type type)
                {
-                       throw new NotImplementedException ();
+                       IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes (type);
+
+                       foreach (var a in attrs)
+                       {
+                               var dt = a.Constructor.DeclaringType;
+                               string name = dt.Name;
+                               if (name == "ProgIdAttribute")
+                               {
+                                       var args = a.ConstructorArguments;
+                                       string text = a.ConstructorArguments[0].Value as string;
+                                       if (text == null)
+                                       {
+                                               text = string.Empty;
+                                       }
+                                       return text;
+                               }
+                       }
+
+                       return type.FullName;
                }
 
                [MonoTODO]
@@ -309,6 +323,7 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
+#if !MOBILE
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern static IntPtr GetCCW (object o, Type T);
 
@@ -319,21 +334,30 @@ namespace System.Runtime.InteropServices
                        else
                                return GetCCW (o, T);
                }
+#endif
 
                public static IntPtr GetComInterfaceForObject (object o, Type T)
                {
+#if !MOBILE
                        IntPtr pItf = GetComInterfaceForObjectInternal (o, T);
                        AddRef (pItf);
                        return pItf;
+#else
+                       throw new NotImplementedException ();
+#endif
+               }
+
+#if NET_4_5
+               public static IntPtr GetComInterfaceForObject<T, TInterface> (T o) {
+                       return GetComInterfaceForObject ((object)o, typeof (T));
                }
+#endif                 
 
-#if NET_2_0
                [MonoTODO]
                public static IntPtr GetComInterfaceForObjectInContext (object o, Type t)
                {
                        throw new NotImplementedException ();
                }
-#endif
 
                [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
                public static object GetComObjectData (object obj, object key)
@@ -341,22 +365,24 @@ namespace System.Runtime.InteropServices
                        throw new NotSupportedException ("MSDN states user code should never need to call this method.");
                }
 
+#if !MOBILE
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private extern static int GetComSlotForMethodInfoInternal (MemberInfo m);
+#endif
 
                public static int GetComSlotForMethodInfo (MemberInfo m)
                {
+#if !MOBILE
                        if (m == null)
-#if NET_2_0
                                throw new ArgumentNullException ("m");
-#else
-                               throw new ArgumentNullException (null, "Value cannot be null.");
-#endif
                        if (!(m is MethodInfo))
                                throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
                        if (!m.DeclaringType.IsInterface)
                                throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
                        return GetComSlotForMethodInfoInternal (m);
+#else
+                       throw new NotImplementedException ();
+#endif
                }
 
                [MonoTODO]
@@ -372,9 +398,7 @@ namespace System.Runtime.InteropServices
                }
 
                [MonoTODO]
-#if NET_2_0
                [ComVisible (true)]
-#endif
                public static IntPtr GetExceptionPointers()
                {
                        throw new NotImplementedException ();
@@ -387,7 +411,9 @@ namespace System.Runtime.InteropServices
 
                        return m.GetHINSTANCE ();
                }
+#endif // !FULL_AOT_RUNTIME
 
+#if !FULL_AOT_RUNTIME
                [MonoTODO ("SetErrorInfo")]
                public static int GetHRForException (Exception e)
                {
@@ -395,9 +421,7 @@ namespace System.Runtime.InteropServices
                }
 
                [MonoTODO]
-#if NET_2_0
                [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                public static int GetHRForLastWin32Error()
                {
                        throw new NotImplementedException ();
@@ -414,13 +438,11 @@ namespace System.Runtime.InteropServices
                        return pUnk;
                }
 
-#if NET_2_0
                [MonoTODO]
                public static IntPtr GetIDispatchForObjectInContext (object o)
                {
                        throw new NotImplementedException ();
                }
-#endif
 
                [MonoTODO]
                public static IntPtr GetITypeInfoForType (Type t)
@@ -439,24 +461,14 @@ namespace System.Runtime.InteropServices
                        return pUnk;
                }
 
-#if NET_2_0
                [MonoTODO]
                public static IntPtr GetIUnknownForObjectInContext (object o)
                {
                        throw new NotImplementedException ();
                }
-#endif
-
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-#if NET_2_0
-               [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
-               public static extern int GetLastWin32Error();
 
                [MonoTODO]
-#if NET_2_0
                [Obsolete ("This method has been deprecated")]
-#endif
                public static IntPtr GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
                {
                        throw new NotImplementedException ();
@@ -475,11 +487,20 @@ namespace System.Runtime.InteropServices
                        Marshal.StructureToPtr(vt, pDstNativeVariant, false);
                }
 
+#if NET_4_5
+               public static void GetNativeVariantForObject<T> (T obj, IntPtr pDstNativeVariant) {
+                       GetNativeVariantForObject ((object)obj, pDstNativeVariant);
+               }
+#endif
+
+#if !MOBILE
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private static extern object GetObjectForCCW (IntPtr pUnk);
+#endif
 
                public static object GetObjectForIUnknown (IntPtr pUnk)
                {
+#if !MOBILE
                        object obj = GetObjectForCCW (pUnk);
                        // was not a CCW
                        if (obj == null) {
@@ -487,6 +508,9 @@ namespace System.Runtime.InteropServices
                                obj = proxy.GetTransparentProxy ();
                        }
                        return obj;
+#else
+                       throw new NotImplementedException ();
+#endif
                }
 
                public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant)
@@ -495,6 +519,13 @@ namespace System.Runtime.InteropServices
                        return vt.GetValue();
                }
 
+#if NET_4_5
+               public static T GetObjectForNativeVariant<T> (IntPtr pSrcNativeVariant) {
+                       Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
+                       return (T)vt.GetValue();
+               }
+#endif
+
                public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars)
                {
                        if (cVars < 0)
@@ -506,6 +537,18 @@ namespace System.Runtime.InteropServices
                        return objects;
                }
 
+#if NET_4_5
+               public static T[] GetObjectsForNativeVariants<T> (IntPtr aSrcNativeVariant, int cVars) {
+                       if (cVars < 0)
+                               throw new ArgumentOutOfRangeException ("cVars", "cVars cannot be a negative number.");
+                       T[] objects = new T[cVars];
+                       for (int i = 0; i < cVars; i++)
+                               objects[i] = GetObjectForNativeVariant<T> ((IntPtr)(aSrcNativeVariant.ToInt64 () +
+                                       i * SizeOf (typeof(Variant))));
+                       return objects;
+               }
+#endif
+
                [MonoTODO]
                public static int GetStartComSlot (Type t)
                {
@@ -513,9 +556,7 @@ namespace System.Runtime.InteropServices
                }
 
                [MonoTODO]
-#if NET_2_0
                [Obsolete ("This method has been deprecated")]
-#endif
                public static Thread GetThreadFromFiberCookie (int cookie)
                {
                        throw new NotImplementedException ();
@@ -540,38 +581,38 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
-               [Obsolete]
+#if NET_4_5
+               public static Type GetTypeFromCLSID (Guid clsid)
+               {
+                       throw new NotImplementedException ();                   
+               }
 #endif
+
+#if !FULL_AOT_RUNTIME
+               [Obsolete]
                [MonoTODO]
                public static string GetTypeInfoName (UCOMITypeInfo pTI)
                {
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
                public static string GetTypeInfoName (ITypeInfo typeInfo)
                {
                        throw new NotImplementedException ();
                }
-#endif
 
-#if NET_2_0
                [Obsolete]
-#endif
                [MonoTODO]
                public static Guid GetTypeLibGuid (UCOMITypeLib pTLB)
                {
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
                [MonoTODO]
                public static Guid GetTypeLibGuid (ITypeLib typelib)
                {
                        throw new NotImplementedException ();
                }
-#endif
 
                [MonoTODO]
                public static Guid GetTypeLibGuidForAssembly (Assembly asm)
@@ -579,33 +620,26 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
                [Obsolete]
-#endif
                [MonoTODO]
                public static int GetTypeLibLcid (UCOMITypeLib pTLB)
                {
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
                [MonoTODO]
                public static int GetTypeLibLcid (ITypeLib typelib)
                {
                        throw new NotImplementedException ();
                }
-#endif
 
-#if NET_2_0
                [Obsolete]
-#endif
                [MonoTODO]
                public static string GetTypeLibName (UCOMITypeLib pTLB)
                {
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
                [MonoTODO]
                public static string GetTypeLibName (ITypeLib typelib)
                {
@@ -625,16 +659,21 @@ namespace System.Runtime.InteropServices
 #endif
 
                [MonoTODO]
-#if NET_2_0
                [Obsolete ("This method has been deprecated")]
-#endif
                public static IntPtr GetUnmanagedThunkForManagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
                {
                        throw new NotImplementedException ();
                }
 
+#if !MOBILE
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                public extern static bool IsComObject (object o);
+#else
+               public static bool IsComObject (object o)
+               {
+                       throw new NotImplementedException ();
+               }
+#endif         
 
                [MonoTODO]
                public static bool IsTypeVisibleFromCom (Type t)
@@ -647,10 +686,21 @@ namespace System.Runtime.InteropServices
                {
                        throw new NotImplementedException ();
                }
+#endif
+
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
+               public static extern int GetLastWin32Error();
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr OffsetOf (Type t, string fieldName);
 
+#if NET_4_5
+               public static IntPtr OffsetOf<T> (string fieldName) {
+                       return OffsetOf (typeof (T), fieldName);
+               }
+#endif
+
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static void Prelink (MethodInfo m);
 
@@ -681,122 +731,193 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static string PtrToStringUni (IntPtr ptr, int len);
 
+#if !MOBILE
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static string PtrToStringBSTR (IntPtr ptr);
+#else
+               public static string PtrToStringBSTR (IntPtr ptr)
+               {
+                       throw new NotImplementedException ();
+               }
+#endif
                
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-#if NET_2_0
                [ComVisible (true)]
-#endif
                public extern static void PtrToStructure (IntPtr ptr, object structure);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-#if NET_2_0
                [ComVisible (true)]
-#endif
                public extern static object PtrToStructure (IntPtr ptr, Type structureType);
 
+#if NET_4_5
+               public static void PtrToStructure<T> (IntPtr ptr, T structure) {
+                       PtrToStructure (ptr, (object)structure);
+               }
+
+               public static T PtrToStructure<T> (IntPtr ptr) {
+                       return (T) PtrToStructure (ptr, typeof (T));
+               }
+#endif
+
+#if !MOBILE
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern static int QueryInterfaceInternal (IntPtr pUnk, ref Guid iid, out IntPtr ppv);
+#endif
 
                public static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv)
                {
+#if !MOBILE
                        if (pUnk == IntPtr.Zero)
                                throw new ArgumentException ("Value cannot be null.", "pUnk");
                        return QueryInterfaceInternal (pUnk, ref iid, out ppv);
+#else
+                       throw new NotImplementedException ();
+#endif
                }
 
                public static byte ReadByte (IntPtr ptr)
                {
-                       return ReadByte (ptr, 0);
+                       unsafe {
+                               return *(byte*)ptr;
+                       }
                }
 
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               public extern static byte ReadByte (IntPtr ptr, int ofs);
+               public static byte ReadByte (IntPtr ptr, int ofs) {
+                       unsafe {
+                               return *((byte*)ptr + ofs);
+                       }
+               }
 
                [MonoTODO]
+               [SuppressUnmanagedCodeSecurity]
                public static byte ReadByte ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
                {
                        throw new NotImplementedException ();
                }
 
-               public static short ReadInt16 (IntPtr ptr)
+               public unsafe static short ReadInt16 (IntPtr ptr)
                {
-                       return ReadInt16 (ptr, 0);
+                       byte *addr = (byte *) ptr;
+                       
+                       // The mono JIT can't inline this due to the hight number of calls
+                       // return ReadInt16 (ptr, 0);
+                       
+                       if (((uint)addr & 1) == 0) 
+                               return *(short*)addr;
+
+                       short s;
+                       String.memcpy ((byte*)&s, (byte*)ptr, 2);
+                       return s;
                }
 
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               public extern static short ReadInt16 (IntPtr ptr, int ofs);
+               public unsafe static short ReadInt16 (IntPtr ptr, int ofs)
+               {
+                       byte *addr = ((byte *) ptr) + ofs;
+
+                       if (((uint) addr & 1) == 0)
+                               return *(short*)addr;
+
+                       short s;
+                       String.memcpy ((byte*)&s, addr, 2);
+                       return s;
+               }
 
                [MonoTODO]
+               [SuppressUnmanagedCodeSecurity]
                public static short ReadInt16 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
                {
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
-               public static int ReadInt32 (IntPtr ptr)
+               public unsafe static int ReadInt32 (IntPtr ptr)
                {
-                       return ReadInt32 (ptr, 0);
+                       byte *addr = (byte *) ptr;
+                       
+                       if (((uint)addr & 3) == 0) 
+                               return *(int*)addr;
+
+                       int s;
+                       String.memcpy ((byte*)&s, addr, 4);
+                       return s;
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               public extern static int ReadInt32 (IntPtr ptr, int ofs);
+               public unsafe static int ReadInt32 (IntPtr ptr, int ofs)
+               {
+                       byte *addr = ((byte *) ptr) + ofs;
+                       
+                       if ((((int) addr) & 3) == 0)
+                               return *(int*)addr;
+                       else {
+                               int s;
+                               String.memcpy ((byte*)&s, addr, 4);
+                               return s;
+                       }
+               }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                [MonoTODO]
+               [SuppressUnmanagedCodeSecurity]
                public static int ReadInt32 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
                {
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
-               public static long ReadInt64 (IntPtr ptr)
+               public unsafe static long ReadInt64 (IntPtr ptr)
                {
-                       return ReadInt64 (ptr, 0);
+                       byte *addr = (byte *) ptr;
+                               
+                       // The real alignment might be 4 on some platforms, but this is just an optimization,
+                       // so it doesn't matter.
+                       if (((uint) addr & 7) == 0)
+                               return *(long*)ptr;
+
+                       long s;
+                       String.memcpy ((byte*)&s, addr, 8);
+                       return s;
                }
 
-#if NET_2_0
-               [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               public extern static long ReadInt64 (IntPtr ptr, int ofs);
+               public unsafe static long ReadInt64 (IntPtr ptr, int ofs)
+               {
+                       byte *addr = ((byte *) ptr) + ofs;
+
+                       if (((uint) addr & 7) == 0)
+                               return *(long*)addr;
+                       
+                       long s;
+                       String.memcpy ((byte*)&s, addr, 8);
+                       return s;
+               }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                [MonoTODO]
+               [SuppressUnmanagedCodeSecurity]
                public static long ReadInt64 ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
                {
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                public static IntPtr ReadIntPtr (IntPtr ptr)
                {
-                       return ReadIntPtr (ptr, 0);
+                       if (IntPtr.Size == 4)
+                               return (IntPtr)ReadInt32 (ptr);
+                       else
+                               return (IntPtr)ReadInt64 (ptr);
                }
                
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               public extern static IntPtr ReadIntPtr (IntPtr ptr, int ofs);
+               public static IntPtr ReadIntPtr (IntPtr ptr, int ofs)
+               {
+                       if (IntPtr.Size == 4)
+                               return (IntPtr)ReadInt32 (ptr, ofs);
+                       else
+                               return (IntPtr)ReadInt64 (ptr, ofs);
+               }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                [MonoTODO]
                public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
                {
@@ -809,22 +930,26 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
 
-#if NET_2_0
+#if !MOBILE
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern static int ReleaseInternal (IntPtr pUnk);
+#endif
 
-#if NET_2_0
                [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                public static int Release (IntPtr pUnk)
                {
+#if !MOBILE
                        if (pUnk == IntPtr.Zero)
                                throw new ArgumentException ("Value cannot be null.", "pUnk");
+
                        return ReleaseInternal (pUnk);
+#else
+                       throw new NotImplementedException ();
+#endif
                }
 
+#if !FULL_AOT_RUNTIME
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern static int ReleaseComObjectInternal (object co);
 
@@ -837,9 +962,7 @@ namespace System.Runtime.InteropServices
                        return ReleaseComObjectInternal (o);
                }
 
-#if NET_2_0
                [Obsolete]
-#endif
                [MonoTODO]
                public static void ReleaseThreadCache()
                {
@@ -851,10 +974,9 @@ namespace System.Runtime.InteropServices
                {
                        throw new NotSupportedException ("MSDN states user code should never need to call this method.");
                }
+#endif
 
-#if NET_2_0
                [ComVisible (true)]
-#endif
                public static int SizeOf (object structure)
                {
                        return SizeOf (structure.GetType ());
@@ -863,6 +985,16 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static int SizeOf (Type t);
 
+#if NET_4_5
+               public static int SizeOf<T> () {
+                       return SizeOf (typeof (T));
+               }
+
+               public static int SizeOf<T> (T structure) {
+                       return SizeOf (structure.GetType ());
+               }
+#endif
+
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr StringToBSTR (string s);
 
@@ -916,7 +1048,6 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr StringToHGlobalUni (string s);
 
-#if NET_2_0
                public static IntPtr SecureStringToBSTR (SecureString s)
                {
                        if (s == null)
@@ -1004,15 +1135,18 @@ namespace System.Runtime.InteropServices
                                throw new ArgumentNullException ("s");
                        return SecureStringToCoTaskMemUnicode (s);
                }
-#endif
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
                [ComVisible (true)]
-#endif
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
 
+#if NET_4_5
+               public static void StructureToPtr<T> (T structure, IntPtr ptr, bool fDeleteOld) {
+                       StructureToPtr ((object)structure, ptr, fDeleteOld);
+               }
+#endif
+
                public static void ThrowExceptionForHR (int errorCode) {
                        Exception ex = GetExceptionForHR (errorCode);
                        if (ex != null)
@@ -1028,29 +1162,55 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr UnsafeAddrOfPinnedArrayElement (Array arr, int index);
 
+#if NET_4_5
+               public static IntPtr UnsafeAddrOfPinnedArrayElement<T> (T[] arr, int index) {
+                       return UnsafeAddrOfPinnedArrayElement ((Array)arr, index);
+               }
+#endif
+
                public static void WriteByte (IntPtr ptr, byte val)
                {
-                       WriteByte (ptr, 0, val);
+                       unsafe {
+                               *(byte*)ptr = val;
+                       }
                }
 
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               public extern static void WriteByte (IntPtr ptr, int ofs, byte val);
+               public static void WriteByte (IntPtr ptr, int ofs, byte val) {
+                       unsafe {
+                               *(byte*)(IntPtr.Add (ptr, ofs)) = val;
+                       }
+               }
 
                [MonoTODO]
+               [SuppressUnmanagedCodeSecurity]
                public static void WriteByte ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, byte val)
                {
                        throw new NotImplementedException ();
                }
 
-               public static void WriteInt16 (IntPtr ptr, short val)
+               public static unsafe void WriteInt16 (IntPtr ptr, short val)
                {
-                       WriteInt16 (ptr, 0, val);
+                       byte *addr = (byte *) ptr;
+                       
+                       if (((uint)addr & 1) == 0)
+                               *(short*)addr = val;
+                       else
+                               String.memcpy (addr, (byte*)&val, 2);
                }
 
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               public extern static void WriteInt16 (IntPtr ptr, int ofs, short val);
+               public static unsafe void WriteInt16 (IntPtr ptr, int ofs, short val)
+               {
+                       byte *addr = ((byte *) ptr) + ofs;
+
+                       if (((uint)addr & 1) == 0)
+                               *(short*)addr = val;
+                       else {
+                               String.memcpy (addr, (byte*)&val, 2);
+                       }
+               }
 
                [MonoTODO]
+               [SuppressUnmanagedCodeSecurity]
                public static void WriteInt16 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, short val)
                {
                        throw new NotImplementedException ();
@@ -1058,12 +1218,13 @@ namespace System.Runtime.InteropServices
 
                public static void WriteInt16 (IntPtr ptr, char val)
                {
-                       WriteInt16 (ptr, 0, val);
+                       WriteInt16 (ptr, 0, (short)val);
                }
 
-               [MonoTODO]
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               public extern static void WriteInt16 (IntPtr ptr, int ofs, char val);
+               public static void WriteInt16 (IntPtr ptr, int ofs, char val)
+               {
+                       WriteInt16 (ptr, ofs, (short)val);
+               }
 
                [MonoTODO]
                public static void WriteInt16([In, Out] object ptr, int ofs, char val)
@@ -1071,29 +1232,61 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-               public static void WriteInt32 (IntPtr ptr, int val)
+               public static unsafe void WriteInt32 (IntPtr ptr, int val)
                {
-                       WriteInt32 (ptr, 0, val);
+                       byte *addr = (byte *) ptr;
+                       
+                       if (((uint)addr & 3) == 0) 
+                               *(int*)addr = val;
+                       else {
+                               String.memcpy (addr, (byte*)&val, 4);
+                       }
                }
 
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               public extern static void WriteInt32 (IntPtr ptr, int ofs, int val);
+               public unsafe static void WriteInt32 (IntPtr ptr, int ofs, int val)
+               {
+                       byte *addr = ((byte *) ptr) + ofs;
+
+                       if (((uint)addr & 3) == 0) 
+                               *(int*)addr = val;
+                       else {
+                               String.memcpy (addr, (byte*)&val, 4);
+                       }
+               }
 
                [MonoTODO]
+               [SuppressUnmanagedCodeSecurity]
                public static void WriteInt32([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, int val)
                {
                        throw new NotImplementedException ();
                }
 
-               public static void WriteInt64 (IntPtr ptr, long val)
+               public static unsafe void WriteInt64 (IntPtr ptr, long val)
                {
-                       WriteInt64 (ptr, 0, val);
+                       byte *addr = (byte *) ptr;
+                       
+                       // The real alignment might be 4 on some platforms, but this is just an optimization,
+                       // so it doesn't matter.
+                       if (((uint)addr & 7) == 0) 
+                               *(long*)addr = val;
+                       else 
+                               String.memcpy (addr, (byte*)&val, 8);
                }
 
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               public extern static void WriteInt64 (IntPtr ptr, int ofs, long val);
+               public static unsafe void WriteInt64 (IntPtr ptr, int ofs, long val)
+               {
+                       byte *addr = ((byte *) ptr) + ofs;
+
+                       // The real alignment might be 4 on some platforms, but this is just an optimization,
+                       // so it doesn't matter.
+                       if (((uint)addr & 7) == 0) 
+                               *(long*)addr = val;
+                       else 
+                               String.memcpy (addr, (byte*)&val, 8);
+               }
 
                [MonoTODO]
+               [SuppressUnmanagedCodeSecurity]
                public static void WriteInt64 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, long val)
                {
                        throw new NotImplementedException ();
@@ -1101,11 +1294,19 @@ namespace System.Runtime.InteropServices
 
                public static void WriteIntPtr (IntPtr ptr, IntPtr val)
                {
-                       WriteIntPtr (ptr, 0, val);
+                       if (IntPtr.Size == 4)
+                               WriteInt32 (ptr, (int)val);
+                       else
+                               WriteInt64 (ptr, (long)val);
                }
 
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               public extern static void WriteIntPtr (IntPtr ptr, int ofs, IntPtr val);
+               public static void WriteIntPtr (IntPtr ptr, int ofs, IntPtr val)
+               {
+                       if (IntPtr.Size == 4)
+                               WriteInt32 (ptr, ofs, (int)val);
+                       else
+                               WriteInt64 (ptr, ofs, (long)val);
+               }
 
                [MonoTODO]
                public static void WriteIntPtr([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, IntPtr val)
@@ -1113,21 +1314,11 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
-               public
-#else
-               internal
-#endif
-               static Exception GetExceptionForHR (int errorCode) {
+               public static Exception GetExceptionForHR (int errorCode) {
                        return GetExceptionForHR (errorCode, IntPtr.Zero);
                }
 
-#if NET_2_0
-               public
-#else
-               internal
-#endif
-               static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo) {
+               public static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo) {
 
                        const int E_OUTOFMEMORY = unchecked ((int)0x8007000EL);
                        const int E_INVALIDARG = unchecked ((int)0X80070057);
@@ -1144,12 +1335,13 @@ namespace System.Runtime.InteropServices
                        return null;
                }
 
-#if NET_2_0
+#if !FULL_AOT_RUNTIME
                public static int FinalReleaseComObject (object o)
                {
                        while (ReleaseComObject (o) != 0);
                        return 0;
                }
+#endif
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private static extern Delegate GetDelegateForFunctionPointerInternal (IntPtr ptr, Type t);
@@ -1160,12 +1352,20 @@ namespace System.Runtime.InteropServices
                                throw new ArgumentNullException ("t");
                        if (!t.IsSubclassOf (typeof (MulticastDelegate)) || (t == typeof (MulticastDelegate)))
                                throw new ArgumentException ("Type is not a delegate", "t");
+                       if (t.IsGenericType)
+                               throw new ArgumentException ("The specified Type must not be a generic type definition.");
                        if (ptr == IntPtr.Zero)
                                throw new ArgumentNullException ("ptr");
 
                        return GetDelegateForFunctionPointerInternal (ptr, t);
                }
 
+#if NET_4_5
+               public static TDelegate GetDelegateForFunctionPointer<TDelegate> (IntPtr ptr) {
+                       return (TDelegate) (object) GetDelegateForFunctionPointer (ptr, typeof (TDelegate));
+               }
+#endif
+
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private static extern IntPtr GetFunctionPointerForDelegateInternal (Delegate d);
                
@@ -1176,6 +1376,14 @@ namespace System.Runtime.InteropServices
                        
                        return GetFunctionPointerForDelegateInternal (d);
                }
+
+#if NET_4_5
+               public static IntPtr GetFunctionPointerForDelegate<TDelegate> (TDelegate d) {
+                       if (d == null)
+                               throw new ArgumentNullException ("d");
+                       
+                       return GetFunctionPointerForDelegateInternal ((Delegate)(object)d);
+               }
 #endif
        }
 }