Annoymous type get properties and values

 1 static void AnnoymousReflection()
 2         {
 3             var obj = new
 4             {
 5                 First = 3456.345,
 6                 Second = 43545657,
 7                 Third = 'A',
 8                 Forth = "Test",
 9                 Fifth = true,
10                 Sixth = 10.043M
11             };
12             
13             Type type = obj.GetType();
14             PropertyInfo[] pis = type.GetProperties();
15             if(pis!=null && pis.Any())
16             {
17                 foreach(var pi in pis)
18                 {
19                     var piValue = pi.GetValue(obj);
20                     Console.WriteLine($"Name={pi.Name},Value={piValue}");
21                 }
22             }
23              
24         }

 

posted @ 2021-03-11 11:35  FredGrit  阅读(54)  评论(0编辑  收藏  举报