Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中动态引用递增的属性?_C#_Javascript_Reflection - Fatal编程技术网

如何在C#中动态引用递增的属性?

如何在C#中动态引用递增的属性?,c#,javascript,reflection,C#,Javascript,Reflection,我有称为reel1、reel2、reel3和reel4的属性。如何通过向方法传递一个整数(1-4)来动态引用这些属性 具体来说,我正在寻找如何在不知道对象名称的情况下获取对象引用 在Javascript中,我将执行以下操作: temp = eval("reel" + tempInt); temp将等于对象reel1 似乎无法在C#中理解这个简单的概念 您可以使用包含属性名称的字符串访问属性值 例如: PropertyInfo pinfo = this.GetType().GetProperty

我有称为reel1、reel2、reel3和reel4的属性。如何通过向方法传递一个整数(1-4)来动态引用这些属性

具体来说,我正在寻找如何在不知道对象名称的情况下获取对象引用

在Javascript中,我将执行以下操作:

temp = eval("reel" + tempInt);
temp将等于对象reel1


似乎无法在C#中理解这个简单的概念

您可以使用包含属性名称的字符串访问属性值

例如:

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

这是C#中通常避免的事情。通常还有其他更好的选择

也就是说,您可以使用反射获得如下属性的值:

object temp = this.GetType().GetProperty("reel" + tempInt.ToString()).GetValue(this, null);
但是,更好的选择可能是在类上使用,这将允许您执行
此[tempInt]

尝试
获取属性对应的PropertyInfo对象,然后对其使用GetValue传递您要计算属性的实例,这是在javascript等解释语言中可以避免的事情之一,在C#等编译语言中非常困难。最好采取另一种策略:

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

将InvokeMember与BindingFlags.GetProperty一起使用。您必须具有对“拥有”对象的引用,并且必须知道您试图检索的属性的类型

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

在Javascript中,这就是你可以做到的,但除非有真正好的理由,否则远离
eval
。是的,不需要eval-只需使用括号符号,比如
myObj['reel'+tempInt]
。请注意,如果这些是全局属性,则对象将是
window
-因此
window['reel'+tempInt]
只是重申一下,不要这样做。使用索引器