我想使用pinvoke来获取当前的工作目录,所以别无选择,所以我选择添加这样写的GetCurrentDirectory
:
using System.Runtime.InteropServices;
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[DllImport("kernel32.dll")]
public static extern uint GetCurrentDirectory(int bufferLength, [Out] char[] buffer);
这很好用,但是我想将char[] buffer
更改为其他内容,我知道System.Text.StringBuilder
可以,但是我真的想尝试自己制作一些东西,因此,我创建了一个测试:
[StructLayout(LayoutKind.Sequential)]
public class CharCollection
{
public CharCollection(int size)
{
_value = new char[size];
}
public override string ToString()
{
//converts _value to string and removes all ctrl chars and unused chars.
}
internal string ToString(int startIndex, uint trueLength)
{
//converts _value to string starting from startIndex and the length of trueLength
}
internal char[] _value;
}
并将char[] buffer
更改为CharCollection buffer
,但是它不起作用,抛出了SafeArrayTypeMismatchException
,我不明白为什么。帮助将不胜感激。