我正在调试一个单元测试。下面的单元测试类中的代码清楚地将一个字符串设置为值"2“。但调试器正在将其转换为(参见屏幕截图) "\0“。我将字符串值更改为具有n个字符的不同值,结果显示为n\0,即\0\0\0\0\0。
单元测试也因此而失败。
我尝试将“即时”窗口的值改回2,然后单元测试通过。
password2 = "2";
为什么Visual Studio / C#在为字符串赋值2时遇到问题?
发布于 2019-06-05 00:42:02
和开发人员谈过了,测试结果很糟糕。它也没有被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();
https://stackoverflow.com/questions/56433762
复制相似问题