2011-10-14 68 views
2

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

建議?

+3

具有_hundreds_屬性的對象?聽起來你可能想考慮使用'Dictionary '代替。 –

+0

你是指所有物業的總長度是什麼意思?他們的價值? – Rob

+0

property.length()? – EO2

回答

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

感謝這正是我正在尋找的東西 – user9969

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

這將意味着,例如,值爲12345的整數類型屬性將爲該長度添加五個,值爲-1234。 – Richard

+0

我同意,但OP表示所有屬性都是字符串 – tonycoupland

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

使用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; 
} 
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; 
    } 
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子句。例如,如果你想排除靜態或私人等

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

試試這個。

相關問題