【发布时间】:2017-05-25 18:42:28
【问题描述】:
问题出在 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!
}
【问题讨论】:
-
嗯,您正在执行参考比较。如果分配给
Value属性(或获取它)需要数组的副本,那就可以解释事情了……你能把它减少到minimal reproducible example 吗? (这里的代码很多,大部分看起来没必要复现了……) -
请提供minimal reproducible example。目前
(byte[])Dts.Variables["User::EncryptionKey"].Value == noKey和“GetEncryptionKey '失败'并返回 noKey”之间没有明确的关联 -
@bielawski
private static byte[] noKey = new byte[] { 255 };。放入Dts.Variables后,设置noKey[0] = 254。从Dts.Variables取回值,将其粘贴到本地a中,然后查看a[0]。如果是254,就是同一个数组。如果是 255,那只猪偷偷溜进了你的副本,天知道如何或为什么。在这种情况下,将 Guid 转换为您的魔法值的数组,或者只转换为byte[]并检查Length == 0,除非有效键的长度可以为零。或者它甚至 必须 是一个字节数组吗?事情是存储object,对吧? -
@bielawski 你还需要在
Main()之外比较noKey吗?我在你原来的问题中看到的可以被重写,所以密钥在Main()的本地,你将它与noKey进行比较,而不是从Dts中取出来进行比较。
标签: c# ssis script-task