views:

104

answers:

5

I have properties called reel1, reel2, reel3, and reel4. How can I dynamically reference these properties by just passing an integer (1-4) to my method?

Specifically, I am looking for how to get an object reference without knowing the name of the object.

In Javascript, I would do:

temp = eval("reel" + tempInt);

and temp would be equal to reel1, the object.

Can't seem to figure this simple concept out in C#.

A: 

You can access the property value by the string containing property name using PropertyInfo.

Example:

PropertyInfo pinfo = this.GetType().GetProperty("reel" + i.ToString());
return (int)pinfo.GetValue(this, null);
Vlad
+6  A: 

This is something that's typically avoided in C#. There are often other, better alternatives.

That being said, you can use Reflection to get the value of a property like this:

object temp = this.GetType().GetProperty("reel" + tempInt.ToString()).GetValue(this, null);

A better alternative, however, might be to use an Indexed Property on your class, which would allow you to do this[tempInt].

Reed Copsey
Just to reiterate, DON'T do this. Use an indexer
Allen
A: 

Try this link Get the corresponding PropertyInfo object for the property and then use GetValue on it passing it the instance on which you want to evaluate the property

Gishu
A: 

That's one of the thing you can get away with in an interpreted language like javascript, that very difficult in a compiled language like C#. Best to take another tack:

switch(tempInt)
{
    case 1:
       temp = reel1;
       break;
    case 2:
       temp = reel2;
       break;
    case 3:
       temp = reel3;
       break;
}
James Curran
A: 

Use InvokeMember, with BindingFlags.GetProperty. You must have a reference to the "owning" object, and you must know the type of the property you're trying to retrieve.

namespace Cheeso.Toys
{
    public class Object1
    {
        public int Value1 { get; set; }
        public int Value2 { get; set; }
        public Object2 Value3 { get; set; }
    }

    public class Object2
    {
        public int Value1 { get; set; }
        public int Value2 { get; set; }
        public int Value3 { get; set; }
        public override String ToString()
        {
            return String.Format("Object2[{0},{1},{2}]", Value1, Value2, Value3);
        }
    }

    public class ReflectionInvokePropertyOnType
    {

        public static void Main(string[] args)
        {
            try
            {
                Object1 target = new Object1
                    {
                        Value1 = 10, Value2 = 20, Value3 = new Object2
                            {
                                Value1 = 100, Value2 = 200, Value3 = 300
                            }
                    };

                System.Type t= target.GetType();

                String propertyName = "Value3";

                Object2 child = (Object2) t.InvokeMember (propertyName,
                                                          System.Reflection.BindingFlags.Public |
                                                          System.Reflection.BindingFlags.Instance  |
                                                          System.Reflection.BindingFlags.GetProperty,
                                                          null, target, new object [] {});
                Console.WriteLine("child: {0}", child);
            }
            catch (System.Exception exc1)
            {
                Console.WriteLine("Exception: {0}", exc1.ToString());
            }
        }
    }
}
Cheeso