reflection on List and printing values

前端 未结 6 866
眼角桃花
眼角桃花 2020-12-20 18:31

I wrote a method that accepts a generic parameter and then it prints its properties. I use it to test my web service. It\'s working but I want to add some features that I do

6条回答
  •  自闭症患者
    2020-12-20 19:24

    Here is a snippet, assuming that your List is of Type T.

     foreach (PropertyInfo item in propertyInfos)
                {
                    Object obj = item.GetValue(object,null);
                    if (!obj.GetType().IsValueType)
                    {
                        if (obj.GetType() == typeof(String))
                        {
                            Console.WriteLine(obj.ToString());
                        }
                        else if (obj.GetType() == typeof(List))
                        {
                            //run a loop and print the list
    
                        }
                        else if (obj.GetType().IsArray) // this means its Array
                        {
                            //run a loop to print the array
                        }
    
                    }
                    else
                    {
                        //its primitive so we will convert to string 
                        Console.WriteLine(obj.ToString());
    
                    }
    

提交回复
热议问题