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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:37:55+00:00 2026-05-17T17:37:55+00:00

I am storing object values in strings e.g., string[] values = new string[] {

  • 0

I am storing object values in strings e.g.,

string[] values = new string[] { "213.4", "10", "hello", "MyValue"};

is there any way to generically initialize the appropriate object types? e.g., something like

double foo1 = AwesomeFunction(values[0]);
int foo2 = AwesomeFunction(values[1]);
string foo3 = AwesomeFunction(values[2]);
MyEnum foo4 = AwesomeFunction(values[3]);

where AwesomeFunction is the function I need. The ultimate use is to intialize properties e.g.,

MyObject obj = new MyObject();
PropertyInfo info = typeof(MyObject).GetProperty("SomeProperty");
info.SetValue(obj, AwesomeFunction("20.53"), null);

The reason I need such functionality is I am storing said values in a database, and wish to read them out via a query and then initialize the corresponding properties of an object. Is this going to be possible? The entire object is not being stored in the database, just a few fields which I’d like to read & set dynamically. I know I can do it statically, however that will get tedious, hard to maintain, and prone to mistakes with numerous different fields/properties are being read.

EDIT: Bonus points if AwesomeFunction can work with custom classes which specify a constructor that takes in a string!

EDIT2: The destination type can be know via the PropertyType, in the specific case where I want to use this type of functionality. I think Enums Would be easy to parse with this e.g.,

Type destinationType = info.PropertyType;
Enum.Parse(destinationType, "MyValue");
  • 1 1 Answer
  • 2 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-05-17T17:37:55+00:00Added an answer on May 17, 2026 at 5:37 pm

    Perhaps the first thing to try is:

    object value = Convert.ChangeType(text, info.PropertyType);
    

    However, this doesn’t support extensibility via custom types; if you need that, how about:

    TypeConverter tc = TypeDescriptor.GetConverter(info.PropertyType);
    object value = tc.ConvertFromString(null, CultureInfo.InvariantCulture, text);
    info.SetValue(obj, value, null);
    

    Or:

    info.SetValue(obj, AwesomeFunction("20.53", info.PropertyType), null);
    

    with

    public object AwesomeFunction(string text, Type type) {
        TypeConverter tc = TypeDescriptor.GetConverter(type);
        return tc.ConvertFromString(null, CultureInfo.InvariantCulture, text);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any reason why storing a NSData object of say ~50MB into NSUserDefaults
Is there a way to create a new class from a String variable in
So, I'm getting a bunch of string values from a JSON object and then
I have a Django Queryset object called data containing string keys and float values.
I'm using ptr_map for storing different types of pointers. boost::ptr_map<string, any> someMap; I store
I'd like to create a Dictionary object, with string Keys, holding values which are
Is there an object in Java that acts like a Map for storing and
I have created dictionary object Dictionary<string, List<string>> dictionary = new Dictionary<string,List<string>>(); I want to
Typical way of creating a CSV string (pseudocode): Create a CSV container object (like
I am creating a W3C Document object using a String value. Once I created

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.