Reflect type on a string C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sniperumm
    New Member
    • Oct 2006
    • 3

    Reflect type on a string C#

    I'm attempting to reflect the type onto a string value within a string array
    using the following:

    string[] types = { 'Int16', 'Boolean' }
    string[] items = { '1', 'true' }

    I could use a switch statement and go through every type and convert the item
    that way, but surely theres a better way of doing it by reflecting the type
    upon the item?
  • Sniperumm
    New Member
    • Oct 2006
    • 3

    #2
    I'll be a bit more clear.

    Basicly I have two string arrays. One which contains the object (integer, boolean etc) as a string, and the other which contains the type.

    string[] item = { '1', 'true' }

    string[] type = { 'Int16', 'Boolean' }

    Both items and types are recieved AS strings. I need to loop through each item and convert from STRING to its type. So '1' will be Int16, and 'true' will be Boolean. Now I can use a switch statement for every type conversion but I was wondering if theres a better way to CAST the type.

    I could use reflection and reflect the Convert class then Invoke its methods to convert at runtime. But is there any other way?

    Comment

    • Sniperumm
      New Member
      • Oct 2006
      • 3

      #3
      This problem has been solved.

      Object o = Convert.ChangeT ype(items[i], Type.GetType("S ystem." + type[i]));

      Comment

      Working...