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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:49:45+00:00 2026-06-16T18:49:45+00:00

Looks like this has been answered for python, but not C#, and since I’m

  • 0

Looks like this has been answered for python, but not C#, and since I’m python illiterate and new at C#, here goes:

I’m trying to get a property from a instance of a class (Task/task) based on a enum parameter (type) and add the property to a List. The tricky part is that I’m not sure if the property value is going to be a string or a list of strings.

So, generally I’m looking at something like:

PropertyInfo propertyInfo = typeof(Task).GetProperty(type.ToString());
List<string> values = new List<string>();

then something I know doesn’t work when the value is a List, but illustrates my intent:

values.Add((string)propertyInfo.GetValue(task, null));

What are my options?

  • 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-16T18:49:46+00:00Added an answer on June 16, 2026 at 6:49 pm

    You can use PropertyInfo.PropertyType to check the type of the property – or you could just fetch the value as object and go from there:

    List<string> values = new List<string>();
    object value = propertyInfo.GetValue(task, null);
    if (value is string)
    {
        values.Add((string) value);
    }
    else if (value is IEnumerable<string>)
    {
        values.AddRange((IEnumerable<string>) value);
    }
    else
    {
        // Do whatever you want if the type doesn't match...
    }
    

    Or instead of using is and casting, you can use as and check the result against null:

    List<string> values = new List<string>();
    object value = propertyInfo.GetValue(task, null);
    string stringValue = value as string;
    if (stringValue != null)
    {
        values.Add(stringValue);
    }
    else
    {
        IEnumerable<string> valueSequence = value as IEnumerable<string>;
        if (valueSequence != null)
        {
            values.AddRange(valueSequence);
        }
        else
        {
            // Do whatever you want if the type doesn't match...
        } 
    }
    

    Note that this works if the property is any other sort of sequence of strings, not just a List<string>. It also copies the list, so that any further changes won’t affect the existing list that the property refers to. Adjust if you need to 🙂

    One point that Lee’s answer reminded me of – if it’s a string property with a null value and you want a list with a single null element, you’ll need to use PropertyType. For example:

    if (propertyInfo.PropertyType == typeof(string))
    {
        values.Add((string) propertyInfo.GetValue(task, null));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looks like this question has been asked thousand times already, but each person's configuration
It looks like this question has been asked dozens of times, but none of
It looks like this has been asked and answered in all the simple cases,
Sorry if this has been answered already, but I could not find an appropriate
Maybe this has already been answered and I am searching incorrectly, but here is
Sorry if this has been answered before; I've looked at similar posts here but
UPDATE It looks like this problem has been quietly fixed in iOS 4.3. Up
I know this question has been asked/answered but I can't find it for the
Sorry if this has been answered somewhere; I'm not quite sure how to phrase
So, I'm sure this has been answered somewhere out there before, but I couldn't

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.