Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how do i convert string to type? help please!

how do i convert string to type? help please!

Scheduled Pinned Locked Moved C#
questionhelptutorial
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abstar
    wrote on last edited by
    #1

    for example if i have a string like this: string str = "System.Data.OleDb.OleDbParameter"; then i have object like this: object obj; the question how do i convert the type of obj to the value of str? thanks so much

    L 1 Reply Last reply
    0
    • A abstar

      for example if i have a string like this: string str = "System.Data.OleDb.OleDbParameter"; then i have object like this: object obj; the question how do i convert the type of obj to the value of str? thanks so much

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Try the static method Type.GetType(string). It will return a Type object that you can manipulate using reflection. I don't know if there is an easy way to do what you want to do. I will research it.

      █▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██

      A 1 Reply Last reply
      0
      • L Lost User

        Try the static method Type.GetType(string). It will return a Type object that you can manipulate using reflection. I don't know if there is an easy way to do what you want to do. I will research it.

        █▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██

        A Offline
        A Offline
        abstar
        wrote on last edited by
        #3

        thanks

        L 1 Reply Last reply
        0
        • A abstar

          thanks

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Sorry, I thought this would work but it doesn't. I will leave it here in the hopes that it might spark an idea on how to do it.

          string stype = "System.Int32";
          Type t = Type.GetType(stype);

          IConvertible converter = (IConvertible)t; //runtime error, Unable to cast object of type
          //'System.RuntimeType' to type 'System.IConvertible'.

          object obj = converter.ToType(typeof(int), CultureInfo.InvariantCulture);
          //(or)
          int i = (int)converter.ToType(typeof(int), CultureInfo.InvariantCulture);

          InvariantCulture is a static property defined in the System.Globalization.CultureInfo class. -- modified at 1:35 Friday 20th October, 2006

          █▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██

          J 1 Reply Last reply
          0
          • L Lost User

            Sorry, I thought this would work but it doesn't. I will leave it here in the hopes that it might spark an idea on how to do it.

            string stype = "System.Int32";
            Type t = Type.GetType(stype);

            IConvertible converter = (IConvertible)t; //runtime error, Unable to cast object of type
            //'System.RuntimeType' to type 'System.IConvertible'.

            object obj = converter.ToType(typeof(int), CultureInfo.InvariantCulture);
            //(or)
            int i = (int)converter.ToType(typeof(int), CultureInfo.InvariantCulture);

            InvariantCulture is a static property defined in the System.Globalization.CultureInfo class. -- modified at 1:35 Friday 20th October, 2006

            █▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            You're half way there cap'n, you need to use Activator.CreateInstance on that Type object obtained using Type.GetType

            --- How to get answers to your questions[^]

            L A 2 Replies Last reply
            0
            • J J4amieC

              You're half way there cap'n, you need to use Activator.CreateInstance on that Type object obtained using Type.GetType

              --- How to get answers to your questions[^]

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Thanks! I knew that, I just couldn't remember:)

              █▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██

              1 Reply Last reply
              0
              • J J4amieC

                You're half way there cap'n, you need to use Activator.CreateInstance on that Type object obtained using Type.GetType

                --- How to get answers to your questions[^]

                A Offline
                A Offline
                abstar
                wrote on last edited by
                #7

                how do i use the Activator.CreateInstance on the type? thanks

                J 1 Reply Last reply
                0
                • A abstar

                  how do i use the Activator.CreateInstance on the type? thanks

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #8

                  Have you tried the documentation?

                  --- How to get answers to your questions[^]

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups