Hi,
I have a System.Type stored in a variable. I wish to Change an object's type to this type.
I have the following code, but cannot get the Type to change to the type.
Ideally I'd like: var intTest3 =(MyType)Convert.ChangeType(test, MyType);
to return an int, when : MyType is a System.Int32
Here's my working so far - where am I going wrong?
// object to cast to int
object test = 1;
// INT32 type
Type MyType = typeof(System.Int32);
// explicit type int WORKS
var intTest = (int)Convert.ChangeType(test, typeof(Int32));
// explicit type to int WORKS
var intTest2 = (int)Convert.ChangeType(test, MyType);
// explicit type to int WORKS - but as object
object intTest3 = Convert.ChangeType(test, MyType);
// cast to my type DOESNT WORK
var intTest3 =(MyType)Convert.ChangeType(test, MyType);
Thank you!