lundi 25 septembre 2017

Need to find the nested dto object's property

I have the following DTO:-

public class StandardDTO
{
        public string InternalNotes { get; set; }
        public string CustomerNotes { get; set; }
        public List<Principal> Principals {get; set;}       
        public VerificationSummary VSummary { get; set; }
}

public class Principal
{
        public string PrincipalTitle { get; set; }
        public string PrincipalName { get; set; }
}

public class VerificationSummary
{
        public List<Entry> Entries { get; set; }
        public decimal GrossTotal { get; set; }
        public decimal Total { get; set; }
}

public class Entry
{
        public string PeriodName { get; set; }
        public decimal Amount { get; set; }
}

I want to find the count of the List<Entry> using StandardDTO and parentName i.e. VerificationSummary.

--int count = GetDTOObjectCount(stdDTOObject, childXElement, childXElement.Parent.Name.ToString())

public int GetDTOObjectCount<T>(T dtoObject, XElement element, string nodeName)
        {
            var dtoObjectType = dtoObject.GetType();
            var objectProperties = GetPropertyInfo(dtoObjectType);

            return GetDTOOBjectCountRecursively(objectProperties, element, element.Parent.Name.LocalName, dtoObject);
        }

        public int GetDTOOBjectCountRecursively<T>(IEnumerable<PropertyInfo> objectProperties, XElement element, string nodeName, T dtoObject)
        {
            foreach (PropertyInfo propInfo in objectProperties)
            {
                if (propInfo.Name.Equals(nodeName, StringComparison.OrdinalIgnoreCase))
                {
                    var lstDTOItems = propInfo.GetValue(dtoObject) as IList;

                    if (lstDTOItems != null)
                    {
                        return lstDTOItems.Count;
                    }
                    else
                    {
                        var objPropInfos = GetPropertyInfo(propInfo.PropertyType);
                        return GetDTOOBjectCountRecursively(objPropInfos, element, element.Name.LocalName, dtoObject);
                    }
                }
            }
            return 0;
        }

private IEnumerable<PropertyInfo> GetPropertyInfo(Type type)
        {
            return type.GetProperties();
        }

Issue is that I'm unable to loop recursively inside StandardDTO -> VerificationSummary -> Entries

It fails at the following line when propinfo = "Entries" propinfo

var lstDTOItems = propInfo.GetValue(dtoObject) as IList;





Aucun commentaire:

Enregistrer un commentaire