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 7761187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:04:28+00:00 2026-06-01T14:04:28+00:00

I am using very similar loops to iterate all public fields and properties of

  • 0

I am using very similar loops to iterate all public fields and properties of any passed object. I determine if the field/property is decorated with a particular custom attribute; if so, an action is performed on the value of the field or property. Two loops are necessary because the method to get a field value is different from the method to get a property value.

// Iterate all public fields using reflection
foreach (FieldInfo fi in obj.GetType().GetFields())
{
  // Determine if decorated with MyAttribute.
  var attribs = fi.GetCustomAttributes(typeof(MyAttribute), true);
  if (attribs.Length == 1)
  {
    // Get value of field.
    object value = fi.GetValue(obj);
    DoAction(value);
  }
}

// Iterate all public properties using reflection
foreach (PropertyInfo pi in obj.GetType().GetProperties())
{
  // Determine if decorated with MyAttribute.
  var attribs = pi.GetCustomAttributes(typeof(MyAttribute), true);
  if (attribs.Length == 1)
  {
    // Get value of property.
    object value = pi.GetValue(obj, null);
    DoAction(value);
  }
}

I would like to place the loop in a single, common method so that I can instead write, more simply:

DoEachMember(obj.GetType().GetFields());
DoEachMember(obj.GetType().GetProperties());

This requires DoEachMember() to accept the MemberInfo type (which is the parent type of both FieldInfo and PropertyInfo). The problem is there is no GetValue method in the MemberInfo class. Both FieldInfo and PropertyInfo use different methods to get the field/property value:

public void DoEachMember(MemberInfo mi, object obj)
{
  foreach (MemberInfo mi in obj.GetType().GetProperties())
  {
    object value mi.GetValue(obj); // NO SUCH METHOD!
  }
}

Thus, I declare a delegate to utilize inside the loop which takes a MemberInfo and returns the value of that member as an object:

// Delegate to get value from field or property.
delegate object GetValue(MemberInfo mi, object obj);

The Question

How can I detect the type of the objects in the members[] array, in order to define the delegate used inside the loop? Currently, I am using the first element of the array, members[0]. Is this a good design?

public void DoEachMember(MemberInfo[] members, object obj)
{
  // Protect against empty array.
  if (members.Length == 0) return;

  GetValue getValue; // define delegate

  // First element is FieldInfo
  if (members[0] as FieldInfo != null)
    getValue = (mi, obj) => ((FieldInfo)mi).GetValue(obj);

  // First element is PropertyInfo
  else if (members[0] as PropertyInfo != null)
    getValue = (mi, obj) => ((PropertyInfo)mi).GetValue(obj, null);

  // Anything else is unacceptable
  else
    throw new ArgumentException("Must be field or property.");

  foreach (MemberInfo mi in members)
  {
    // Determine if decorated with MyAttribute.
    var attribs = mi.GetCustomAttributes(typeof(MyAttribute), true);
    if (attribs.Length == 1)
    {
      object value = getValue(mi, obj);
      DoStuff(value);
    }
  }
}

Alternatively, I could detect the type upon each iteration, but there should be no reason individual array members will ever differ:

foreach (MemberInfo mi in members)
{
  // ...

  object value;

  if ((var fi = mi as FieldInfo) != null)
    value = fi.GetValue(obj);

  else if ((var pi = mi as PropertyInfo) != null)
    value = pi.GetValue(obj, null);

  else
    throw new ArgumentException("Must be field or property.");

  DoStuff(value);
}
  • 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. Editorial Team
    Editorial Team
    2026-06-01T14:04:29+00:00Added an answer on June 1, 2026 at 2:04 pm

    You could project to the object values first and then work on those in your loop. Your whole code could be boiled down to this (plus your loop):

        /// <summary>
        /// Gets the value of all the fields or properties on an object that are decorated with the specified attribute
        /// </summary>
        private IEnumerable<object> GetValuesFromAttributedMembers<TAttribute>(object obj)
            where TAttribute : Attribute
        {
            var values1 = obj.GetType().GetFields()
                            .Where(fi => fi.GetCustomAttributes(typeof(TAttribute), true).Any())
                            .Select(fi => fi.GetValue(obj));
            var values2 = obj.GetType().GetProperties()
                            .Where(pi => pi.GetCustomAttributes(typeof(TAttribute), true).Any())
                            .Select(pi => pi.GetValue(obj, null));
            return values1.Concat(values2);
        }
    

    Your current code mixes two concerns: finding the values and doing something with them. It would be cleaner to separate those concerns. The above LINQ could be placed in one method that fetches all values from a class that are in fields or properties that match a given attribute and another than is just a loop doing the work on whatever it is passed.

    Not as clean but sticking with your original goal you could do this and pass in a delegate appropriate to the type of the MemberInfo you are retrieving:-

        public void DoEachMember<TAttribute, TMembertype>(IEnumerable<TMembertype> members,
                                 Func<TMembertype, object> valueGetter)
            where TMembertype : MemberInfo
        {
            foreach (var mi in members)
            {
                if (mi.GetCustomAttributes(typeof(TAttribute), true).Any())
                {
                    // Get value of field.
                    object value = valueGetter(mi);
                    DoAction(value);
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using nHibernate to map an object very similar to .NET's System.Web.SiteMapNode. In order
I'm using the jQuery validation plugin in a very similar manner to the Remember
I have an App using core data with 3 entities with very similar attributes.
I'm using Flash Builder 4 (the version after Flex Builder 3, so very similar
I'm using a very simple Stylesheet Switch by php. It was fine all along
I'm working on legacy code that looks very similar to this: public class BaseParser
I am an iphone application developer, all iphones have very similar Operating systems, and
Im using code very similar to below: - (void)flipToViewController:(UIViewController*)targetViewController transition:(UIViewAnimationTransition)transition { if( targetViewController )
I've built a CSS navigation menu using a sprite, very similar to the one
I've already asked a very similar question but that was using javascript/php - i've

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.