Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 81225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:25:36+00:00 2026-05-10T21:25:36+00:00

I am doing some simple sanity validation on various types. The current test I’m

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:25:36+00:00Added an answer on May 10, 2026 at 9:25 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 61k
  • Answers 61k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I have used this solution for a RichTextBox, but I… May 11, 2026 at 9:44 am
  • added an answer I used the following psd that I derived from http://www.teehanlax.com/blog/?p=447… May 11, 2026 at 9:44 am
  • added an answer I do not know Flex Builder project config files (I… May 11, 2026 at 9:44 am

Related Questions

I am doing some simple sanity validation on various types. The current test I'm
I am doing some research on Unicode for a white-paper I am writing. Does
I am doing some calculations that require a large array to be initialized. The
I am doing some TTF work for MOSA (the correlating body between all the
I am doing some float manipulation and end up with the following numbers: -0.5
I am doing some population modeling (for fun, mostly to play with the concepts
I am doing some significant refactoring and feature-adding on a project, and have just
I am doing some work on a web site that has a secure area
I am doing some maintenance on a VB6 Windows application. I have a .Net
I am new to C# and am doing some work in an existing application.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.