Convert Dynamic to another type

Asked

Viewed 33 times

1

I need to convert a Dynamic variable to another type in Runtime. The type I want to convert is stored in another variable (I don’t know what type at development time).

There’s something like this?

dynamic dynamicVar = 3;
string tipo = "double";
var valor = MetodoQueConverte(dynamicVar,tipo ) ;

The code above is a summary of what I’m doing. The Methodoconverts() It’s just an example of what I need, it doesn’t have to exist. For example, the variable dynamicVar will stay with type int in this case, but I need to store it somewhere else as double.

I need to do a conversion based on a type that is stored as a string in another variable.

  • Dude, I couldn’t understand your question or the code presented. Show an example, there’s nothing dynamic and without seeing the MetodoQueConverte() you have no idea what you’re trying to do. Try to edit the question by providing a [MCVE]

  • https://answall.com/q/286192/101

  • This question should not be closed, because there really is a class in C# that does these types of conversions, it is not cast

  • Bruno be clearer this dynamic type got strange in your question put the value of this dynamicVar!

  • I improved the description.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.