【问题标题】:C# How can I declare a Dictionary inside a struct that is being marshalled to get the sizeof the struct?C# 如何在正在编组的结构中声明字典以获取结构的大小?
【发布时间】:2016-04-08 06:33:46
【问题描述】:

我有以下结构

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct CombinedDS
{
    public HeaderStruct Header;
    public StructA a;
    public StructB b;
    public StructC c;
    public StructD d;
    public Dictionary<string, KiteClass> KiteObjDict;
}

其中StructA StructB StructC StructD 都是带有[StructLayout(LayoutKind.Sequential, Pack = 1)] 的结构 而KiteClass 是一个普通的类。

我收到了这个 ArgumentException类型“MyApp.CombinedDS”不能作为非托管结构进行封送;当我试图获取结构 CombinedDS 的大小时,无法计算出有意义的大小或偏移量

int varsize = Marshal.SizeOf(typeof(CombinedDS));

我想知道这背后的理论原因,以及实现我想要的正确方法或任何建议。

【问题讨论】:

    标签: c# dictionary structure marshalling argumentexception


    【解决方案1】:

    字典是任意数量的项目的集合。编译器不知道它的大小,因为它会在运行时添加/删除项目时发生变化。

    如果您需要建议,您可能需要在问题中添加信息,因为它并没有真正说明您要达到的目标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多