Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through nested properties of an object

Tags:

c#

.net

I am trying to loop through all properties in an object including nested objects and objects in collections, to check if the property is of DateTime data type. If it is, convert the value to UTC time leaving everything intact including the structure and the value of other properties untouched. The structure of my classes as follows:

public class Custom1 {
    public int Id { get; set; }
    public DateTime? ExpiryDate { get; set; }
    public string Remark { get; set; }
    public Custom2 obj2 { get; set; }
}

public class Custom2 {
    public int Id { get; set; }
    public string Name { get; set; }
    public Custom3 obj3 { get; set; }
}

public class Custom3 {
    public int Id { get; set; }
    public DateTime DOB { get; set; }
    public IEnumerable<Custom1> obj1Collection { get; set; }
}

public static void Main() {
    Custom1 obj1 = GetCopyFromRepository<Custom1>();

    // this only loops through the properties of Custom1 but doesn't go into properties in Custom2 and Custom3
    var myType = typeof(Custom1);
    foreach (var property in myType.GetProperties()) {
        // ...
    }
}

How do I loop through the properties in obj1 and traverse further down obj2 then obj3 then obj1Collection? The function have to be generic enough because the Type passed to the function cannot be determined at design/compile time. Conditional statements to test for Types should be avoided since they might be class Custom100

//avoid conditional statements like this
if (obj is Custom1) {
    //do something
} else if (obj is Custom2) {
    //do something else
}  else if (obj is Custom3) {
    //do something else
} else if ......
like image 500
Twisted Whisper Avatar asked Oct 06 '14 16:10

Twisted Whisper


2 Answers

This is not a complete answer but I would start from here.

var myType = typeof(Custom1);
            ReadPropertiesRecursive(myType);


private static void ReadPropertiesRecursive(Type type)
        {
            foreach (PropertyInfo property in type.GetProperties())
            {
                if (property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime?))
                {
                    Console.WriteLine("test");
                }
                if (property.PropertyType.IsClass)
                {
                    ReadPropertiesRecursive(property.PropertyType);
                }
            }
        }
like image 134
Sunny Avatar answered Oct 17 '22 15:10

Sunny


I think it's far from the original question but ReadPropertiesRecursive above fall in infinite loop on

class Chain { public Chain Next { get; set; } }

I'd rather propose version with accumulation

private static void ReadPropertiesRecursive(Type type, IEnumerable<Type> visited)
{
    foreach (PropertyInfo property in type.GetProperties())
    {
        if (property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime?))
        {
            Console.WriteLine("test");
        }
        else if (property.PropertyType.IsClass && !visited.Contains(property.PropertyType))
        {
            ReadPropertiesRecursive(property.PropertyType, visited.Union(new Type[] { property.PropertyType }));
        }
    }
}
like image 24
elpolaco Avatar answered Oct 17 '22 17:10

elpolaco