-3

Have a large object in c# which contains many strings and integers. for example:

object.something.something = "somestring";
object.something2.something = "somestring2";

I also have a int which represents an element in the object. for example:

object.something.1 = "the content of number 1";
object.something.2 = "the content of number 2";
object.something.3 = "the content of number 3";
object.something.4 = "the content of number 4";

and so on...

if i have a variable which contains a number such as

int number = 3;

i need to use that to find the value of object.something.3

Rand Random
  • 7,300
  • 10
  • 40
  • 88
004123
  • 270
  • 1
  • 12

1 Answers1

1

Basically you have the name of a method/property, which you would like to invoke. First i would think about redesigning the program, but it you don't want to, then you could use reflection

MethodInfo method = something.getType().GetMethod(number.ToString());
string result = (string)method.Invoke(something);

if it is a property then use GetProperty(number.ToString()).GetGetMethod() instead of GetMethod()