From 38baa53cc114e65af3fd08f160b32617ff73d487 Mon Sep 17 00:00:00 2001 From: David Hall Date: Fri, 6 Dec 2019 17:09:56 -0700 Subject: [PATCH] Unfinished work on object marshaler --- Core/InteropServices/IntPtrConverter.cs | 40 ++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/Core/InteropServices/IntPtrConverter.cs b/Core/InteropServices/IntPtrConverter.cs index a9d274e3..1f91eeec 100644 --- a/Core/InteropServices/IntPtrConverter.cs +++ b/Core/InteropServices/IntPtrConverter.cs @@ -1,10 +1,14 @@ using System; +using System.Collections; +using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; using System.Runtime.InteropServices; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using Vanara.Extensions; +using Vanara.Extensions.Reflection; using Vanara.PInvoke; namespace Vanara.InteropServices @@ -52,7 +56,7 @@ namespace Vanara.InteropServices { return marshaler.MarshalNativeToManaged(ptr, sz); } - if (typeof(ISerializable).IsAssignableFrom(destType)) + if (destType.IsSerializable) { using var mem = new MemoryStream(ptr.ToArray((int)sz)); return new BinaryFormatter().Deserialize(mem); @@ -101,6 +105,40 @@ namespace Vanara.InteropServices object GetBlittable(Type retType) => InteropExtensions.GetValueType(ptr, retType); } + /*public static IntPtr Convert(object value, Func memAlloc, out int bytesAllocated, CharSet charSet = CharSet.Auto, int prefixBytes = 0) + { + bytesAllocated = 0; + if (value is null) + return IntPtr.Zero; + + var type = value.GetType(); + + // Handle special cases + if (type.IsArray || type.InheritsFrom(typeof(IEnumerable<>))) + { + var elemType = type.FindElementType(); + if (elemType == typeof(string)) + { + } + else + { + var enumType = typeof(IEnumerable<>).MakeGenericType(new[] { elemType }); + var mi = typeof(InteropExtensions).GetMethod("Write", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(IntPtr), enumType, typeof(int), typeof(SizeT) }, null); + var gmi = mi.MakeGenericMethod(new[] { elemType }); + return gmi.Invoke(null, new object[] { x }); + } + } + if (value is IEnumerable) + { + if () + { + + } + } + + throw new NotImplementedException(); + }*/ + /// Converts the specified pointer to . /// The destination type. /// A block of allocated memory.