首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

再问struct成员赋值有关问题!

2011-12-22 
再问struct成员赋值问题!!!!自定义的结构publicstructtest{publicStringtmp001publicStringtmp002public

再问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是值类型,而值类型参数默认是传值的,实际是对副本操作,并不影响原变量
[解决办法]
就是要把它转成对象才行

热点排行