1

问题出在 Main 中间的那行

if ((byte[])Dts.Variables["User::EncryptionKey"].Value == noKey)

当 GetEncryptionKey“失败”并返回 noKey 时,“if”仍然采用“else”路径,我不明白为什么。我尝试了这个,结果相同。

if (noKey.Equals((byte[])Dts.Variables["User::EncryptionKey"].Value))

除非对 noKey 的每个引用都以某种方式实例化 byte[0] 的新副本,否则我看不出它们如何不相等。我已经走过无数次了,它们看起来确实是一样的。

    private static byte[] noKey = new byte[0];

    public void Main()
    {
        int keyLen = 32;
            Dts.Variables["User::EncryptionKey"].Value =
                GetEncryptionKey((string)Dts.Variables["User::EncryptionKeyAsHex"].Value, keyLen);

        if ((byte[])Dts.Variables["User::EncryptionKey"].Value == noKey)
        {
            Dts.TaskResult = (int)ScriptResults.Failure;
        }
        else
        {
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }

    private static byte[] GetEncryptionKey(string hexString,int numBytes)
    {
            return noKey;  //<-this definitely does get hit!
    }
4

2 回答 2

1

这就是问题所在:“除非对 noKey 的每个引用都以某种方式实例化 byte[0] 的新副本,否则我看不出它们如何不相等。” 我从来没有想过 Dts....Value.set 是创建新副本的那个。

所以感谢 Ed Plunkett 建议我调查一下,因为事实证明这是真的。

将返回值存储在局部变量中以用于比较可以避免该问题。

于 2017-05-25T20:44:40.300 回答
0

我认为您在这里混淆了这个概念,两个数组不相同,因为它们共享相同的元素。运算符 == 调用 ReferenceEquals,它检查两个元素是否指向内存上的相同空间。

看看这里C# .Equals()、.ReferenceEquals() 和 == 运算符的区别

尝试使用@Eris 方法。希望这有帮助

于 2017-05-25T19:10:02.737 回答