【问题标题】:arrays struct in c#c#中的数组结构
【发布时间】:2013-03-25 12:35:45
【问题描述】:

我正在尝试这样做:

public static class GlobalVar
{
    [DllImport("Export.dll")]
    public static extern sentences Export();
    public unsafe struct sentence_node
    {
        public sentence_node* next;   // next node in the dictionary in the same level
        public int sNum;  // sentence number starting from 1
        public int sLoc;  // the location in the sentence (protien)
    }

    public unsafe struct sentences
    {  // list of lists of sentences in which words exists. 
        public fixed sentence_node* sList[50];
        public char[,] xplus = new char[50, 100];    
        public int wordCount;   
    }
}

但我收到这两个错误:

错误 1:

固定大小的缓冲区类型必须是以下之一:bool、 byte、short、int、long、char、sbyte、ushort、uint、ulong、float 或 双 C:\Users\Aseel\Documents\Visual Studio 2010\Projects\CBS\CBS\GlobalVar.cs 40 22 CBS

错误 2:

GlobalVar.sentences.xplus': 不能有实例字段初始化器 结构 C:\Users\Aseel\Documents\Visual Studio 2010\Projects\CBS\CBS\GlobalVar.cs 41 24 CBS

dll 文件包含 C 语言中的搜索算法,并具有我在上面发布的两个结构以及其他结构,但我需要这两个来显示我的结果。有没有办法进入这些结构而无需在 C# 中重新定义它们?

【问题讨论】:

标签: c# arrays struct pinvoke dllimport


【解决方案1】:

一般来说,在 COM 和 .NET 之间传输对象时,要么对象具有非常简单的内存占用,要么将对象中的数据从适用于一个框架的数据结构复制到一个适合另一个。

我无法准确说出您要对数据结构做什么,但也许最简单的做法是将所有内容存储在一个或多个 Int32 数组中,并对数据进行自己的解释其中。例如,不要使用sentence_node 的指针链接列表,而是将所有sentence_node 项目的数据放在一个数组中,并为sentences 中的每个项目存储第一个节点的数组索引,并让每个节点保存下一个的数组索引。如果你这样做,用 COM 和 .NET 编写的代码将能够直接使用数据,而无需复制或转换它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多