I have following class hierarichy
class firstlevel
{
public secondlevel sl { get; set; }
}
class secondlevel
{
public string Name { get; set; }
}
There is an object created of firstlevel and the Name is set to sandy. var fl = new firstlevel { sl = new secondlevel { Name = "sandy" } };
This is and example , I will not know the Name in real senario , but I know this class hierarichy.
I need to write a method to get value of Name
By reading lot of stuff I've written following code, but surprisingly it gives me name of the property not its value, I am wondering what is wrong with my code, can anybody figure it out.
public static Object GetValue()
{
var fl = new firstlevel { sl = new secondlevel { Name = "sandy" } };
Object obj = fl;
const string path = "fl.sl.Name";
String[] part = path.Split('.');
Type type = obj.GetType();
string firstPart = part[0];
string secondpart = part[1];
string thirdpart = part[2];
PropertyInfo info = type.GetProperty(secondpart);
if (info == null) { return null; }
PropertyInfo info1 = info.GetType().GetProperty(thirdpart);
if (info1 == null) { return null; }
return info1.GetValue(info1, null);
}