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

        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());
        }

  

 

相关文章:

  • 2022-01-06
  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-31
  • 2022-02-10
猜你喜欢
  • 2022-02-24
  • 2021-09-29
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2021-08-08
相关资源
相似解决方案