是否可以将字符串转换为代码?
例如,代替:
string foo = "bar";
...
switch(foo){
case "bar":
pictureBox.Image = Project.Properties.Resources.bar;
break;
...
}
有没有一种方法可以简单地:
string foo = "bar"
pictureBox.Image = Project.Properties.Resources.<foo string goes in here>;
我希望这个例子有意义。
您尝试做的是在C#中称为“反射”。您可以在StackOverflow中找到包含其代码示例的帖子:使用C#中的反射从字符串获取属性值
编辑:这是示例
string foo = "bar";
var resources = Project.Properties.Resources;
object o = resources.GetType().GetProperty(foo).GetValue(resources, null);
if (o is System.Drawing.Image) {
pictureBox.Image = (System.Drawing.Image) o;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句