先给个例子:
using System;
public class MyClass 
{
public static void TestRef(ref char i) 
{
char  c = i ;
c = 'b';
} public static void TestNoRef(char i) 
{
i = 'c';
}
public static void Main() 
{ char i = 'a';    // variable must be initialized
TestRef(ref i);  // the arg must be passed as ref
Console.WriteLine(i);
TestNoRef(i);
Console.WriteLine(i);
Console.ReadLine() ;
}
}可知ref传值要想修改原值则只能在函数体内。实际问题是这样的:我有一个窗体用来修改对象的信息,直接在窗体中进行修改可以反映到对象,
但是,我有保存到文件和从文件读取对象信息的功能,在从文件中读取对象信息时,我是通过序列化
将对象还原的,这势必产生一个新对象这样配置信息就不能传递到原值中去,甚是郁闷!例如:
C为配置信息类
A中需要配置信息c
B为修改配置信息的窗体
test函数为从文件中反序列化出配置信息,在此做了简化public class C  
{
       string name ;
       public C(string name)
{
this.name = name ;
}
}public class A 
{
      C c = new C("A") ;
}
public class B 
{
C c;
        public B(ref C c)
{
this.c = c ;
}
public void Test() 
{
c = new C("test") ;
} public static void Main() 
{
A a = new A() ;
Console.WriteLine(A.c.name);
B b = new B(ref A.c) ;
B.test() ;
Console.WriteLine(A.c.name);
Console.ReadLine() ;
}
}结果为:  A
 A
怎么样才能让反序列化出来的配置信息反映到原对象中????、

解决方案 »

  1.   

    public static void TestRef(ref char i) 
    {
    char  c = i ;
    c = 'b';
    }
    =-----------------------------------
    你的例子中,没有改变传入的ref char i参数,也就体现不出你用ref的意图来,
      

  2.   

    lz 意思是想在b 中修改b的c ,同时影响A的c
      

  3.   


    B b = new B(ref A.c) ;//并没有修改A.c
    B.test() ;//同样没有修改A.c
      

  4.   

    这是因为string类型的copy问题string类型虽说属于引用类型,但是比较特殊
    对于新的修改,不会作用于老的上面。
      

  5.   

    例如:
    string strValue = "Test";
    string strRefValue = strValue;
    strValue = "NewTest";//strRefValue's value is "Test" here
      

  6.   

    hdt(倦怠) 说的很对!
    我的意思是想在b 中修改b的c ,同时影响A的c
      

  7.   

    to: string类型虽说属于引用类型,但是比较特殊楼主换成其他类型试试