Hi friends,
Getting straight to the question....
I understand that I can access property like
objA.Prop1.....through reflection as,
Type myType = objA.GetType();
PropertyInfo myProperty = myType.GetProperty("Prop1");
But suppose I want to access property like
objA.Prop1.Prop2 ...then how to retrive that value and also how to set that using reflection.
Thanks !!
AlanPosted Oct 25, 2007, 3:50 PM
This might be closer to what you're looking for:
using System;
using System.Reflection;
class Test
{
static void Main()
{
MyClass objA = new MyClass();
ShowPropertyValue(objA, "Prop1", "Prop3", 42);
Console.WriteLine();
ShowPropertyValue(objA, "Prop2", "Prop4", "Goodbye");
Console.ReadLine();
}
static void ShowPropertyValue(object objT, string prop1, string prop2, object newValue)
{
Type myType = objT.GetType(); //gets runtime type
Console.WriteLine("objT is of type {0}", myType);
BindingFlags bf = BindingFlags.Public | BindingFlags.Instance |
BindingFlags.GetProperty;
PropertyInfo prop1Info = myType.GetProperty(prop1, bf);
if (prop1Info == null)
{
Console.WriteLine("There is no public instance property called {0} in the {1} type", prop1, myType);
return;
}
object result = myType.InvokeMember(prop1, bf, null, objT, null);
Type prop1Type = prop1Info.PropertyType;
PropertyInfo prop2Info = prop1Type.GetProperty(prop2, bf);
if (prop2Info == null)
{
Console.WriteLine("There is no public instance property called {0} in the {1} type", prop2, prop1Type);
return;
}
object result2 = prop1Type.InvokeMember(prop2, bf, null, result, null);
Console.WriteLine("Value of objT.{0}.{1} is {2}", prop1, prop2, result2);
prop2Info.SetValue(result, newValue, null);
result2 = prop2Info.GetValue(result, null);
Console.WriteLine("Value of objT.{0}.{1} is now {2}", prop1, prop2, result2);
}
}
class MyClass
{
YourClass yourClass = new YourClass();
HisClass hisClass = new HisClass();
public YourClass Prop1
{
get { return yourClass; }
set { yourClass = value; }
}
public HisClass Prop2
{
get { return hisClass; }
set { hisClass = value; }
}
}
class YourClass
{
int myInt = 3; // default value
public int Prop3
{
get { return myInt; }
set { myInt = value; }
}
}
class HisClass
{
string myString = "Hello"; // default value
public string Prop4
{
get { return myString; }
set { myString = value; }
}
}
YashPosted Oct 25, 2007, 11:26 AM
That was really good !! But the actual issue is I dont Know about type of the object..ie it could be objA,objB means can be of any class.Lets suppose I have 3 classes A,B,C.
Now, I will know about the type of class only at run time..which i supposed in my original query as of class A.Then writing code like,
int value = (int)prop2Info.GetValue(objA.Prop1, null);......wud not be possible as I cannot dynamically write objA.Prop1 Since during design time I wud be having only object of type Object as
Object objT = new object() and not
MyClass objA = new MyClass();
Hope I made you understand my problem.
AlanPosted Oct 24, 2007, 10:49 AM
Here's a quick example of how you can do this:
using System;
using System.Reflection;
class Test
{
static void Main()
{
MyClass objA = new MyClass();
Type myType = objA.GetType();
PropertyInfo prop1Info = myType.GetProperty("Prop1");
Type prop1Type = prop1Info.PropertyType;
PropertyInfo prop2Info = prop1Type.GetProperty("Prop2");
int value = (int)prop2Info.GetValue(objA.Prop1, null);
Console.WriteLine("Value of Prop2 is {0}", value);
prop2Info.SetValue(objA.Prop1, 42, null);
value = (int)prop2Info.GetValue(objA.Prop1, null);
Console.WriteLine("Value of Prop2 is now {0}", value);
Console.ReadLine();
}
}
class MyClass
{
YourClass yourClass = new YourClass();
public YourClass Prop1
{
get { return yourClass; }
set { yourClass = value; }
}
}
class YourClass
{
int myInt = 3; // default value
public int Prop2
{
get { return myInt; }
set { myInt = value; }
}
}