ICustomMarshaler 可变长度数组

发布于 2024-12-11 04:37:59 字数 948 浏览 0 评论 0原文

我知道可以使用数组索引来指定封送 C 数组的数组长度。然而,我想做一些不同的事情。

我希望大小是前缀 Int16。如果我将其作为数组的条目,我将无法控制计数说明符的编组大小。

那么,简而言之,如何编写一个自定义编组器,将 Int16 作为计数前缀。

请注意,我必须序列化数据,因此不允许使用 IntPtr。

我困惑的部分是如何实现 GetNativeDataSize。此时我没有 IntPtr 或托管对象,所以我如何能够编组 Int16 来获取计数。

例如。

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
class Something
{
    [MarshalAs(UnmanagedType.CustomMarshaler ... ]
    public ArrayItem[];
}

而另一个类

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
class ArrayItem
{
    public int Item;
}

在本机端,这将显示为

struct
{
    short count;
    int[] Item;
}

但由于托管端的每个可变长度数组都会执行此操作,我希望自定义编组器为我附加计数。

问题是

  • 我不知道 GetNativeDataSize 应该做什么,或者它如何工作,因为我没有任何对本机数据的引用。
  • 我不能依赖 LPArray,因为计数必须是 Int16
  • 数组项编组器是否正确,或者数组自定义编组器是否隐藏项目编组器,或者我是否必须实现一些通用编组器(如果可能) 。

I know one can use an index into an array to specify length of the array for a marshaled C-array. However, I would like to do it a little different.

I would like the size to be a prefixed Int16. If I make it an entry of the array, I can't control the marshaled size of the count specifier.

So, in short, how do I write a custom marshaller that prefixes an Int16 as a count.

Please note, I have to serialize the data, so no IntPtrs are allowed.

The part I'm stumped on, is how to implement GetNativeDataSize. I don't have an IntPtr or Managed Object at that point, so how will I be able to marshal out the Int16 to get the count.

For example.

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
class Something
{
    [MarshalAs(UnmanagedType.CustomMarshaler ... ]
    public ArrayItem[];
}

And the other class

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
class ArrayItem
{
    public int Item;
}

In Native side, this would appear as

struct
{
    short count;
    int[] Item;
}

But since every variable length array on managed side would do this, I would like a custom marshaller to append the count for me.

Problems are

  • I don't know what GetNativeDataSize should do, or how it will work, since I don't have any reference to the native data.
  • I can't rely on the LPArray, because the count must be an Int16
  • Will the array items marshal correctly, or does the array custom marshaller hide the items marshaller, or am I going to have to implement some generic marshaller (if that's possible).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2024-12-18 04:37:59

我要说的是......自定义封送拆收器感觉有点矫枉过正。如果您确实想使用自定义封送拆收器来执行此操作,则必须使用不安全块/方法和指针数学来写入数据。

为什么不拥有一个类似于您的本机结构的类,并在编组时将您的数组包装在其中?确实,无论如何你都在尝试传递一个结构;自定义封送拆收器的唯一原因是从数组神奇地生成该结构。在我看来,最好避免这种魔法,同时让默认封送拆收器完成繁重的工作。

I'm gonna say it...a custom marshaler feels like overkill. If you really want to do this with a custom marshaler, you're going to have to use unsafe blocks/methods and pointer math to write the data.

Why not have a class similar to your native struct, and wrap your array in that when marshaling? Really, you're trying to pass a structure anyway; the only reason for the custom marshaler is to magically generate that struct from the array. Better to avoid the magic, IMO, and at the same time let the default marshaler do the grunt work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文