zoukankan      html  css  js  c++  java
  • 使用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));
           }

  • 相关阅读:
    【R】爬虫案例
    [R] 保存pheatmap图片对象到文件
    [R] 添加误差棒的分组折线图:geom_path: Each group consists of only one observation. Do you need to adjust the...
    [R] read.table/read.delim读入数据行数变少?
    [R] cbind和filter函数的坑
    [R]在dplyr函数的基础上编写函数(3)tidyeval
    [R]在dplyr基础上编写函数(2)substitute和quote
    30个Java知识点
    Java的30个知识点
    40个知识点
  • 原文地址:https://www.cnblogs.com/zbw911/p/4953947.html
Copyright © 2011-2022 走看看