Implement net 4.5.1 generic wrapper methods in Marshal.
authorZoltan Varga <vargaz@gmail.com>
Wed, 25 Sep 2013 11:03:51 +0000 (13:03 +0200)
committerZoltan Varga <vargaz@gmail.com>
Wed, 25 Sep 2013 11:04:02 +0000 (13:04 +0200)
mcs/class/corlib/System.Runtime.InteropServices/Marshal.cs

index a3134933a312865392cb309f979a53957438d7f3..a66a8dc7ae222b5a49ceca16ac8e6f8334b2c474 100644 (file)
@@ -187,6 +187,12 @@ 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)
                {
@@ -204,12 +210,24 @@ 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)]
                [ComVisible (true)]
                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);
 
@@ -329,6 +347,12 @@ namespace System.Runtime.InteropServices
 #endif
                }
 
+#if NET_4_5
+               public static IntPtr GetComInterfaceForObject<T, TInterface> (T o) {
+                       return GetComInterfaceForObject ((object)o, typeof (T));
+               }
+#endif                 
+
                [MonoTODO]
                public static IntPtr GetComInterfaceForObjectInContext (object o, Type t)
                {
@@ -463,6 +487,12 @@ 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);
@@ -489,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)
@@ -500,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)
                {
@@ -639,6 +688,12 @@ namespace System.Runtime.InteropServices
                [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);
 
@@ -687,6 +742,16 @@ namespace System.Runtime.InteropServices
                [ComVisible (true)]
                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 object PtrToStructure<T> (IntPtr ptr) {
+                       return PtrToStructure (ptr, typeof (T));
+               }
+#endif
+
 #if !MOBILE
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern static int QueryInterfaceInternal (IntPtr pUnk, ref Guid iid, out IntPtr ppv);
@@ -913,6 +978,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);
 
@@ -1059,6 +1134,12 @@ namespace System.Runtime.InteropServices
                [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)
@@ -1074,6 +1155,12 @@ 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)
                {
                        unsafe {
@@ -1266,6 +1353,12 @@ namespace System.Runtime.InteropServices
                        return GetDelegateForFunctionPointerInternal (ptr, t);
                }
 
+#if NET_4_5
+               public static Delegate GetDelegateForFunctionPointer<T> (IntPtr ptr) {
+                       return GetDelegateForFunctionPointer (ptr, typeof (T));
+               }
+#endif
+
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private static extern IntPtr GetFunctionPointerForDelegateInternal (Delegate d);
                
@@ -1276,5 +1369,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
        }
 }