G
Guest
I want to convert a string (or object) to a primitive type. But I don't know
the type to convert to at run time.
For example l have variable (lets say its an int):
int unknownType = 0;
And a string:
string str = "123";
If I knew the type is int I could have used:
unknownType = Convert.ToInt32(str); or int.Parse(str) etc.
I also could have used the Convert.ChangeType function, but this function
returns an Object and not a primitive type that again need to cast.
Is there a way to perform this kind of casting without using the switch
statement on the Type of the unknownType type ?
the type to convert to at run time.
For example l have variable (lets say its an int):
int unknownType = 0;
And a string:
string str = "123";
If I knew the type is int I could have used:
unknownType = Convert.ToInt32(str); or int.Parse(str) etc.
I also could have used the Convert.ChangeType function, but this function
returns an Object and not a primitive type that again need to cast.
Is there a way to perform this kind of casting without using the switch
statement on the Type of the unknownType type ?