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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:06:27+00:00 2026-05-25T22:06:27+00:00

Alternative Title: Dynamically convert to a type at runtime. I want to to convert

  • 0

Alternative Title: Dynamically convert to a type at runtime.

I want to to convert an Object to a type that will be assigned at runtime.

For example, assume that I have a function that assigns a string value(from a TextBox or Dropdownlist) to an Object.Property.

How would I convert the value to proper type? For instance, it could be an integer, string, or enum.

Public void Foo(object obj,string propertyName,object value)
{
  //Getting type of the property og object.
  Type t= obj.GetType().GetProperty(propName).PropertyType;

  //Now Setting the property to the value .
  //But it raise an error,because sometimes type is int and value is "2"
  //or type is enum (e.a: Gender.Male) and value is "Male"
  //Suppose that always the cast is valid("2" can be converted to int 2)

  obj.GetType().GetProperty(propName).SetValue(obj, value, null);
}
  • 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-05-25T22:06:27+00:00Added an answer on May 25, 2026 at 10:06 pm

    You need to use the Convert.ChangeType(…) function [note: in the function below, the input propertyValue could just as easily have been of type object … I just had a string version pre-baked]:

    /// <summary>
    /// Sets a value in an object, used to hide all the logic that goes into
    ///     handling this sort of thing, so that is works elegantly in a single line.
    /// </summary>
    /// <param name="target"></param>
    /// <param name="propertyName"></param>
    /// <param name="propertyValue"></param>
    public static void SetPropertyValueFromString(this object target,               
                                  string propertyName, string propertyValue)
    {
        PropertyInfo oProp = target.GetType().GetProperty(propertyName);
        Type tProp = oProp.PropertyType;
    
        //Nullable properties have to be treated differently, since we 
        //  use their underlying property to set the value in the object
        if (tProp.IsGenericType
            && tProp.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
        {
            //if it's null, just set the value from the reserved word null, and return
            if (propertyValue == null)
            {
                oProp.SetValue(target, null, null);
                return;
            }
    
            //Get the underlying type property instead of the nullable generic
            tProp = new NullableConverter(oProp.PropertyType).UnderlyingType;
        }
    
        //use the converter to get the correct value
        oProp.SetValue(target, Convert.ChangeType(propertyValue, tProp), null);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

-Edit- Alternative question/example How do i cast A to object to class A when
I have a number of elements with the same class like this: <html> <head><title>example</title>
A bit hard to find a proper title... I have an object which basically
is there an alternative for mysql_insert_id() php function for PostgreSQL? Most of the frameworks
Is there any alternative image manipulation library for .net? I would prefer something that
As title says need to convert a Zip file to text file , no
As the title says, I have a problem with binding to a change in
Like the title says. I find a lot of the issues I have are
I'm trying to insert a string that begins with the word title as in
I have a doubt in XML Vocabulary. XML schema is an alternative to the

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.