Converting an object of type Object to another type using an object of type

Having such an object:

object integerObject=1;

And such an object:

Type integerType=typeof(Int32);

How can an integerType be used to cast an integerObject to an Int32 type

+3
source share
2 answers
int result = (int)Convert.ChangeType(integerObject, integerType);

Of course, this is pretty much a useless exercise in this case, but if your "integerObject" has been defined differently (possibly a string), then this is a valid way to do what I think.

You may be interested in this question: Cast with GetType ()

+3
source

, integerObject, Int32, .

, , , , . , , .

. , Convert.ChangeType - , , , .

+5

Source: https://habr.com/ru/post/1703720/


All Articles