private static int? GetUserIdProperty(object src)
{
var properties = src.GetType().GetProperties();
if (src.GetType() == typeof (User))
{
var userIdAsProperty = GetPropValue(src, "Id");
return (int)userIdAsProperty;
}
foreach (var property in properties)
{
if (property.Name == "User" || property.Name == "Leader")
{
var userPropName = property.Name;
var userValue = GetPropValue(src, userPropName);
if (userValue == null) return null;
var userIdFromObject = GetPropValue(userValue, "Id");
if (userIdFromObject == null) return null;
return (int)userIdFromObject;
}
if (property.Name == "UserId")
{
var userIdAsProperty = GetPropValue(src, "UserId");
return (int) userIdAsProperty;
}
var propertyValue = GetPropValue(src, property.Name);
if (propertyValue == null
|| propertyValue is string
|| propertyValue is DateTime
|| propertyValue.GetType().IsPrimitive
|| propertyValue is IEnumerable
|| propertyValue.GetType().IsArray) continue;
var objectClass = GetPropValue(src, property.Name);
var recursiveResult = GetUserIdProperty(objectClass);
return recursiveResult;
}
return null;
}