简体   繁体   中英

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.

I am getting this ArgumentException : Type 'MyApp.CombinedDS' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed when am trying to get the size of the struct CombinedDS using

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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