如何在C#结构中定义动态数组 [英] How to define a dynamic array in a C# structure

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

问题描述

我在c ++中有一个结构,指针作为该结构的成员,用于指向另一种结构的数组。我不知道该数组的大小,因为它是由第三方dll动态分配的,而且是读取文件的。请帮我这个



我尝试过:



你好我是c#的新手并且遇到了问题。

i必须将应用程序从c ++移植到c#,而在c ++中有一种格式结构如图所示

struct框架

{

int frameID;

dirData * arr_dirdata;

}



struct dirData

{

int nFiles;

filesData * arr_fileData;

这些结构用于从文件中读取数据,而这些数据实际上是一些dirData结构数组和filesData结构数组。该结构实际​​上是由第三方dll分配的。我没有dll的c#版本,所以我使用的是c ++版本并将其包裹起来。



当我将相同的结构转换为c#i时定义如下

[StructLayout(LayoutKind.Sequential)]

struct frame

{

int frameID;

[MarshalAs(UnmanagedType.LPArray,SizeConst = 1)]

dirData [] arrdirData;

}

如何定义在我的情况下阵列。





在此先感谢

AK

解决方案

你已经为静态数组做了

 dirData [] arrdirData; 



现在只需设置你想要的初始尺寸就可以让它成为50

 arrdirData = new dirData [50]; 



或者如果你想要它在一行

 dirData [] arrdirData = new dirdata [50]; 



但是在C#那里您可以使用通用列表c创建动态数组lass

列表< dirdata> arrdirData =  new 列表< dirdata>(); 



您只需使用aardirData.Add添加等你可以查看通用列表

函数,如添加,删除,计数,替换等。你需要在文件顶部包含

通用列表标题


I have a structure in c++ with pointers as the member of that structure which is used to point to an array of another type of structures. I dont know the size of that array as that is dynamically allocated by a thirdparty dll and that to reading a file. Please help me with this

What I have tried:

Hi i am very new to c# and have encountered a problem.
i have to port an application from c++ to c# and in c++ there is a structure of the format as shown
struct Frame
{
int frameID;
dirData* arr_dirdata;
}

struct dirData
{
int nFiles;
filesData* arr_fileData;
}

These structures are used to read data from a file and that data is actually a number of dirData structure array and filesData structure array. The structure is actually allocated by a third party dll. i dont have the c# version of the dll so i am using the c++ version and wrapping around that.

when i am converting the same structure to c# i defined as following
[StructLayout(LayoutKind.Sequential)]
struct Frame
{
int frameID;
[MarshalAs(UnmanagedType.LPArray, SizeConst = 1)]
dirData[] arrdirData;
}
How to define an array in my case.


Thanks in Advance
AK

解决方案

You already did it for static arrays

dirData[] arrdirData;


Now just set the initial size you want lets make it 50

arrdirData = new dirData[50];


Or if you want it in one line

dirData[] arrdirData = new dirdata[50];


However in C# there you can create dynamic arrays using the generic list class

List<dirdata> arrdirData = new List <dirdata>();


You just the use aardirData.Add to add etc. You can look at the generic list
functions like add, delete, count, replace etc. You will need to include the
generic list header at the top of your file.


这篇关于如何在C#结构中定义动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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