zoukankan      html  css  js  c++  java
  • 用字符串的方式获取变量名

    this.GetType().GetField("testString").GetValue(this)

    转自http://www.cnblogs.com/yinxiangpei/articles/2623597.html

    例子一、将字符串转为变量名

    复制代码
            string str = "spp";
            public string spp = "very good";
            private void button1_Click(object sender, EventArgs e)
            {           
                MessageBox.Show(this.GetType().GetField(str).GetValue(this).ToString());
            }
    复制代码

    例子二、通过字符串给变量赋值

    复制代码
      public string gisoracle = "ok";
    
            private void button2_Click(object sender, EventArgs e)
            {
    
                //通过字符串获得变量值
                MessageBox.Show(this.GetType().GetField("gisoracle").GetValue(this).ToString());
                //通过给变量赋值
                this.GetType().GetField("gisoracle").SetValue(this, "gisoracle@126.com");
                //新的值
                MessageBox.Show(this.GetType().GetField("gisoracle").GetValue(this).ToString());
            }
    复制代码
  • 相关阅读:
    第五周总结
    第四周总结
    关于“模仿"和”创新“
    第三周总结
    第九周总结
    第八周总结
    第六周总结
    中国历史上成功的两人合作
    第五周总结
    第四周总结
  • 原文地址:https://www.cnblogs.com/vsdog/p/6922421.html
Copyright © 2011-2022 走看看