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



我有一个具有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;
    }

建议?

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

要获得给定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;
}

像这个

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

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

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

这是我的建议:

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

试试这个。

        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条款。例如,如果您想排除statics或privates等,

相关内容

  • 没有找到相关文章

最新更新