如何设置一个c#方法范围的变量影响另一个?

这个真让我难过.我和另一位打电话给我的开发商合作,因为他无法相信他所看到的.我们一起调试了调试器,我也看到了它并没有解释.这是场景.他正在编写一个通过自动生成的COM包装器与第三方COM对象进行交互的方法(仅通过添加COM组件作为参考生成.这是他的方法的顶部:
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 – 一个字符已被更改!?

它让我感到震惊,因为某种老式的c语言堆栈溢出(即使它与COM组件进行互操作,我也不希望在托管应用程序中使用它).我们都感到困惑.为了破解这种奇怪的现象,我尝试将第一个字符串的内容复制到另一个位置,如下所示:

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输出构建文件夹中.

这有什么合理的解释吗?我很难过如何继续.

解决方法

看起来strCustom1和strCustom2都被设置为引用,返回GetAttributeValueByID的结果.我知道不是为什么,而且看看这里的其他人(Paging Dr. Skeet lol ……)是否可以.

在短期内,我认为你会发现这将为你解决问题……

public bool RefolderDocument(ref IManDocument odoc)
    {
        string strCustom1 = "" + string.copy((string)odoc.GetAttributeValueByID(imProfileAttributeID.imProfileCustom1));
        string strCustom2 = "" + string.copy((string)odoc.GetAttributeValueByID(imProfileAttributeID.imProfileCustom2));

我的想法基本上是让它评估表达而不是直接使用引用…

马丁.

PS. string.copy()有什么用?

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...