Usa InvokeMember, con BindingFlags.GetProperty. Devi avere un riferimento all'oggetto "possedere" e devi conoscere il tipo di proprietà che stai cercando di recuperare.
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());
}
}
}
}
In Javascript, è così che si potrebbe * farlo, ma a meno che non ci sia una buona ragione, stare lontano, molto lontano da "eval". –
Sì, non c'è bisogno di eval: basta usare la notazione della parentesi, come 'myObj ['reel' + tempInt]'. Nota se queste sono proprietà globali, il tuo oggetto sarebbe 'window' - quindi' window ['reel' + tempInt] ' – jbyrd