最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I dynamically reference incremented properties in C#? - Stack Overflow

programmeradmin0浏览0评论

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#.

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#.

Share Improve this question edited May 14, 2014 at 20:56 tshepang 12.5k25 gold badges97 silver badges139 bronze badges asked Apr 12, 2010 at 15:37 Jeff BlankenburgJeff Blankenburg 7208 silver badges11 bronze badges 2
  • 1 In Javascript, that's how you could do it, but unless there's a really good reason, stay far, far away from eval. – Matt Ball Commented Apr 12, 2010 at 15:43
  • Yeah no need for eval - just use bracket notation, like myObj['reel' + tempInt]. Note if those are global properties, your object would be window - so window['reel' + tempInt] – jbyrd Commented Jul 3, 2017 at 13:15
Add a ment  | 

5 Answers 5

Reset to default 6

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].

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);

That's one of the thing you can get away with in an interpreted language like javascript, that very difficult in a piled 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;
}

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());
            }
        }
    }
}

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

发布评论

评论列表(0)

  1. 暂无评论