簡體   English   中英

需要找到嵌套的dto對象的屬性

[英]Need to find the nested dto object's property

我有以下帶有偽數據的DTO。.我想從StandardDTO對象中找到條目列表的數量:-

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 =“ Entries” propinfo時,它在以下行失敗

var lstDTOItems = propInfo.GetValue(dtoObject)作為IList;

好。

由於以下語句,因此發生了錯誤:

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;
}

哪個會更健壯。

為什么要使用反射?

您可以簡單地寫:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM