CSharp · Console
using System;
public class Program
{
public void Main()
{
First blank = new First();
First populated = new First(true);
int value = NestedProperty<First,int>(blank,f => f.Second.Third.id);
Console.WriteLine(value);
value = NestedProperty<First,int>(populated,f => f.Second.Third.id);
Console.WriteLine(value);
}
public E NestedProperty<T,E>(T Parent, Func<T,E> Path, E IfNullOrEmpty = default(E))
{
try
{
return Path(Parent);
}
catch
{
return IfNullOrEmpty;
}
}
}
public class First
{
public Second Second { get; set; }
public int id { get; set; }
public First(){}
public First(bool init)
{
this.id = 1;
this.Second = new Second();
}
}
public class Second
{
public Third Third { get; set; }
public int id { get; set; }
public Second()
{
this.id = 1;
this.Third = new Third();
}
}
public class Third
{
public int id { get; set; }
public Third()
{
this.id = 1;
}
}