简体   繁体   English

C#如何在已被整理以获取结构大小的结构中声明字典?

[英]C# How can I declare a Dictionary inside a struct that is being marshalled to get the sizeof the struct?

I have this following struct 我有以下结构

[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;
}

where StructA StructB StructC StructD all are structures with [StructLayout(LayoutKind.Sequential, Pack = 1)] and KiteClass is a normal class. 其中StructA StructB StructC StructD全部是具有[StructLayout(LayoutKind.Sequential, Pack = 1)]KiteClass是普通类。

I am getting this ArgumentException : Type 'MyApp.CombinedDS' cannot be marshaled as an unmanaged structure; 我收到此ArgumentException类型'MyApp.CombinedDS'无法编组为非托管结构; no meaningful size or offset can be computed when am trying to get the size of the struct CombinedDS using 尝试使用以下方法获取结构CombinedDS的大小时, 无法计算出有意义的大小或偏移量

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

I would like to know the theoretical reason behind this and also the correct methodology or any suggestion to achieve what i want. 我想知道背后的理论原因,也想知道实现我想要的目标的正确方法或任何建议。

A Dictionary is a collection of any number of items. 词典是任意数量项目的集合。 It's size is not known to the compiler as it will change at runtime when items are added/removed. 它的大小对于编译器是未知的,因为在添加/删除项目时它将在运行时更改。

You may want to add information to the question if you want suggestions as it doesn't really specify what you're trying to achieve. 如果您需要建议,则可能需要向问题中添加信息,因为它实际上并未指定您要实现的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM