locked
Traversing the object graph C# RRS feed

  • Question

  • User-963208184 posted

    Hi All,

    I have an object that has a number of properties and other objects hanging off it, the child objects may also have properties and more objects on them.

    I want a generic method to traverse the parent object and child objects looking for particular properties. How can I do this with Generics?

    Wednesday, August 28, 2013 6:54 AM

All replies

  • User197322208 posted

    search for C# reflection to find names of properties .

    However, you sdhould be aware of recursion( object a have a list of object b that have as parent the object a that ..)

    Wednesday, August 28, 2013 7:35 AM
  • User-963208184 posted
    public static T SetCreated<T>() where T: class
            {
                Type t = T;
                foreach (PropertyInfo p in t.GetProperties())
                {
                    if (p.Name == "CreatedBy").........
                    
                }
            }

    What should I set Type t To (bold)???

    Wednesday, August 28, 2013 9:09 AM
  • User197322208 posted

    Type t = T;
    

    typeof(T)

    But I think that is better have an interface

    ICreated

    that have CreatedBy as property ( and T implement ICreated)

     

    Wednesday, August 28, 2013 11:32 AM