有没有一种方法可以将结构(可能存储在TypedReference中)编组到非托管内存,而无需实际装箱?结构的类型在运行时未知,因此我不能使用StructureToPtr(.NET 4.5.1)的泛型重载。我可以得到StructureToPtr重载的MethodInfo,但是似乎没有一种方法可以通过泛型引用或TypedReference来调用它。

编辑:通用的StructureToPtr仍然装箱结构,因此尝试调用它是无用的。

最佳答案

我终于找到了答案,SafeBuffer类。它恰好包含我想要的-使用同时使用TypedReference和通用参数的编组方法进行结构。因此,制作包装非常简单:

public static unsafe class InteropTools
{
    private static readonly Type SafeBufferType = typeof(SafeBuffer);
    public delegate void PtrToStructureNativeDelegate(byte* ptr, TypedReference structure, uint sizeofT);
    public delegate void StructureToPtrNativeDelegate(TypedReference structure, byte* ptr, uint sizeofT);
    const BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Static;
    private static readonly MethodInfo PtrToStructureNativeMethod = SafeBufferType.GetMethod("PtrToStructureNative", flags);
    private static readonly MethodInfo StructureToPtrNativeMethod = SafeBufferType.GetMethod("StructureToPtrNative", flags);
    public static readonly PtrToStructureNativeDelegate PtrToStructureNative = (PtrToStructureNativeDelegate)Delegate.CreateDelegate(typeof(PtrToStructureNativeDelegate), PtrToStructureNativeMethod);
    public static readonly StructureToPtrNativeDelegate StructureToPtrNative = (StructureToPtrNativeDelegate)Delegate.CreateDelegate(typeof(StructureToPtrNativeDelegate), StructureToPtrNativeMethod);

    private static readonly Func<Type,bool,int> SizeOfHelper_f = (Func<Type,bool,int>)Delegate.CreateDelegate(typeof(Func<Type,bool,int>), typeof(Marshal).GetMethod("SizeOfHelper", flags));

    public static void StructureToPtrDirect(TypedReference structure, IntPtr ptr, int size)
    {
        StructureToPtrNative(structure, (byte*)ptr, unchecked((uint)size));
    }

    public static void StructureToPtrDirect(TypedReference structure, IntPtr ptr)
    {
        StructureToPtrDirect(structure, ptr, SizeOf(__reftype(structure)));
    }

    public static void PtrToStructureDirect(IntPtr ptr, TypedReference structure, int size)
    {
        PtrToStructureNative((byte*)ptr, structure, unchecked((uint)size));
    }

    public static void PtrToStructureDirect(IntPtr ptr, TypedReference structure)
    {
        PtrToStructureDirect(ptr, structure, SizeOf(__reftype(structure)));
    }

    public static void StructureToPtr<T>(ref T structure, IntPtr ptr)
    {
        StructureToPtrDirect(__makeref(structure), ptr);
    }

    public static void PtrToStructure<T>(IntPtr ptr, out T structure)
    {
        structure = default(T);
        PtrToStructureDirect(ptr, __makeref(structure));
    }

    public static T PtrToStructure<T>(IntPtr ptr)
    {
        T obj;
        PtrToStructure(ptr, out obj);
        return obj;
    }

    public static int SizeOf<T>(T structure)
    {
        return SizeOf<T>();
    }

    public static int SizeOf<T>()
    {
        return SizeOf(typeof(T));
    }

    public static int SizeOf(Type t)
    {
        return SizeOfHelper_f(t, true);
    }
}


用法

Guid g = Guid.NewGuid();
int size = InteropTools.SizeOf(g);
IntPtr mem = Marshal.AllocHGlobal(size);
InteropTools.StructureToPtr(ref g, mem);

Guid g2 = InteropTools.PtrToStructure<Guid>(mem);


现在,它实际上比Marshal类中的非通用对象获取方法有什么优势吗?看来StructureToPtr花费的时间减少了约80%,而PtrToStructure花费的时间减少了近95%。同样,这些方法可以正确处理可为空的类型。

10-06 12:16