指向结构数组的C#指针
嗨,我想创建一个指向结构数组的指针 我有两个这样的结构指向结构数组的C#指针,c#,pascal,C#,Pascal,嗨,我想创建一个指向结构数组的指针 我有两个这样的结构 public struct TRecord { string Key; string Value; } public struct TDB { public int Count; TRecord[] Records; } 但是,因为我正在为Delphi应用程序编写一个插件,该插件传递DBPointer pointer(DBPointer有一个数据数组),所以我需要将DBPointer指向TDB,以便从中获
public struct TRecord
{
string Key;
string Value;
}
public struct TDB
{
public int Count;
TRecord[] Records;
}
但是,因为我正在为Delphi应用程序编写一个插件,该插件传递DBPointer pointer(DBPointer有一个数据数组),所以我需要将DBPointer指向TDB,以便从中获取数据。当你想这样做的时候
public unsafe TDB* DBPointer; //DBPointer points to TDB struct which has struct array
我明白了
Error CS0208 Cannot take the address of, get the size of, or declare a pointer to a managed type
正如我读到的,我不能声明一个指向字符串和结构的指针
所以我不知道现在该怎么办,因为如果用pascal写这个,效果很好
type TRecord = record
Key: shortstring;
Value: shortstring;
end;
type TPRecord = record
Key: PChar;
Value: PChar;
end;
type TDB = record
Count: integer;
Records: array [0..255] of TRecord;
end;
PTDB = ^TDB;
function ReadValue(Key: PChar; DBPointer: PTDB): PChar; stdcall;
var a,i: integer;
SKey: shortstring;
begin
Result:='';
SKey:=shortstring(Key);
a:=DBPointer^.Count;
if (a>length(DBPointer^.Records)) then a:=length(DBPointer^.Records); //ochrana
for i:=1 to a do
begin
if (DBPointer^.Records[i-1].Key=SKey) then
begin Result:=StrPCopy(Param1,DBPointer^.Records[i-1].Value); exit; end;
end;
end;
感谢您的问候和问候您为什么要使用不安全的类型?你必须给他们一个额外的关键字,因为他们被认为是不安全的…@fredrik“我需要将DBPointer指向TDB,这样我才能从中获取数据”-与非托管代码的互操作是
不安全
代码的许多合法用途之一;当然,甚至在纯托管代码中也有一些用途(尽管Span
修复了其中的许多),所以有人知道该怎么做吗?@Persona:显然还没有。请耐心点。也许有人知道答案,也许没有。FWIW,用Delphi编写插件怎么样?这不是一个选项吗?对Pascal(Delphi)没有信心,所以如果可能的话,我宁愿用C#来做