Convert to a datatype given in string format

13,966

Solution 1

You have to map the strings to a type. Since not all of them are System.<yourType> directly, I would consider creating a mapping:

Dictionary<string, Type> types = new Dictionary<string, Type>();
types.Add("int", typeof(System.Int32);
//etc.

Then, use Convert.ChangeType to get your object:

object myObj = Convert.ChangeType(b, types[a]);

Maybe you could extend this by trying to get the type if the key does not exist in your type mapping:

object myObj = Convert.ChangeType(b, Type.GetType("System." + a));

Solution 2

in general if you know the type name you can do this:

Type type = Type.GetType("System.Data.OleDb.OleDbParameter");

for example, so you must anyway know the full name of the type.

said so, if you have an object which was set from the caller to a certain value, you can do a GetType() on the object and get its actual type.

if "True" comes as string, you have no way to distinguish if should be a bool or a string.

Share:
13,966
Mixxiphoid
Author by

Mixxiphoid

Updated on June 23, 2022

Comments

  • Mixxiphoid
    Mixxiphoid almost 2 years

    I have been searching for a solution for quite a while now and couldn't find anything close.

    What I want to achieve:
    I have two strings, 'a' and 'b'. String 'a' contains a datatype, for example "Boolean".
    String b contains a value like "1" or "False", could be anything.
    I want to be able to store the value of string 'b' in a variable which has the datatype of string 'a'.

    If I would itterate trough a list of results with the same values as given in the example the results would be as following:

    Foreach(var value in MyList)
    {
      if(!var ConvertTo a) //Result would be negative because for "1" for it is not a boolean value, however if 'a' is "Int32" the result would be true.
        continue;  
      else 
      {//The value "False" is convertable to boolean, so the result would true
        Create a variable with datatype boolean with the value 'false'.
      }
    }
    

    More or less i'm in search of some strangly hacked version of TryParse().

    I used boolean in my example, but this can be any datatype. At least I should be able to handle atleast the following datatypes:

    • Int, Int32, Int64
    • string
    • Boolean
    • float, decimal
    • DateTime

    My question:
    Is it possible in any way to (try to) convert a value to any datatype given in a string?

    I hope my question and example is clear, if not please leave a comment.

  • Mixxiphoid
    Mixxiphoid over 12 years
    I can't make it work. type will be filled with the name of the data-type, but I already have that. How can I convert a value to the data-type of type? I tried out your code, but "5" stays "5" and won't be converted to int (5). Am I doing something wrong?
  • Mixxiphoid
    Mixxiphoid over 12 years
    I tried this and it helped my a little bit further! I'll have to test more to be sure it all works. Thanks a lot! I'll come back to this.
  • Bas
    Bas over 12 years
    With what are you still having problems?
  • Mixxiphoid
    Mixxiphoid over 12 years
    If the Convert fails it throws an exception, I had to catch it properly :). It all works now! Thanks a lot!