代码之家  ›  专栏  ›  技术社区  ›  Michael D. Irizarry

类的嵌套属性的C#反射

  •  2
  • Michael D. Irizarry  · 技术社区  · 14 年前

    public class Customer
    {
       public string Name {get; set;}
       public string Lastname {get; set;}
       public CustomerAddress Address {get; set;}
    }
    

    所以我可以得到Name和LastName的属性值,但是我不知道如何得到客户地址。城市.

    这就是我现在所拥有的。

    public object GetPropertyValue(object obj, string property)
    {
       if (string.IsNullOrEmpty(property))
       return new object { };
    
       PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
       return propertyInfo.GetValue(obj, null);
    } 
    

    然后在LINQ语句中使用此方法。

    var cells = (from m in model
                             select new
                             {
                                 i = GetPropertyValue(m, key),
                                 cell = from c in columns
                                    select reflection.GetPropertyValue(m, c)
                         }).ToArray();
    

    所以我的客户地址没有价值。

    任何帮助都将不胜感激。

    我是怎么做到的。

    public object GetNestedPropertyValue(object obj, string property)
            {
                if (string.IsNullOrEmpty(property)) 
                    return string.Empty;
    
                var propertyNames = property.Split('.');
    
                foreach (var p in propertyNames)
                {
                    if (obj == null)
                        return string.Empty;
    
                    Type type = obj.GetType();
                    PropertyInfo info = type.GetProperty(p);
                    if (info == null)
                        return string.Empty;
    
                    obj = info.GetValue(obj, null);
    
                }
    
                return obj;
            }
    
    2 回复  |  直到 14 年前
        1
  •  3
  •   Brian Genisio    14 年前

    首先,如果你想得到 CustomerAddress.City CustomerAddress 是类型,而不是属性名。你想得到什么 Address.City .

    也就是说,你的 GetPropertyValue 不是为了做你想做的。

    尝试将名称按点拆分:

    var propertyNames = property.split('.');

    现在,您有一个属性名称列表 {"Address", "City"}

    然后,您可以逐个循环这些属性名,调用 获取属性值 递归地。

        2
  •  0
  •   Mathias    9 年前

    @迈克尔->倒转部分;-)

    public static void SetNestedPropertyValue(object obj, string property,object value)
            {
                if (obj == null)
                    throw new ArgumentNullException("obj");
    
                if (string.IsNullOrEmpty(property))
                    throw new ArgumentNullException("property");
    
                var propertyNames = property.Split('.');
    
                foreach (var p in propertyNames)
                {
    
                    Type type = obj.GetType();
                    PropertyInfo info = type.GetProperty(p);
                    if (info != null)
                    {
                        info.SetValue(obj, value);
                        return;
                    }
    
                }
    
                throw new KeyNotFoundException("Nested property could not be found.");
            }