c# - 如何将字符串转换为代码? (C#)

标签 c# string picturebox

是否可以将字符串转换为代码?
例如,而不是:

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 中找到一个带有代码示例的帖子:Get property value from string using reflection in 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;
     }

关于c# - 如何将字符串转换为代码? (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27873562/

相关文章:

c# - MEF 导入不适用于外部组件

c# - Xunit.Assert.Collection 中的问题 - C#

java - 在 Java 中拆分包含 (

vb6 - 如何在 Visual Basic 6 PictureBox 中制作非常小的字体大小?

c# - 在 Visual Studio 中使用 PictureBox

c# - 如何仅保存图片框中显示的图像

c# - 将sql命令写入文本文件

c# - 难以理解 Silverlight 和 SQL

string - 调用 C# 代码时,PowerShell $null 不再为 null

string - 如何在 MATLAB 中构造 "excel-friendly"字符串?