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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:28:36+00:00 2026-05-13T21:28:36+00:00

I have a System.Type stored in a variable. I wish to Change an object’s

  • 0

I have a System.Type stored in a variable. I wish to Change an object’s type to this type.

I have the following code, but cannot get the Type to change to the type.

Ideally I’d like: var intTest3 =(MyType)Convert.ChangeType(test, MyType); to return an int, when : MyType is a System.Int32

Here’s my working so far – where am I going wrong?

        // object to cast to int
        object test = 1;

        // INT32 type
        Type MyType = typeof(System.Int32);

        // explicit type int WORKS
        var intTest = (int)Convert.ChangeType(test, typeof(Int32));


        // explicit type to int WORKS
        var intTest2 = (int)Convert.ChangeType(test, MyType);

        // explicit type to int WORKS - but as object
        object intTest3 = Convert.ChangeType(test, MyType);


        // cast to my type DOESNT WORK
        var intTest3 =(MyType)Convert.ChangeType(test, MyType);

Thank you!

  • 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-13T21:28:37+00:00Added an answer on May 13, 2026 at 9:28 pm

    This isn’t really about Convert.ChangeType – it’s about casting. The type you’re casting to has to be known at compile time, although it could be a generic type. In this case, as far as the compiler is concerned MyType could refer to any type. It doesn’t “know” that it will definitely have the value typeof(int), so it won’t just emit a cast to int.

    In this case, what would you expect the compile-time type of intTest3 to be?

    What’s the bigger picture here? What are you really trying to do? How would you want to use the value of intTest3?

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

Sidebar

Related Questions

I have the following Powershell code: Add-Type -Assembly System.Configuration [ExeConfigurationFileMap] $configMap = New-Object ExeConfigurationFileMap
i have this code im trying to do for a type of cache system
I have this class stored in C:/CODE/src/ I open up the CMD and type
I have a lot of objects of type ContainedClass stored in an object of
Why does .Net not have the System.Float type like System.String , System.Double etc.?
My web application consists of library type system where books have due dates. I
When defining parameter type that is e.g. in System.Data you have no intellisense and
Are there any standalone type conversion libraries? I have a data storage system that
In C#, there is a type called decimal (the System.Decimal structure). I have found
Trying to send a complex type between two systems that have the same code

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.