2017-09-25 383 views
0

我有以下的DTO与虚拟data..I想找出条目列表从StandardDTO对象计数: -需要找到嵌套的DTO对象的属性

public class StandardDTO 
{ 
    public string InternalNotes { get; set; } 
    public string CustomerNotes { get; set; } 
    public List<Principal> Principals { get; set; } 
    public VerificationSummary VerificationSummary{ 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; } 
} 

void Main() 
{ 
    // Need to populate stdDTOObject and childXElement 
    int count = GetDTOObjectCount(GetStandardDTOObject(), "VerificationSummary"); 
    count.Dump(); 
} 

public StandardDTO GetStandardDTOObject() 
{ 
    StandardDTO stdDTOObj = new StandardDTO(); 
    stdDTOObj.InternalNotes = "InternalNotes"; 
    stdDTOObj.CustomerNotes = "CustomerNotes"; 

    List<Principal> lstPrincipal = new List<Principal>(); 
    Principal pObj = new Principal(); 
    pObj.PrincipalTitle = "Mr"; 
    pObj.PrincipalName = "ABC"; 
    lstPrincipal.Add(pObj); 

    pObj = new Principal(); 
    pObj.PrincipalTitle = "Mrs"; 
    pObj.PrincipalName = "XYZ"; 
    lstPrincipal.Add(pObj); 

    stdDTOObj.Principals = lstPrincipal; 

    VerificationSummary vs = new VerificationSummary(); 
    List<Entry> lstEntry = new List<Entry>(); 
    Entry entry = new Entry(); 
    entry.PeriodName = "Sept17"; 
    entry.Amount = 1212; 
    lstEntry.Add(entry); 

    entry = new Entry(); 
    entry.PeriodName = "Oct17"; 
    entry.Amount = 12000; 
    lstEntry.Add(entry); 

    entry = new Entry(); 
    entry.PeriodName = "Nov17"; 
    entry.Amount = 1000; 
    lstEntry.Add(entry); 

    entry = new Entry(); 
    entry.PeriodName = "Dec17"; 
    entry.Amount = 2000; 
    lstEntry.Add(entry); 

    entry = new Entry(); 
    entry.PeriodName = "Jan18"; 
    entry.Amount = 2000; 
    lstEntry.Add(entry); 

    vs.Entries = lstEntry; 
    vs.GrossTotal = 5555; 
    vs.Total = 10000; 
    stdDTOObj.VerificationSummary = vs; 
    return stdDTOObj; 
} 

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

    return GetDTOOBjectCountRecursively(objectProperties, nodeName, dtoObject); 
} 

public int GetDTOOBjectCountRecursively<T>(IEnumerable<PropertyInfo> objectProperties, 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); 
       //hardcoded the nodeName just for this test. 
       return GetDTOOBjectCountRecursively(objPropInfos, "Entries", dtoObject); 
      } 
     } 
    } 
    return 0; 
} 

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

的问题是,我无法循环内递归 StandardDTO - > VerificationSummary - >条目

它未能在下面的行当propinfo = “条目” propinfo

变种lstDTOItems = propInfo.GetValue(dtoObject)作为IList的;

+0

请发表您的意见/建议。它非常紧迫。 – user1581860

+0

我试图在.Net Core 1.1 – user1581860

回答

0

好的。

所以错误发生在由于以下语句:

return GetDTOOBjectCountRecursively(objPropInfos, "Entries", dtoObject); 

您需要发送,你进步了,当你发送“条目”像这样的新对象:

return GetDTOOBjectCountRecursively(objPropInfos, "Entries", propInfo.GetValue(dtoObject)); 

这应该可以解决问题。

此外,为了检查一个泛型列表,而不是做这样的:

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

您可以使用此方法:

private bool IsList(Object obj) 
{ 
    return obj is IList && obj.GetType().IsGenericType; 
} 

这将更加强劲。

+0

Thankyou Saurabh,它的工作就像一个魅力! – user1581860

0

为什么你使用反射?

,你可以简单地写:

var obj = GetStandardDTOObject(); 
var count = obj.VerificationSummary.Entries.Count();