private static int CalculateDistanceFromLogicalParent <T>(ILogical logical, int @default = -1) where T : class { var result = 0; while (logical != null && logical.GetType() != typeof(T)) { ++result; logical = logical.LogicalParent; } return(logical != null ? result : @default); }