Is it possible to cast an object to a desired type using System.Type?
as the reference?
I had a search and the general consensus was no, although I was hoping there may be some aids introduced in C# 4.0 that could help me.
I.e. the below will not work, but the pseudocode is what I would like.
object o = null;
var t = typeof(string);
...
string foo = (t)o;
Edit: I need use XmlSerializer
to reconstruct / deserialize to the type stored in t
Have a look at:
var foo = Convert.ChangeType(o, typeof(string))
That doesn't make sense.
Casting doesn't change an object at all; it just lets you use the object as the given type at compile-time.
If you don't know what type you're casting it to at compile-time, the cast is useless, since it wouldn't let you do anything with the casted expression.
No need to cast. The object doesn't change, your type of references (variables) changes when "casting".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With