【问题标题】:get the value of the attributes in C#获取 C# 中属性的值
【发布时间】:2018-12-10 14:49:42
【问题描述】:

这是一个非常简单的问题。 比如这段代码:

        if(o == null)
        {
            o = new { };
        }
        PropertyInfo[] p1 = o.GetType().GetProperties();
        foreach(PropertyInfo pi in p1)
        {}

但是像这样:

ModelA.ModelB.ModelC.ModelD.ModelE

如何通过反射ModelA得到ModelE的值

【问题讨论】:

标签: c#


【解决方案1】:

有解决办法解释here:

使用辅助方法:

public static class ReflectionHelper
{
    public static Object GetPropValue(this Object obj, String propName)
    {
        string[] nameParts = propName.Split('.');
        if (nameParts.Length == 1)
        {
            return obj.GetType().GetProperty(propName).GetValue(obj, null);
        }

        foreach (String part in nameParts)
        {
            if (obj == null) { return null; }

            Type type = obj.GetType();
            PropertyInfo info = type.GetProperty(part);
            if (info == null) { return null; }

            obj = info.GetValue(obj, null);
        }
        return obj;
    }
}

那么方法可以这样使用:

ModelA obj = new ModelA { */....*/ };
obj.GetPropValue("modelB.modelC.modelD.modelE");

请注意,您应该将属性名称而不是类名称传递给函数。

【讨论】:

    【解决方案2】:

    使用嵌套函数如下:

        var testObj = new
    {
        nameA = "A",
        ModelB = new
        {
            nameB = "B",
            ModelC = new
            {
                NameC = "C",
            }
        }
    };
    var result = ParseProperty(testObj, null, "ModelA");
    
    public Dictionary<string, object> ParseProperty(object o, Dictionary<string, object> result, string preFix = null)
    {
        result = result ?? new Dictionary<string, object>();
    
        if (o == null) return result;
    
        Type t = o.GetType();
        //primitive type or value type  or string or nested return
        if (t.IsPrimitive || t.IsValueType || t.FullName == "System.String" || t.IsNested) return result;
    
        var proerties = o.GetType().GetProperties();
        foreach (var property in proerties)
        {
            var value = property.GetValue(o);
            result.Add($"{preFix}.{property.Name}", value);
            //nested call
            ParseProperty(value, result, $"{preFix}.{property.Name}");
        }
        return result;
    }
    

    【讨论】:

      【解决方案3】:

      我假设这些都是匿名类型,否则你可以只对 ModelE 的类型执行 GetProperties()。

      所以你基本上必须像接下来的 5 个循环一样

      foreach (PropertyInfo pi1 in o1.GetType().GetProperties())
      {
          if (pi.Name = "ModelB") // or some other criterion
          {
              o2 = pi1.GetValue(o1);
              foreach (PropertyInfo pi2 in o2.GetType().GetProperties())
              {
                  if (pi.Name = "ModelC") // or some other criterion
                  {
                      o3 = pi1.GetValue(o2);
                      // and so on
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-04-24
        • 2015-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-17
        • 1970-01-01
        相关资源
        最近更新 更多