使用Path语法取得对象的值

借鉴了http://stackoverflow.com/questions/4473928/c-sharp-dynamic-string-property-path

 

 

public class ReflectUtil
   {
       public static object ReflectOnPath(object o, string path)
       {
           object value = o;
           string[] pathComponents = path.Split('.');
           foreach (var component in pathComponents)
           {
               var x = component.Split(':');

               value = value.GetType().GetProperty(x[0]).GetValue(value, null);

               if (value.GetType().IsArray)
               {
                   int[] index = null;
                   if (x.Length > 1)
                       index = x.Skip(1).Take(int.MaxValue).Select(y => int.Parse(y)).ToArray();
                   if (index != null) value = ((Array)value).GetValue(index);
               }
           }
           return value;
       }
   }

 

 

 

Example:

[TestMethod]
       public void TestGetValue()
       {
           var o = new DistributeCfg
           {
               Configs = new[]
               {
                   new Config.Config
                   {
                       AppType = "v",
                       CallbackUrl = "fasdfasdf",
                       Key = "k",
                        NoticePostDataSetting = new PostDataSetting
                        {
                             Args = new Arg[]
                             {
                                  new Arg
                                  {
                                       Name = "name1",
                                       Value = "a.b.c"
                                  },
                                   new Arg
                                  {
                                       Name = "name2",
                                       Value = "a.b.c"
                                  }
                             }
                        },
                         PredictPostDataSetting  = new PostDataSetting
                        {
                             Args = new Arg[]
                             {
                                  new Arg
                                  {
                                       Name = "name1",
                                       Value = "Predict.b.c"
                                  },
                                  new Arg
                                  {
                                       Name = "name2",
                                       Value = "Predict.b.c"
                                  }
                             },
                              DeleteFlgSetting = new DeleteFlgSetting
                              {
                                   Audit = "1,2",
                                    Delete = "9",
                                     Pass = "10"
                              }
                        },
                   },
                     new Config.Config
                   {
                       AppType = "v",
                       CallbackUrl = "asdfasdfasdf",
                       Key = "k",
                        NoticePostDataSetting = new PostDataSetting
                        {
                             Args = new Arg[]
                             {
                                  new Arg
                                  {
                                       Name = "name1",
                                       Value = "a.b.c"
                                  },
                                   new Arg
                                  {
                                       Name = "name2",
                                       Value = "a.b.c"
                                  }
                             }
                        },
                         PredictPostDataSetting  = new PostDataSetting
                        {
                             Args = new Arg[]
                             {
                                  new Arg
                                  {
                                       Name = "name1",
                                       Value = "Predict.b.c"
                                  },
                                  new Arg
                                  {
                                       Name = "name2",
                                       Value = "Predict.b.c"
                                  }
                             },
                              DeleteFlgSetting = new DeleteFlgSetting
                              {
                                   Audit = "1,2",
                                    Delete = "9",
                                     Pass = "10"
                              }
                        },
                   }
               }
           };


           //var d = o.GetType().GetField("Configs").GetValue(o);


           string path = "Configs:0.PredictPostDataSetting.Args:0.Name";
           Console.WriteLine(ReflectUtil.ReflectOnPath(o, path));
           var p = o.GetType().GetProperty("Configs");
           //p.GetValue()
           //var array = o.GetType().GetProperty("Configs").GetType().IsArray;


           //var value = o.GetType().GetProperty("Configs").GetValue(o);

 

       }


       [TestMethod]
       public void TestValue()
       {
           var a = new A();
           a.Field = new B();
           a.Field.SubField = 10;

           string path = "Field.SubField";
           Console.WriteLine(ReflectUtil.ReflectOnPath(a, path));
       }

posted @ 2015-11-10 18:30  张保维  阅读(175)  评论(0编辑  收藏  举报