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

  • SEARCH
  • Home
  • 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 91459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:01:59+00:00 2026-05-10T23:01:59+00:00

I know I could have an attribute but that’s more work than I want

  • 0

I know I could have an attribute but that’s more work than I want to go to… and not general enough.

I want to do something like

class Whotsit {     private string testProp = 'thingy';      public string TestProp      {         get { return testProp; }         set { testProp = value; }     }  }  ...  Whotsit whotsit = new Whotsit(); string value = GetName(whotsit.TestProp); //precise syntax up for grabs.. 

where I’d expect value to equal ‘TestProp’

but I can’t for the life of me find the right reflection methods to write the GetName method…

EDIT: Why do I want to do this? I have a class to store settings read from a ‘name’, ‘value’ table. This is populated by a generalised method based upon reflection. I’d quite like to write the reverse…

/// <summary> /// Populates an object from a datatable where the rows have columns called NameField and ValueField.  /// If the property with the 'name' exists, and is not read-only, it is populated from the  /// valueField. Any other columns in the dataTable are ignored. If there is no property called /// nameField it is ignored. Any properties of the object not found in the data table retain their /// original values. /// </summary> /// <typeparam name='T'>Type of the object to be populated.</typeparam> /// <param name='toBePopulated'>The object to be populated</param> /// <param name='dataTable'>'name, 'value' Data table to populate the object from.</param> /// <param name='nameField'>Field name of the 'name' field'.</param> /// <param name='valueField'>Field name of the 'value' field.</param> /// <param name='options'>Setting to control conversions - e.g. nulls as empty strings.</param>  public static void PopulateFromNameValueDataTable<T>         (T toBePopulated, System.Data.DataTable dataTable, string nameField, string valueField, PopulateOptions options)     {         Type type = typeof(T);         bool nullStringsAsEmptyString = options == PopulateOptions.NullStringsAsEmptyString;          foreach (DataRow dataRow in dataTable.Rows)         {             string name = dataRow[nameField].ToString();             System.Reflection.PropertyInfo property = type.GetProperty(name);             object value = dataRow[valueField];              if (property != null)             {                 Type propertyType = property.PropertyType;                 if (nullStringsAsEmptyString && (propertyType == typeof(String)))                 {                     value = TypeHelper.EmptyStringIfNull(value);                 }                 else                 {                     value = TypeHelper.DefaultIfNull(value, propertyType);                 }                  property.SetValue(toBePopulated, System.Convert.ChangeType(value, propertyType), null);             }         }     } 

FURTHER EDIT: I am just in code, have an instance of Whotsit and I want to get the text string of the ‘TestProp’ property. It seems kind of weird I know, I can just use the literal ‘TestProp’ – or in the case of my class to datatable function I’d be in a foreach loop of PropertyInfos. I was just curious…

The original code had string constants, which I found clumsy.

  • 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-10T23:02:00+00:00Added an answer on May 10, 2026 at 11:02 pm

    No, there’s nothing to do this. The expression whotsit.TestProp will evaluate the property. What you want is the mythical ‘infoof’ operator:

    // I wish... MemberInfo member = infoof(whotsit.TestProp); 

    As it is, you can only use reflection to get the property by name – not from code. (Or get all the properties, of course. It still doesn’t help you with your sample though.)

    One alternative is to use an expression tree:

    Expression<Func<string>> = () => whotsit.TestProp; 

    then examine the expression tree to get the property.

    If none of this helps, perhaps you could tell us more about why you want this functionality?

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

Sidebar

Related Questions

Image i have a view that is cached with the OutputCache attribute but i
I have a question that is more in the realm of design, than implementation.
I know I could write scripts and create jobs to run them, but at
Yes, I know you could use regular objects as associative arrays in JavaScript, but
Our dev shop currently uses Visual SourceSafe. We all know how that could end
Does anyone know of an algorithm that I could use to find an interesting
Does anyone know of an application (hosted or otherwise) that I could use to
Before I start using CORBA I want to know something. It would seem intuitive
I have a JavaScript function that returns a string. I want to write this
I know you could make a helper pretty easily given the data. So, if

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.