Dynamic keyword (.NET 4.X) for iOS - alternatives? Or workarounds?

Hi,

I want to use the dynamic keyword (e.g. could be a float, or an int, or a string, etc).

Is there a workaround for this, because it doesn’t seem supported and the workaround detailed in this issue does not work - Unity Issue Tracker - [IL2CPP] NotSupportedExceptions exception is thrown in build with Newtonsoft.Json plugin

I don’t know why the issue got closed, there wasn’t a reason when it got marked “Not Fixed”.

Thank you.

710 people following this question. 0 answers. Bump.

I will say as of right now though–pursuing this avenue is a waste of time and resources. Implement statically typed functions for your code. It may be difficult depending on how much you have, but because IL2CPP is an AOT compiler, it will most likely never support any kind of dynamic type not known at compile time.

You could cast the item to an Object at compile time and then cast the Object to whatever type you need later.

What Chris said in his answer is pretty spot on. However I think I should point out some facts about the dynamic type. The dynamic type was never meant to be used as a normal managed type. It was introduced to simplify bridging with other untyped languages or to work with COM object interfaces.

Please read this documentation carefully. The dynamic type is essentially the same thing as “object”, just with some additional compiler magic and runtime boilerplate. This makes this type rather dangerous and also slow and a huge performance issue. Almost all type and member checks are essentially delayed and implemented in runtime code which makes the usage extremely slow. Since the dynamic type is essentially the type “object”, it means that primitive types are most likely boxed and generate garbage. Even if it doesn’t, it’s most likely by some orders of magnitude slower. It makes debugging much more difficult since the compiler can not really help you with any potential issues. So you really should stay away from the dynamic type whenever possible.