首页 > Windows开发 > 详细

C# 字符串转组件名、变量名

时间:2016-11-17 17:53:38      阅读:255      评论:0      收藏:0      [点我收藏+]

字符串转组件名

(Controls["button1"] as Button).Text = "Hello";//单独组件
(Controls["tabControl1"].Controls[0].Controls["DataSource1"] as TextBox).Text = "111.111.111.111";//嵌套组件

 

字符串转变量名

string str = "demo"; //可以写到下面button1_Click里面,demo就是下面的变量名
public string demo = "Old String";
private void button1_Click(object sender, EventArgs e)
{
    //通过字符串获得变量值
    MessageBox.Show(this.GetType().GetField(str).GetValue(this).ToString());    //显示Old String
    GetType().GetField(str).SetValue(this, "New String");
    MessageBox.Show(spp);    //显示New String
    MessageBox.Show(this.GetType().GetField(str).GetValue(this).ToString());    //显示New String
}


public string Demo = "Old String";
private void button2_Click(object sender, EventArgs e)
{
    //通过字符串获得变量值
    MessageBox.Show(this.GetType().GetField("Demo").GetValue(this).ToString());    //显示Old String
    //通过给变量赋值
    this.GetType().GetField("Demo").SetValue(this, "New String");
    //新的值
    MessageBox.Show(Demo);    //显示New String
    MessageBox.Show(this.GetType().GetField("Demo").GetValue(this).ToString());    //显示New String
}

C# 字符串转组件名、变量名

原文:http://www.cnblogs.com/chinalantian/p/6074272.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!