簡體   English   中英

C# 對 List<> 屬性的屬性值的遞歸迭代的轉換

[英]C# Conversion of recursive iteration of properties' values for List<> property

我有以下方法用於遍歷我的類中的所有屬性(只是屬性),並在屏幕上打印它們的名稱和它們保存的值(如果有)。

        public static void PrintProperties(object obj, int indent = 1)
        {
        if ( obj == null )
            return;
        string indentString = new string (' ', indent);

        Type objType = obj.GetType ();
        PropertyInfo[] properties = objType.GetProperties ();

        foreach ( PropertyInfo property in properties )
        {
            object propValue = property.GetValue (obj, null);
            var elems = propValue as IList;
            if ( elems != null )
            {
                foreach ( var item in elems )
                {
                    PrintProperties (item, indent + 3);
                }
            }
            else
            {
                if ( property.PropertyType.Assembly == objType.Assembly )
                {
                    Console.WriteLine("{0}{1}:", indentString, property.Name);

                    PrintProperties (propValue, indent + 2);
                }
                else
                {
                    Console.WriteLine("{0}{1}: {2}", indentString, property.Name, propValue);
                }
            }
            }

不幸的是,我將我的一個類中的一個屬性從 string 更改為List<string>以適應我必須在那里分配的多個值,現在我收到錯誤System.Reflection.TargetParameterCountException: 'Parameter count mismatch.' 我不知道如何解決,很可能是因為我有一個 List 屬性。 我該如何解決,以便當我遇到這樣的屬性時,列出它的所有值? 有人可以幫我嗎?

你必須做三件事來修復你的實現:

  1. 專門處理字符串(因為否則它們將在您的實現中被視為字符數組)。
  2. 專門處理數組(否則你會看到你看到的異常)。
  3. 打印出每個項目的值,而不僅僅是它的屬性。 否則,如果該項目是一個int那么它就不會被打印出來。

沿着這些路線的東西應該工作:

public static void PrintProperties(object obj, int indent = 1)
{
    if (obj == null)
        return;

    string indentString = new string(' ', indent);

    Type objType    = obj.GetType();
    PropertyInfo[] properties = objType.GetProperties();

    foreach (PropertyInfo property in properties)
    {
        object propValue;

        if (objType == typeof(string))
            return; // Handled at a higher level, so nothing to do here.

        if (property.PropertyType.IsArray)
            propValue = (Array)property.GetValue(obj);
        else
            propValue = property.GetValue(obj, null);

        var elems = propValue as IList;

        if (elems != null)
        {
            Console.WriteLine("{0}{1}: IList of {2}", indentString, property.Name, propValue.GetType().Name);

            for (int i = 0; i < elems.Count; ++i)
            {
                Console.WriteLine("{0}{1}[{2}] == {3}", indentString, property.Name, i, elems[i]);

                if (objType != typeof(string))
                    PrintProperties(elems[i], indent + 3);
            }
        }
        else
        {
            if (property.PropertyType.Assembly == objType.Assembly)
            {
                Console.WriteLine("{0}{1}:", indentString, property.Name);

                PrintProperties(propValue, indent + 2);
            }
            else
            {
                Console.WriteLine("{0}{1}: {2}", indentString, property.Name, propValue);
            }
        }
    }
}

您可能希望調整輸出以適合您實際想要的內容,但這應該為您提供要點。

發生錯誤的這一行中的問題:

object propValue = property.GetValue (obj, null);

與列表本身無關。 嘗試枚舉string上的所有屬性是您的方法的遞歸性質。 一個簡單的解決方案,如果您只想打印列表的值,只需將枚舉列表的部分更改為:

var elems = propValue as IList;
if ( elems != null )
{
     Console.WriteLine("{0}{1}:", indentString, property.Name);
     foreach ( var item in elems )
     {
            Console.WriteLine("{0}{1}",new string (' ', indent+2),item);
     }
}

https://dotnetfiddle.net/NBOA4u

暫無
暫無

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

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