這一個真的讓我難住。我正在和另一位打電話給我的開發者合作,因爲他無法相信他所看到的。我們一起調試了一下,我也看到了,沒有任何解釋。這是場景。他正在寫的是通過一個自動生成的COM包裝第三方COM對象進行交互的方法(通過添加COM組件作爲參考只是產生這是他的方法的頂部:如何設置一個c#方法範圍變量影響另一個?
public bool RefolderDocument(ref IManDocument oDoc)
{
string strCustom1 = (string) oDoc.GetAttributeValueByID(imProfileAttributeID.imProfileCustom1);
string strCustom2 = (string) oDoc.GetAttributeValueByID(imProfileAttributeID.imProfileCustom2);
的目的代碼是從「文檔」對象(oDoc)中獲取項目編號和子項目編號。
以下是您逐步完成後會發生的情況。第一個分配strCustom1具有預期值「32344」(項目編號)和strCustom2如預期的那樣爲空。第二次分配後,strCustom2獲取子項目編號「0002」 - ,但strCustom1已更改爲32334 - 一個字符r已經改變了!?
它讓我覺得某種古老的c語言堆棧溢出(即使它與COM組件互操作,我也不會期望在託管應用程序中)。我們都很困惑。在試圖破解解決這個bizarreness,我想第一個字符串的內容複製到其他位置,像這樣:
public bool RefolderDocument(ref IManDocument oDoc)
{
string strCustom1 = string.Copy((string)oDoc.GetAttributeValueByID(imProfileAttributeID.imProfileCustom1));
string strCustom2 = string.Copy((string)oDoc.GetAttributeValueByID(imProfileAttributeID.imProfileCustom2));
相同的結果!在這一點上,我們一直在抓住精力,將代碼從.NET 4移到.NET 3.5(CLR 2),但沒有任何改變。一個可能相關的觀點是,這是一項服務,我們正在關注服務流程。該版本針對x86,並且服務位置肯定位於Debug輸出構建文件夾中。
這是否有任何合理的解釋?我很難理解如何繼續。
'string.Copy'是他試圖解決這個奇怪的問題... – 2011-03-31 15:47:51
是的。 String.Copy嘗試在源位置被覆蓋之前移動strCustom1實際指向的位置。我預計這會起作用。它沒有。 – 2011-03-31 16:40:15