Reputation: 16040
I have an object with 100s of properties. they are all strings.How can I calculate the total length of all properties put together?
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;
}
Suggestions?
Upvotes: 3
Views: 2564
Reputation: 62544
Using LINQ Sum() and Where() methods:
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: To enable LINQ you've to add using System.Linq
;
EDIT: More generic approach
/// <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;
}
Upvotes: 4
Reputation: 4075
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));
You could alter the where clause to your needs. For example if you want to exclude statics or privates, etc.
Upvotes: 0
Reputation: 1613
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;
}
Try this.
Upvotes: 0
Reputation: 12458
Here is my suggestion:
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;
}
Upvotes: 0
Reputation: 109080
To get the value of a property given its PropertyInfo
use the GetValue
method passing the containing object and no parameters (unless this is an indexed property – which would make things more complicated):
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;
}
Upvotes: 2
Reputation: 1618
Assuming that all properties are public:
Object objValue = property.GetValue(obj, null);
if(objValue != null)
totalMaxLength += obj
Value.ToString().Length;
If the properties are not all public then you'll have to combine the needed binding flags like this
Object objValue = property.GetValue(obj, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static /* etc... */, null, null, null);
Upvotes: 0
Reputation: 4247
Like this
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;
}
Upvotes: 1