2

Is it possible to convert a string to code?
For example, instead of:

string foo = "bar";
...
switch(foo){
case "bar":
    pictureBox.Image = Project.Properties.Resources.bar;
    break;
...
}

Is there a way to simply:

string foo = "bar"
pictureBox.Image = Project.Properties.Resources.<foo string goes in here>;

I hope this example makes sense.

Tom
  • 195
  • 2
  • 12
  • http://stackoverflow.com/questions/17828774/get-resources-with-string – prospector Jan 10 '15 at 06:42
  • possible duplicate of [Get property value from string using reflection in C#](http://stackoverflow.com/questions/1196991/get-property-value-from-string-using-reflection-in-c-sharp) – mleko Jan 10 '15 at 08:07

1 Answers1

4

What you are trying to do is called Reflection in C#. You can find a post in StackOverflow with code samples for it: Get property value from string using reflection in C#

EDIT: Here is the example

     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;
     }
Community
  • 1
  • 1
Julian J. Tejera
  • 1,015
  • 10
  • 17