C# 无法设置字符串值 - 而不是设置 \0
C# failing to set string value - instead setting \0
我正在调试单元测试。下面单元测试 class 中的代码清楚地将字符串设置为值“2”。但是调试器正在将它翻译成(见截图)“\0”。我将字符串值更改为具有 n 个字符的不同值,它显示为 n \0's。 IE。 [=22=][=22=][=22=][=22=][=22=][=22=]。
单元测试也因此失败。
我试过将立即数window的值改回2,然后单元测试通过。
password2 = "2";
为什么 Visual Studio / C# 无法将值 2 分配给字符串?
与开发人员交谈后发现测试很糟糕。它也没有被 CI 选中,所以这是一场完美的风暴。测试是使用 RtlZeroMemory 将内存引用归零,这是我以前从未见过的。
感谢所有花时间调查的人。
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("...")]
[ComVisible(true)]
public class FileEncryption
{
[DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
public static extern bool ZeroMemory(IntPtr Destination, int Length);
public void Decrypt(string filenamewithpath, string password)
{
// We manually control memory and free when it is completed
GCHandle gch = GCHandle.Alloc(password, GCHandleType.Pinned);
try
{
...
ZeroMemory(gch.AddrOfPinnedObject(), password.Length * 2);
}
catch (Exception ex)
{
throw ex;
}
finally
{
gch.Free();
我正在调试单元测试。下面单元测试 class 中的代码清楚地将字符串设置为值“2”。但是调试器正在将它翻译成(见截图)“\0”。我将字符串值更改为具有 n 个字符的不同值,它显示为 n \0's。 IE。 [=22=][=22=][=22=][=22=][=22=][=22=]。
单元测试也因此失败。
我试过将立即数window的值改回2,然后单元测试通过。
password2 = "2";
为什么 Visual Studio / C# 无法将值 2 分配给字符串?
与开发人员交谈后发现测试很糟糕。它也没有被 CI 选中,所以这是一场完美的风暴。测试是使用 RtlZeroMemory 将内存引用归零,这是我以前从未见过的。
感谢所有花时间调查的人。
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("...")]
[ComVisible(true)]
public class FileEncryption
{
[DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
public static extern bool ZeroMemory(IntPtr Destination, int Length);
public void Decrypt(string filenamewithpath, string password)
{
// We manually control memory and free when it is completed
GCHandle gch = GCHandle.Alloc(password, GCHandleType.Pinned);
try
{
...
ZeroMemory(gch.AddrOfPinnedObject(), password.Length * 2);
}
catch (Exception ex)
{
throw ex;
}
finally
{
gch.Free();