views:

68

answers:

3

I am doing some simple sanity validation on various types. The current test I'm working on is checking to make sure their properties are populated. In this case, populated is defined as not null, having a length greater than zero (if a string), or not equal to 0 (if an integer).

The "tricky" part of this test is that some properties are immune to this check. Right now I use a giant if statement that weeds out properties that don't need to be checked.

//Gets all the properties of the currect feature.
System.Reflection.PropertyInfo[] pi = t.GetProperties();

for(int i = 0; i < pi.Length; i++)
{
 if(!pi[i].Name.Equals("PropertyOne") 
 && !pi[i].Name.Equals("PropertyTwo") 
 && !pi[i].Name.Equals("PropertyThree") 
            //... repeat a bunch more times
 && !pi[i].Name.IndexOf("ValueOne") != -1 
 && !pi[i].Name.IndexOf("ValueTwo") != -1 
            //... repeat a bunch more times
 {
  //Perform the validation check.
 }                 
}

When profiling, I noticed the if statement is actually performing worse than the reflection (not that the reflection is blazing fast). Is there a more efficient way to filter the properties of several different types?

I've thought about a massive regular expression but I'm unsure on how to format it, plus it would probably be unreadable given its size. I've also considered storing the values in a List and then using Linq but I'm not sure how to handle the cases that use String.IndexOf() to find if the property contains a certain value.

Thanks in advance.

+2  A: 

Make a HashSet "exactNames" with PropertyOne, PropertyTwo etc, and then a List "partialNames" with ValueOne, ValueTwo etc. Then:

var matchingProperties = pi.Where(exactNames.Contains(pi.Name) ||
                          partialNames.Any(name => pi.Name.Contains(name));

foreach (PropertyInfo property in matchingProperties)
{
    // Stuff
}

(Odd indenting just to avoid wrapping.)

Note that you could cache the set of properties to validate on a per-type basis, so you only need to go through this check once per type.

Jon Skeet
A: 

Your idea help speed up my program, thank you. However, you had some syntax issues, plus you were matching items found in the lists and I needed items not in the list. Here is the code I ended up using.

List<System.Reflection.PropertyInfo> pi = type.GetProperties().ToList();

var matchingProperties = pi.Where( prop => !PropertyExclusionSet.Contains( prop.Name )
&& !PropertiesPartialSet.Any( name => prop.Name.Contains( name ) ) );
A: 

You might consider decorating your properties with attributes that tell what action needs to be done to them.

public class MyClass {

   [CheckMe]
   public int PropertyOne { get; set; }

   [DontCheckMe]
   public int PropertyTwo { get; set; }

}
Ted Elliott