如何在struct中声明数组? [英] How I can declare arrays in struct?

查看:72
本文介绍了如何在struct中声明数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何声明结构中包含固定大小的数组?

How can I declare structure with a fixed size array in it?

我找到了解决方案,但是它仅适用于原始数据类型.我需要我的数组的类型为 MyStruct .

I found solution, but it only works for primitive data-types. I need my array to be of type MyStruct.

那么如何声明一个包含其他结构数组的结构?

So how can I declare a struct with an array of other structs in it?

例如.

    unsafe struct Struct1{
      fixed int arrayInt[100]; // works properly 
      fixed Struct2 arrayStruct[100]; //not compile
    }

推荐答案

我的同事找到了可行的方法.我认为这是正确的方法.

My colleague found the working way to do this. I think it`s right way.

    [StructLayout(LayoutKind.Sequential)]
     public struct Struct1
     {
           [MarshalAs(UnmanagedType.ByValArray, SizeConst = sizeOfarray)]
           private Struct2[] arrayStruct;
     }

这篇关于如何在struct中声明数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆