Lets say I have a LunchBox class with a property for FreezerPack that has a property for Name.
public class LunchBox
{
public FreezerPack FreezerPack { get; set; }
}
public class FreezerPack
{
public string Name { get; set; }
}
Using reflection, I can get FreezerPack like so:
var lunchBox = new LunchBox
{
FreezerPack = new FreezerPack
{
Name = "I'm so cool"
}
};
var freezerPack = typeof(LunchBox)
.GetProperty("FreezerPack")
.GetValue(lunchBox, null);
But how can I get Name from FreezerPack in LunchBox when all I have is LunchBox as T and a string with the property path “FreezerPack.Name”? .GetProperty("FreezerPack.Name") doesn’t seem to do the trick.
No, you need to reflect on the result of the
FreezerPack: