淡水求咸

博客园已经停止更新,请移步 http://lovecjh.com/

导航

将字符串转为变量名(C#)

问题提出:  

string str = "spp";
string spp = "very good"; 

  如何输出str的值是“very good”?

 

精彩回答:

  public partial class Form1 : Form
    {
        string str = "spp";
        public string spp = "very good";

        public Form1()
        {
            InitializeComponent();

            MessageBox.Show(this.GetType().GetField(str).GetValue(this).ToString());
        }
    }

 

原帖链接在此。

 

posted on 2013-01-11 17:50  深圳彦祖  阅读(255)  评论(0编辑  收藏  举报