lundi 23 avril 2018

Using reflection return a list of objects from Property.GetValue

I need to retrieve a list of objects when using the PropertyInfo.GetValue method. I do not know what type of list it will be. My code looks like this:

    public IEnumerable<Error> FindErrors(object obj)
    {
        var errors = new List<Error>();

        errors.AddRange(Validate(obj));


        List<PropertyInfo> properties = obj.GetType().GetProperties().Where(p => !p.PropertyType.IsByRef).ToList();


        foreach (var p in properties)
        {
            if (IsList(p))
            {
                // This line is incorrect?  What is the syntax to get this to be correcT?????
                List<object> objects = p.GetValue(obj, null);

                foreach (object o in objects)
                {
                    errors.AddRange(FindErrors(o));
                }
            }
            else
            {
                errors.AddRange(FindErrors(p.GetValue(obj, null)));
            }
        }


        return errors;
    }

My problem is I am not sure what the syntax should be to get that List because that line of code is currently incorrect. How can I get that list of objects?





Aucun commentaire:

Enregistrer un commentaire