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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:20:48+00:00 2026-06-12T06:20:48+00:00

I want to cast a string to a Type. Is use this code: private

  • 0

I want to cast a string to a Type. Is use this code:

private static T CastToT<T>(string value)
{
    return (T) Convert.ChangeType(value, typeof (T));
}

Type nr5 = CastToT<Type>(typeof(Class1).ToString());

But i got a InvalidCastException.

Invalid cast from ‘System.String’ to ‘System.Type’.

Why can’t I cast the string to a Type?

  • 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-12T06:20:50+00:00Added an answer on June 12, 2026 at 6:20 am

    So if you want to retrieve a Type based on a name, you can simply use this method :

    Type.GetType(typeName);
    

    typeName is the fullname of the type you want to retrieve. This method will work if the type you are looking for is in the current assembly or in mscorlib.dll.

    For example, based on your example :

    private static Type CastToType(string value)
    {
        return Type.GetType(value);
    }
    

    And you call that method by giving the FullName of the type:

    Type nr5 = CastToType(typeof(Random).FullName);
    // nr5 will be null if no corresponding type is found.
    
    if (nr5 != null && nr5.FullName == typeof(Random).FullName)
    {
        // success
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use this code to get a String array of headings used in a
I want to use this code to grab all methods from the assembly Mscorlib.dll
I want to reinterpret cast a function pointer into a void* variable. The type
I want to overload a cast operator, I have the following piece of code
Basically i want to do this. aa causes a bad cast exception. NOTE: o
Here is the code that I use to play around with. I want to
// legacy code void setCacheValue(String name, Object value){ getServletContext().setAttribute(name, value); } Object getCacheValue(String name){
I use EF code-first 4.1. in my application. Now I want to get entities
i'll use some sample code to demonstrate my problem... this is an entity public
Using this code I get the property of some object by its name: string

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.