计算对象中所有字符串属性的总长度

时间:2011-10-14 08:56:10

标签: c# .net reflection

我有一个具有100个属性的对象。 它们都是字符串。如何计算所有属性的总长度?

MyAttempt:

    public static int GetPropertiesMaxLength(object obj)
    {
        int totalMaxLength=0;
        Type type = obj.GetType();
        PropertyInfo[] info = type.GetProperties();
        foreach (PropertyInfo property in info)
        {
           // ? 
            totalMaxLength+=??
        }
        return totalMaxLength;
    }

建议?

7 个答案:

答案 0 :(得分:4)

使用LINQ Sum()Where()方法:

public static int GetTotalLengthOfStringProperties(object obj)
{            
    Type type = obj.GetType();
    IEnumerable<PropertyInfo> info = type.GetProperties();

    int total = info.Where(p => p.PropertyType == typeof (String))
                    .Sum(pr => (((String) pr.GetValue(obj, null)) 
                               ?? String.Empty).Length);
    return total;
}

PS:要启用 LINQ,您需要添加using System.Linq;

编辑:更通用的方法

/// <summary>
/// Gets a total length of all string-type properties
/// </summary>
/// <param name="obj">The given object</param>
/// <param name="anyAccessModifier">
/// A value which indicating whether non-public and static properties 
/// should be counted
/// </param>
/// <returns>A total length of all string-type properties</returns>
public static int GetTotalLengthOfStringProperties(
                                  object obj, 
                                  bool anyAccessModifier)
{
    Func<PropertyInfo, Object, int> resolveLength = (p, o) =>        
        ((((String) p.GetValue(o, null))) ?? String.Empty).Length;

    Type type = obj.GetType();
    IEnumerable<PropertyInfo> info = anyAccessModifier 
        ? type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | 
                             BindingFlags.Instance | BindingFlags.Static)
        : type.GetProperties();    

    int total = info.Where(p => p.PropertyType == typeof (String))
                    .Sum(pr => resolveLength(pr, obj));
    return total;
}

答案 1 :(得分:1)

喜欢这个

public static int GetPropertiesMaxLength(object obj)
{
    int totalMaxLength=0;
    Type type = obj.GetType();
    PropertyInfo[] info = type.GetProperties();
    foreach (PropertyInfo property in info)
    {
        totalMaxLength+= property.GetValue(obj, null).ToString().Length;
    }
    return totalMaxLength;
}

答案 2 :(得分:1)

要获取给定其PropertyInfo的属性的值,请使用传递包含对象的GetValue方法而不使用任何参数(除非这是一个索引属性 - 这会使事情变得更复杂):

public static int GetPropertiesMaxLength(object obj) {
  int totalMaxLength=0;
  Type type = obj.GetType();
  PropertyInfo[] info = type.GetProperties();
  foreach (PropertyInfo property in info) {
     if (property.PropertyType == typeof(string)) {
       string value = property.GetValue(obj, null) as string;
       totalMaxLength += value.Length;
    }
  }
  return totalMaxLength;
}

答案 3 :(得分:0)

假设所有属性都是公开的:

Object objValue = property.GetValue(obj, null);
if(objValue  != null)
  totalMaxLength += obj
Value.ToString().Length;

如果属性不是全部公开,那么你将必须组合所需的绑定标志,如

Object objValue = property.GetValue(obj, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static /* etc... */, null, null, null);

答案 4 :(得分:0)

这是我的建议:

    public static int GetPropertiesMaxLength(object obj)
    {
        int totalMaxLength = 0;
        Type type = obj.GetType();
        PropertyInfo[] info = type.GetProperties();
        foreach (PropertyInfo property in info)
        {
            var value = property.GetValue(obj, null) as string;
            if (!string.IsNullOrEmpty(value))
            {
                totalMaxLength += value.Length;
            }
        }
        return totalMaxLength;
    }

答案 5 :(得分:0)

        int count = value.GetType()
            .GetProperties()
            .Where(p => p.PropertyType == typeof (string) && p.CanRead)
            .Select(p => (string) p.GetValue(value, null))
            .Sum(s => (string.IsNullOrEmpty(s) ? 0 : s.Length));

您可以根据需要更改where子句。例如,如果要排除静态或私有等,

答案 6 :(得分:0)

public static int GetPropertiesMaxLength(object obj)
{
    Type type = obj.GetType();
    PropertyInfo[] info = type.GetProperties();
    int totalMaxLength = info.Sum(prop => (prop.GetValue(prop, null) as string).Length);        
    return totalMaxLength;
}

试试这个。