【问题标题】:C# Reflection on Nested Properties of a ClassC# 对类的嵌套属性的反思
【发布时间】:2011-04-09 19:26:45
【问题描述】:

我想知道如何在 C# 中获取属性的值,但是这个属性是另一种类型。

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

所以我能够获取 Name 和 LastName 的属性值,但我完全不知道如何获取 CustomerAddress.City 的值。

这是我到现在为止的。

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();

所以我对 CustomerAddress 没有任何价值。

任何帮助将不胜感激。

**** 更新 ****

我是如何做到的。

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;
        }

【问题讨论】:

  • 如何设置嵌套属性的值? (和这个一样,但是对于 info.SetValue...)?

标签: c# linq linq-to-sql reflection properties


【解决方案1】:

@Michael -> 倒置部分;-)

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.");
        }

【讨论】:

  • 这完全没用,它甚至没有简单的Typeconversion。它也不会创建父对象的新实例。在 foreach 的第二次迭代中会有一个空引用。这根本不起作用,仅适用于字符串并且仅当它不是复杂类型时。
【解决方案2】:

首先,如果你想得到CustomerAddress.City 的值,你永远也得不到。 CustomerAddress 是类型,而不是属性名称。你想得到Address.City

也就是说,您的 GetPropertyValue 未设置为执行您想要的操作。

尝试用点分隔名称:

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

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

然后,您可以逐个循环遍历这些属性名称,递归调用GetPropertyValue

应该这样做:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 2018-07-19
    相关资源
    最近更新 更多