日期:2014-05-20  浏览次数:20809 次

再问struct成员赋值问题!!!!
自定义的结构
  public   struct   test
        {
                public   String   tmp001;
                public   String   tmp002;
                public   String   tmp003;
                public   String   tmp004;
                public   String   tmp005;

        };

然后使用下面代码访问结构内成员
test   testTmp;
string   a= '1111 ';
System.Reflection.FieldInfo   myFinfo;
testTmp   =   new   test();
Type   type   =   testTmp.GetType();
myFinfo   =   type.GetField( "tmp001 ");
myFinfo.SetValue(testTmp,a);
MessageBox.Show(testTmp.tmp001);


运行出来的结果显示testTmp.tmp001的值仍然是null,请高手帮办指点一下吧.
因为是才接触C#,所以,问题如果显的弱智,就请多包含吧




------解决方案--------------------
object testTmp;
string a = "1111 ";
System.Reflection.FieldInfo myFinfo;
testTmp = new test();
//ArrayList al = new ArrayList();
//al.Add(testTmp);
Type type = testTmp.GetType();
myFinfo = type.GetField( "tmp001 ");
myFinfo.SetValue(testTmp, a);
MessageBox.Show(((test)testTmp).tmp001);
------解决方案--------------------
struct是值类型,而值类型参数默认是传值的,实际是对副本操作,并不影响原变量
------解决方案--------------------
就是要把它转成对象才行