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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:11:53+00:00 2026-06-13T20:11:53+00:00

I have been reading over old questions asked ans searching MSDN help but I

  • 0

I have been reading over old questions asked ans searching MSDN help but I cant really understand what a PropertyInfo is, specifically relating to the question of looping through an array list

c# foreach (property in object)… Is there a simple way of doing this?)

I made a simple class

public MyClass
{
public double myProperty; // etc
}

and then I add class objects to a list. I want to loop through the list, to change just a property of each object

foreach ( MyClass i in MyClassList)
{
foreach ( double myProperty in i.GetType().GetProperties() )
{
// do something
}


}

but I get an error. In the linked question (above) it says to use PropertyInfo instead of ‘double. what is the PropertyInfo that should replace the ‘double’ of myProperry and what does it represent?

  • 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-13T20:11:54+00:00Added an answer on June 13, 2026 at 8:11 pm

    PropertyInfo simply represents the fact that a property is defined for that type; it isn’t per-object – it is for the type. You don’t need any instances to get a PropertyInfo.

    First, however, note that myProperty is not currently a property: let’s fix that first:

    public MyClass
    {
        public double MyProperty {get;set;}
    }
    

    now we can find out about that property, either by asking about “all the properties it has”, i.e.

    PropertyInfo[] props = typeof(MyClass).GetProperties();
    

    or an individual property, perhaps getting the name from configuration at runtime:

    PropertyInfo prop = typeof(MyClass).GetProperty("MyProperty");
    

    You can inspect a PropertyInfo for the name, type, attributes, etc – very useful for library code. However, in regular code the simplest option is just to use static C#:

    foreach(MyClass obj in MyClassList) {
        obj.MyProperty = 123.45;
    }
    

    if you need to do this via reflection, then you can use SetValue:

    foreach(MyClass obj in MyClassList) {
        prop.SetValue(obj, 123.45, null);
    }    
    

    however, reflection is relatively slow unless you go to greater lengths. For example, another alternative for targeting the “I’ll know the names at runtime” scenario would be FastMember; then you can do:

    var accessor = TypeAccessor.Create(typeof(MyClass));
    string propName = "MyProperty";
    object value = 123.45;
    foreach(MyClass obj in MyClassList) {
        accessor[obj, propName] = value;
    }
    

    which will be much faster than raw reflection, while having more flexibility in terms of finding property-names at runtime.

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

Sidebar

Related Questions

I have been reading over a lot of questions, but cant seem to find
I have been searching for days and reading over facebook docs but I cannot
I have been reading all over the web about this and still can't understand
I am slowly learning C, but not very well. I have been reading over
I have been reading up most questions on this item, but somehow I can't
I have just been reading over some of my old oo design books and
I have been reading over some code lately and came across some lines such
hello im new to python and have been reading over the documentation and am
I have been reading that direct access to a SQL Server database over the
Over the past week or so I have been reading a lot of articles

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.