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.